aspose file tools*
The moose likes Sockets and Internet Protocols and the fly likes Simulate POST using HttpURLConnection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "Simulate POST using HttpURLConnection" Watch "Simulate POST using HttpURLConnection" New topic
Author

Simulate POST using HttpURLConnection

Ekaterina Galkina
Greenhorn

Joined: Sep 14, 2010
Posts: 15
Hi guys,

I need to implement the following task - login into a site (e.g. http://myexample.com) using java. I chose HttpURLConnection. I need to simulate POST request, user name and password are provided.
When I use browser login is successfull, but the code below rises an Exception (java.io.IOException: Server returned HTTP response code: 408 for URL). That means Request timeout. Why? How to avoid it?

BTW, in browser login is successfull, I see the welcome page. But Firebug demonstrates response code 302.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41572
    
  54
I'd use a library like HtmlUnit for this. It takes care of all the nitty gritty details of HTTP and HTML forms, including cookies and sessions, so your code doesn't have to.


Ping & DNS - my free Android networking tools app
Ekaterina Galkina
Greenhorn

Joined: Sep 14, 2010
Posts: 15
Ulf Dittmer wrote:I'd use a library like HtmlUnit for this. It takes care of all the nitty gritty details of HTTP and HTML forms, including cookies and sessions, so your code doesn't have to.


Thank you. Can anyone provide htmlunit for me? http://sourceforge.net/projects/htmlunit/files/htmlunit/ download seems not working.

Update: Now downloading is enabled. I've got it.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Simulate POST using HttpURLConnection