Win a copy of Create an Uber Clone in 7 Days: Build a real world full stack mobile app in Java this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Access web url through java code.  RSS feed

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have one problem. I have one web url htpp://127.0.0.1:8080/UserLogin.jsp
Now I have to acees this url though java code not from the web browser.
When I am accessing this page through HttpURLConnection class,
It is ok, but can any body tell me how I can give user name and password through code for this page UserLogin.jsp.

 
Bartender
Posts: 9501
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you using form-based authentication? Send the user name and password as request parameters like this example.
If you are using HTTP based authentication, use the Authenticator class as described here.
 
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If login from a browser does POST on a form, you can simulate that nicely with your URLConnection. Set DoInput and DoOutput true, get the output stream from the connection and write your form content. Don't forget to flush and close the stream.

Here's what I write to log on my Wiki ... the form has two hidden fields and the userid & password entry fields:

See the JavaDoc and the Sun Network Tutorial for more details.
 
Rancher
Posts: 42975
76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See this entry of the Applet FAQ, which describes how to do it. In this regard, applets and applications are identical.
 
Ranch Hand
Posts: 259
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you need to write that code for testing purpose HttpUnit can help.

Please read section Testing pool entry in the link below to know if the link helpshttp://httpunit.sourceforge.net/doc/tutorial/task1editor-entry.html
[ September 19, 2006: Message edited by: karthi keyan ]
 
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you have idea where the form of login.jsp is hitting then you can fake out the login process and there is no harm in this as you are making a tool not the browser.If you know where the login form sumbits like /cool/verify
and username variable is user and password is pass then you can create the url like /cool/verify?user=<userName>&pass=<password> with the url try creating the HTTPURLConnection , similar is the process .The only thing is that user=<userName>&pass=<password> will be in the body and you have to set the header set-content-length header.
 
Beauty is in the eye of the tiny ad.
Why should you try IntelliJ IDEA ?
https://coderanch.com/wiki/696337/IntelliJ-IDEA
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!