Httpurlconnection is a Java network that supports HTTP. It is convenient to use it to access the content of a URL. However, a problem occurs recently, that is, when a POST request is sent, the error 502: Bad Gateway is always returned. However, the same code, the same IDE, the same JDK, and the same system environment are returned, using Apache's open-source httpclient to send the same POST request is normal returns 200; more strange is that using the JDK1.6.0-14 version everything works, returns 200, but with the JDK1.6.0-20/24 but returns 502. At first, I suspected that JDK had a bug. However, it turns out that when you suspect that a large system has a problem, the problem always exists on your own. Only a very small probability will it happen.
In addition to questions about JDK, we also think that the httpurlconnection class is improperly used. Google has been working for a long time. The following describes how to use httpurlconnection:
For http post and put requests, follow these steps:
- Generate an httpurlconnection instance and set HTTP request parameters or attributes;
- Call the getoutputstream method to obtain outputstream and output data to the output stream. Whether or not you have data to send to the server, At least you need to call the getoutputstream () method to ignore data writing;
- Call the getinputstream method to send a real HTTP request and wait for the response from the server. Whether you need server feedback or not, this must be called.
For HTTP requests except post and put (such as get, Head, delete, Trace, options), follow these steps:
- Generate an httpurlconnection instance and set HTTP request parameters or attributes;
- Call the getinputstream method to send a real HTTP request and wait for the response from the server.
We cannot assume that the actual HTTP request is sent to the server by calling getoutputstream () to obtain outputstream, write data, and then calling the flush ()/close () method of the output stream.In fact, the getinputstream () method that calls httpurlconnection is the only method that sends a real request. The getoutputstream () method does not send a real HTTP request.
In addition, when using httpurlconnection, you must follow the execution sequence described above based on the request type (post, get, etc.). For example, you have already called the getinputstream () method, when you call the getoutputstream () method, an exception is thrown. httpurlconnection says this is illegal.
The strange problem I encountered was that the getoutputstream () method was not called when a POST request was sent, and the 2nd steps in the execution sequence of the post/put request were omitted. Similarly, if getoutputstream () is called in a GET request, the JDK Code shows that the request is automatically converted to a POST request. For backward compatibility reasons, it does not tell you and no exception is thrown. Instead of post or put requests, an exception is directly thrown. You can also see from the code that using post or put, or calling getoutputstream () in a GET request must be set to setdooutput (true );
Public synchronized outputstream getoutputstream () throws ioexception {<br/> If (! Dooutput) {<br/> throw new protocolexception (<br/> "cannot write to a urlconnection" + "If dooutput = false-call setdooutput (true )"); <br/>}< br/> If (method. equals ("get") {<br/> method = "Post"; // backward compatibility <br/>}< br/> If (! "Post". Equals (method )&&! "Put ". equals (method) & "HTTP ". equals (URL. getprotocol () {<br/> throw new protocolexception ("HTTP Method" + method <br/> + "doesn't support output "); <br/>}< br/> //....... <br/>}
At this time, we can see that Java's lengthy method names aim to improve readability. Variable names are misleading at this time. Another problem is the inconsistency between different JDK 1.6.0 versions. The reason is unknown.