my dog learned polymorphism
The moose likes Sockets and Internet Protocols and the fly likes HttpURLConnection   problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "HttpURLConnection   problem" Watch "HttpURLConnection   problem" New topic

HttpURLConnection problem

Hrishikesh Ghatnekar

Joined: Oct 25, 2004
Posts: 28
I want to open a URL trough a java code with post menthod ,but I am getting errors .

If I copy past the same URL in the browser or try throuth the get method it works fine.

My code is


String parameters= new String(" rity=sadassad&Request_Type=General&Subcategory=XXXX");

URL url = new URL(parameters);

HttpURLConnection urlConn = (HttpURLConnection)url.openConnection();

urlConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
urlConn.setRequestProperty("Content-Length", parameters.length()+"" );
System.out.println("going to open document");
Document xmlRetMsg = new Document(urlConn.getInputStream());
Element root = xmlRetMsg.getRootElement();
System.out.println("got root element ");
String condition = root.getChildText("_condition");
if (condition == null || condition.equals("false")) {

System.out.println("Got _condition null or false with "+xmlRetMsg.getXMLString().toString());



} catch(Exception ee){

After running the prog following is the O/P Connection reset
at testclass.doit(
at testclass.main(
Ilja Preuss

Joined: Jul 11, 2001
Posts: 14112
You did mess up the request, and therefore didn't get any XML back, I'd think.

More specifically, you provided the parameters in the URL, which looks more like a GET request, and you didn't provide any request body content, even though you did set the content length.

It's probably easier to use Jakarta Commons HttpClient for this than doing it manually. (Google for it, it's open source.)

Anyway, Moving to Sockets and Internet Protocols...

The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
I agree. Here's the link:
subject: HttpURLConnection problem
It's not a secret anymore!