获取 post 请求的原生数据

巴扎黑
Libérer: 2016-11-12 15:17:31
original
1922 Les gens l'ont consulté

有时,有些请求提交的数据不是常见的 参数名:参数值 这种映射关系的键值对,如微信公众平台服务器向开发者的指定URL提交的数据,就是 xml 字符串,这时,无法通过java的request.getParameter("参数名")来取得,也无法通过php的$_POST['参数名']来取得,针对这种数据,解决办法如下:

  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);
Copier après la connexion

补充说明: getReader()与getInputStream()一次请求过来,只能调用一次,并且二者不可同时调用。

Php代码

$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
Copier après la connexion

补充说明:

html from enctype 属性规定在将表单数据发送到服务器之前如何对其进行编码,默认的缺省值是“application/x-www-form-urlencoded”。

application/x-www-form-urlencoded,会将传输的数据编码成键值对的形式,后端可以直接通过request.getParameter()获取

text/plain,数据以纯文本形式进行编码,其中不含任何控件或格式字符。

multipart/form-data,传输的数据要用到多媒体传输协议,由于多媒体传输的都是大量的数据,所以规定上传文件必须是post方法。
在文件上传时,所使用的编码类型应当是“multipart/form-data”,它既可以发送文本数据,也支持二进制数据上载。


Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!