Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HttpURLConnection problem

 
Hrishikesh Ghatnekar
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

try{

String parameters= new String("http://myserver.hri.com/GetService/something.aspx?Mode=TEST&msgsEBT.ProcessComet&Action_Required=DELETE+ME%28S%29&Affected_Item=ABCD&Assignee=Analyst+Generic&Assignment_group=TESTGRP&Short_Description=Service+Deco+of+10445+++JuTest&Description=%0A%0A%0A+++++++++++++Additional+Description++++++++++++++++++++%0AC+L++Environment+%28s%29+++%3DTEST%2C+QA%2C+PROD.%0AName%09+++%3DTest%0ASceID%09%09+++%3D10445%0AEstimated++entitlements+%09+++%3D%0AService+usage+in+past+3+months%09+++%3DNO%0APrio rity=sadassad&Request_Type=General&Subcategory=XXXX");


URL url = new URL(parameters);

HttpURLConnection urlConn = (HttpURLConnection)url.openConnection();
urlConn.setRequestMethod("POST");

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){
ee.printStackTrace();
}
}




After running the prog following is the O/P


java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(SocketInputStream.java:168)
at java.io.BufferedInputStream.fill(BufferedInputStream.java:183)
at java.io.BufferedInputStream.read1(BufferedInputStream.java:222)
at java.io.BufferedInputStream.read(BufferedInputStream.java:277)
at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:756)
at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:844)
at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:711)
at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:721)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:626)
at testclass.doit(testclass.java:53)
at testclass.main(testclass.java:21)
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic