Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

HttpURLConnection and HTTP response code: 411

843841Sep 17 2007 — edited Sep 18 2007
Hi there,

I am trying to post an xml message to a sever, but I get the following exception back:

java.io.IOException: Server returned HTTP response code: 411 for URL: http://www.myurl.com/...

So, I did add connection.setRequestProperty("Content-Length", "" + encodedData.length()); to my code. Not sure why I have to do this, but anyways. Adding the above line to my code causes the following exception:

java.net.SocketException: Unexpected end of file from server

Any help would be appreciated.

Here is my code:
	public String submitRequestTest(Document doc) throws MalformedURLException, IOException {

		XMLOutputter outputter = new XMLOutputter();
		
		String encodedData = URLEncoder.encode(outputter.outputString(doc));
		
		URL url = new URL("http://www.myurl.com/api.aspx?xml=" + encodedData);
		
		HttpURLConnection connection = (HttpURLConnection)url.openConnection();
		connection.setRequestProperty("Content-Length", "" + encodedData.length());
		connection.setRequestMethod("POST");
		connection.setUseCaches(false);
		connection.setDoOutput(true);
		connection.setDoInput(true);

		java.io.BufferedReader reader = new java.io.BufferedReader(new java.io.InputStreamReader(connection.getInputStream()));

		String respMessage = "";
		String response = reader.readLine();
		
		while(response != null ) {
			respMessage += response;
			
			response = reader.readLine();
		}

		return respMessage;	
	}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Oct 16 2007
Added on Sep 17 2007
8 comments
2,743 views