HttpURLConnection 411 Error Resolution

Source: Internet
Author: User

1. Description of the problem

The Java HttpURLConnection class sends an HTTP request link to an extranet that returns Responsecode to 411. NET server, the reason for the online identification may come from the following:

1, there may be no HTTP in the Content-length parameters, this generally has.
2, may be because the request method is wrong, originally only obtains the data, should use gets the way, but you use the post the Way.

first, the first method:

Conn.setrequestproperty ("content-length", "" "+info.getbytes (). Length);

After setting or 411 error

The second method: change the request mode to get, Success.

however, the feedback request method of the United side has not changed, still post, very puzzled ..... After work, using the browser HTTP online test tool to try, found that after adding parameter content-length, Post method can also request Success.

In turn, print the log and discover that the property "content-length" is null ... That is, httpurlconnection call Setrequestproperty is Invalid. So here also remind yourself, Self-Test can avoid communication waste.

HTTP test with fiddler is recommended Here. Locate the problem and check it Out.

http://blog.csdn.net/Feng______/article/details/38301293 This person summed up very well, himself also went to see the source Code.

private static final set<string> restrictedheaderset;    private static final string[] restrictedheaders = {/* Restricted by XMLHttpRequest2 *///"accept-charset",//" Accept-encoding "," access-control-request-headers "," access-control-request-method "," Connection ",/* Close is Allowed */"content-length",//"Cookie",//"Cookie2", "content-transfer-encoding",//"Date", "Expect", "Host", " Keep-alive "," Origin ",//" Referer ",//" TE "," Trailer "," transfer-encoding "," Upgrade ",//" user-agent "," Via "    };

The discovery of allowrestrictedheaders in static initialization code is determined by the return value of the security manager.

The contents of the Restrictedheaderset are determined by the Restrictedheaders array.

The reason for this is that for security reasons, these fields should not be altered as much as Possible. Sun has made this set of security Mechanisms.

The workaround is to add the property to True when the JVM starts sun.NET.http.allowRestrictedHeaders .

first, the order of the headers is Irrelevant. Second, in order to manually override the host header you need to set sun.net.http.allowrestrictedheaders=trueeither in Co De

Add the following code directly inside the Program. And then It's Ok.

System.setproperty ("sun.net.http.allowRestrictedHeaders", "true");

HttpURLConnection interface

Http://www.cjsdn.net/doc/jdk50/java/net/HttpURLConnection.html

Http://www.cnblogs.com/skyaccross/archive/2012/12/22/2828986.html

2. Dooutput and doinput instructions

Httpurlconnection.setdooutput (true)

Httpurlconnection.setdoinput (true)

These two methods are not found in the HttpURLConnection method of the Develope.

The GET request does not use Conn.getoutputstream () because the parameter is appended directly to the address, so the default is False.

Post requests (for example, file Uploads) require a large amount of data to be transferred to the service area, which is placed inside the body of the http, so you need to write the data to the server after the connection is Established.

Because Conn.getinputstream () is always used to get the response from the server, the default value is True.

HttpURLConnection 411 Error Resolution

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.