Request. getinputstream () reading the XML file passed by post

Source: Internet
Author: User

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

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.