Processing method of HTTP 411 error code

When a JAVA program requests a remote URL, it sometimes gets the following error message:
java.io.IOException: Server returned HTTP response code: 411
This is because some Web servers will reject a request made to the Web server using HTTP POST without providing the appropriate BODY data. We can see the official story disseminated in explanation of the error code 411 (http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html) :

10.4.12 411 Length Required
The server refuses to accept the request without a defined Content- Length. The client MAY repeat the request if it adds a valid Content-Length header field containing the length of the message-body in the request message.
The solution is to set content-length =0 in the request header and write an empty BODY data into the OutputStream.
URL the URL = new URL (” http://www.webservicex.net/MortgageIndex.asmx/GetCurrentMortgageIndexByWeekly “);

final HttpURLConnection conn = (HttpURLConnection) url.openconnection ();

conn. SetDoInput (true);
conn. SetDoOutput (true);
conn. SetRequestMethod (” POST “);

conn. SetRequestProperty (” the content-type “, “text/XML”);

conn. SetRequestProperty (” the Content – Length “, “0”);
DataOutputStream OS = new DataOutputStream(conn. GetOutputStream ());
OS. Write (“. “getBytes (” utf-8″), 0, 0).
OS. Flush ();
OS. The close ();

conn. The connect ();

InputStream is = conn. GetInputStream ();

Integer code = conn. GetResponseCode ();
final String contentType = conn. GetContentType ();
System. Out. Println (code + + contentType “:”);

Read More: