Recently, a project needs to receive XML data packets sent from post. In the servlet method, request. getinputstream () is called to find that no data can be read.
The following is the post data program code: Java code
URL url = new URL (urlbuf); <br/> httpurlconnection = (httpurlconnection) URL <br/>. openconnection (); <br/> httpurlconnection. setdooutput (true); <br/> httpurlconnection. setdoinput (true); <br/> httpurlconnection. setrequestmethod ("Post"); httpurlconnection. setconnecttimeout (5000); <br/> httpurlconnection. setreadtimeout (5000); <br/> httpurlconnection. connect (); <br/> bufferedwriter out = new bufferedwriter (New outputstreamwriter (<br/> httpurlconnection. getoutputstream (), "GBK"); <br/> out. write (Word); <br/> out. flush ();
This is to read the program code: Java code
Bufferedreader BR = new bufferedreader (New inputstreamreader (servletinputstream) request. getinputstream (); <br/> string line = NULL; <br/> stringbuilder sb = new stringbuilder (); <br/> while (line = BR. readline ())! = NULL) {<br/> Sb. append (line); <br/>}< br/>
At first, I thought it was a low-level error. I kept debugging to find the cause, but it was fruitless. There are different opinions and there is no good solution when I search for Baidu. I also thought about it. The file upload is also handled on the servlet facade. The component must use inpputstream without any problems. It is not possible that it will not work here,
Add the following Java code to the post program:
Httpurlconnection. setrequestproperty ("Content-Type", "text/html");, the test is successful!
According to the above phenomenon, it is not difficult to see that the servlet does not specify the Content-Type request, and some processing is performed during encapsulation, resulting in the failure to obtain the request in the servlet. getinputstream () and request. getreader ().
Reprint please indicate the source http://blog.csdn.net/shimiso
Technical Exchange Group: 173711587