File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "HttpURLConnection   problem" Watch "HttpURLConnection   problem" New topic
Author

HttpURLConnection problem

Hrishikesh Ghatnekar
Greenhorn

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

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

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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: HttpURLConnection problem