Obtain the native data of the post request

Source: Internet
Author: User
Sometimes, the data submitted by some requests is not a common Parameter Name: Key-value pair of the ing relationship of parameter values, such as the data submitted by the public platform server to the developer's specified URL, is the xml string... sometimes, the data submitted by some requests is not a common Parameter Name: Key-value pairs of the ing relationship such as parameter values. For example, the data submitted by the public platform server to the developer's specified URL is an xml string, at this time, you cannot use the java request. getParameter ("parameter name") cannot be obtained through $ _ POST ['parameter name'] in php. The solution for this data is as follows:

  request.setCharacterEncoding("utf-8");StringBuilder buffer = new StringBuilder();java.io.BufferedReader reader=null;try{/*** getReader() * Retrieves the body of the request as character data using a BufferedReader* getInputStream() * Retrieves the body of the request as binary data using a ServletInputStream.*/reader = request.getReader();String line=null;while((line = reader.readLine())!=null){buffer.append(line);       }}catch(java.io.IOException e){e.printStackTrace();}finally{if(null!=reader){try {reader.close();} catch (java.io.IOException e) {e.printStackTrace();}}}String res = buffer.toString();System.out.print(res);

Note: getReader () and getInputStream () can be called only once and cannot be called simultaneously.

Php code

$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];

Note:

The html from enctype attribute specifies how to encode the form data before it is sent to the server. The default value is "application/x-www-form-urlencoded ".

Application/x-www-form-urlencoded: The transmitted data is encoded as a key-value pair. the backend can directly obtain the data through request. getParameter ().

Text/plain, data is encoded in plain text format, which does not contain any control or format characters.

Multipart/form-data, the data to be transmitted must use the multimedia transmission protocol. Because multimedia transmission involves a large amount of data, it is required that the file to be uploaded must be post.
During file upload, the encoding type used should be "multipart/form-data". It can send text data and upload binary data.

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.