Home > Backend Development > PHP Tutorial > Get the native data of the post request

Get the native data of the post request

巴扎黑
Release: 2016-11-12 15:17:31
Original
1998 people have browsed it

Sometimes, the data submitted by some requests is not the common parameter name: the key-value pair of the parameter value mapping relationship. For example, the data submitted by the WeChat public platform server to the developer's specified URL is an xml string. At this time, it cannot be passed It cannot be obtained through java's request.getParameter("parameter name"), nor through php's $_POST['parameter name']. For this kind of data, the solution 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);
Copy after login

Additional explanation: getReader() and getInputStream () can only be called once once requested, and both cannot be called at the same time.

Php code

$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
Copy after login

Additional explanation:

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

application/x-www-form-urlencoded will encode the transmitted data into the form of key-value pairs. The backend can directly obtain

text/plain through request.getParameter(), and the data will be encoded in plain text, where Does not contain any controls or formatting characters.

multipart/form-data, the data transmitted must use the multimedia transmission protocol. Since multimedia transmits a large amount of data, it is stipulated that the uploaded file must be the post method.
When uploading files, the encoding type used should be "multipart/form-data", which can send text data and also supports binary data upload.


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template