总结! http post请求 application/x-www-form-urlencoded body体数据获取不到?
首先,简单介绍下Http请求中Content-Type类型
- 类型格式:type/subtype(;parameter)? type
- 主类型,任意的字符串,如text,如果是*号代表所有;
- subtype 子类型,任意的字符串,如html,如果是*号代表所有;
- parameter 可选,一些参数,如Accept请求头的q参数, Content-Type的 charset参数。
例如: Content-Type: text/html;charset=utf-8;
常见的媒体格式类型如下:
- text/html : HTML格式
- text/plain :纯文本格式
- text/xml : XML格式
- image/gif :gif图片格式
- image/jpeg :jpg图片格式
- image/png:png图片格式
以application开头的媒体格式类型:
- application/xhtml+xml :XHTML格式
- application/xml : XML数据格式
- application/atom+xml :Atom XML聚合格式
- application/json : JSON数据格式
- application/pdf :pdf格式
- application/msword : Word文档格式
- application/octet-stream : 二进制流数据(如常见的文件下载)
- application/x-www-form-urlencoded : <form encType=””>中默认的encType,当form表单请求为get时,数据被编码为key/value格式(name1=value1&name2=value2…),然后把这个字串append到url后面,用?分割,加载这个新的url发送到服务器(表单默认的提交数据的格式);当请求为post时,浏览器把form数据封装到http body中,然后发送到server。(form的enctype属性为编码方式,常用有两种:application/x-www-form-urlencoded和multipart/form-data,默认为application/x-www-form-urlencoded。)
另外一种常见的媒体格式是上传文件之时使用的:
- multipart/form-data : 需要在表单中进行文件上传时,就需要使用该格式
以上就是我们在日常的开发中,经常会用到的若干content-type的内容格式。
最近项目开发对接接口,那边发送http post请求(
已知是xml数据,类似 <?xml version="1.0" encoding="GBK"?><Advpay><PubInfo><ReturnCode>0000</ReturnCode>**其他节点略**</PubInfo</Advpay>),
抓包显示,"Content-Type" 是 "application/x-www-form-urlencoded;charset=GBK",
在body里用如下方式获取数据:(该方式获取不到数据)
public String parserRequest() {
HttpServletRequest request = ServletActionContext.getRequest();
StringBuffer sb = null;
InputStream in = null;
BufferedReader br = null;
try {
in = request.getInputStream();
br = new BufferedReader(new InputStreamReader(in, "utf-8"));
sb = new StringBuffer();
String s = "";
while ((s = br.readLine()) != null) {
sb.append(s);
}
} catch (Exception e) {
e.printStackTrace();
logger.error(e.getMessage(), e);
} finally {
try {
if (br != null) {
br.close();
}
if (in != null) {
in.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
String xml = sb.toString();
if (xml != null && !"".equals(xml)) {
logger.info("报文response xml = " + xml);
}
return xml;
}
这种方式可以:(觉得很怪。。。想着一般post请求,都body体读取流数据
"Content-Type" = "application/x-www-form-urlencoded 这种格式要特别注意)
public String parserRequest() {
HttpServletRequest request = ServletActionContext.getRequest();
String reqXml = null;
try {
Map<String, String[]> map = request.getParameterMap();
StringBuffer xmlBuf = new StringBuffer();
for (Map.Entry<String, String[]> m : map.entrySet()) {
logger.info("返回的报文 key=" + m.getKey() + " Value[0]="
+ m.getValue()[0]);
//打印出来的key=<?xml version value[0]="1.0" encoding="GBK"?><Advpay><PubInfo><ReturnCode>0000</ReturnCode>**其他节点略**</PubInfo</Advpay>
xmlBuf.append(m.getKey());
xmlBuf.append("=");
xmlBuf.append(m.getValue()[0]);
}
reqXml = xmlBuf.toString();
} catch (Exception e) {
logger.error("Exception" + e.getMessage(), e);
}
if (reqXml != null && !"".equals(reqXml)) {
logger.info("报文response reqXml = " + reqXml);
}
return reqXml;
}
原因如下:
在servlet规范3.1.1节里
1. The request is an HTTP or HTTPS request.
2. The HTTP method is POST.
3. The content type is application/x-www-form-urlencoded.
4. The servlet has made an initial call of any of the getParameter family of methods on the request object.
If the conditions are met, post form data will no longer be available for reading directly from the request object’s input stream.
在tomcat的Request.parseParameters方法里,对于application/x-www-form-urlencoded是有做判断的,对这种编码会去解析body里的数据,填充到parameters里,所以后续想再通过流的方式读取body是读不到的(除非你没有触发过getParameter相关的方法)。
tomcat源码。。。待续。。。。。。。。。。。。。
另外,看了下 org.apache.commons.httpclient.HttpClient 的源码 ,设置body体数据编码格式,
postMethod.setRequestEntity(new StringRequestEntity(body,contentType, charSet));
当"Content-Type" = "application/x-www-form-urlencoded;charset=GBK"
charSet="UTF-8" 两个参数都传入时,到底用哪个编码?
由源码看来,以第二个参数 charSet 为准。

未完待续。。。。。。
总结! http post请求 application/x-www-form-urlencoded body体数据获取不到?的更多相关文章
- 请求报错:“应以Content-Type: application/x-www-form-urlencoded为请求类型,在form表单中提交登录信息。"
竟然是post 方法少了参数 // // 摘要: // 以异步操作将 POST 请求发送给指定 URI. // // 参数: // requestUri: // 请求发送到的 URI. // // c ...
- AJAX POST请求中参数以form data和request payload形式在servlet中的获取方式
转载:http://blog.csdn.net/mhmyqn/article/details/25561535 HTTP请求中,如果是get请求,那么表单参数以name=value&name1 ...
- AJAX POST请求中參数以form data和request payload形式在servlet中的获取方式
HTTP请求中,假设是get请求,那么表单參数以name=value&name1=value1的形式附到url的后面,假设是post请求,那么表单參数是在请求体中,也是以name=value& ...
- [转]AJAX POST请求中参数以form data和request payload形式在servlet中的获取方式
转载至 http://blog.csdn.net/mhmyqn/article/details/25561535 最近在写接收第三方的json数据, 因为对java不熟悉,有时候能通过request能 ...
- POST请求中参数以form data和request payload形式+清空数组方式
测试与服务端ajax时用的dva封装的request方法,而后端怎么也拿不到参数.结果返现参数在request payload里. HTTP POST表单请求提交时:Content-Typeappli ...
- aJax请求结果中包含form的问题
jsp页面a.jsp如下: <form action='login' id='formId' method='post'> <input name='user'> </f ...
- ajax请求也可以用form表单向后台提交数据!!!!
激动的我简直语无伦次,不说了上代码,用ajax实现form表单数据的请求,啦啦啦啦 html: <form> <input name="userName" val ...
- c# Application.run和form.show区别
Application.run(form):在当前线程上开始运行标准应用程序消息循环,并使指定窗体可见. form.show() :使指定窗体可见: 参照:https://blog.csdn.net/ ...
- AJAX POST请求中参数以form data和request payload形式在php中的获取方式
一.MINE TYPE问题: php对mime type为“application/x-www-form-urlencoded”(表单提交)和“multipart/form-data”(文件上传)的P ...
随机推荐
- Atitit.用户权限服务 登录退出功能
Atitit.用户权限服务 登录退出功能 参数说明 /com.attilax/user/loginOut.jsp?url="+url Utype=mer 作者:: ★(attilax)&g ...
- Atitit.判断汉字的编码 regedit 注册表里面的reg_sz
Atitit.判断汉字的编码 regedit 注册表里面的reg_sz 1. 可以判断出是unicode编码1 2. 有下面分割ucs2 大头小偷编码1 3. 使用小偷编码测试1 4. 注册表里面的r ...
- Atitit.index manager api design 索引管理api设计
Atitit.index manager api design 索引管理api设计 1. kw1 1.1. 索引类型 unique,normal,fulltxt1 1.2. 聚集索引(clustere ...
- 详解Java中格式化日期的DateFormat与SimpleDateFormat类
DateFormat其本身是一个抽象类,SimpleDateFormat 类是DateFormat类的子类,一般情况下来讲DateFormat类很少会直接使用,而都使用SimpleDateFormat ...
- tensorflow 之模型的保存与加载(三)
前面的两篇博文 第一篇:简单的模型保存和加载,会包含所有的信息:神经网络的op,node,args等; 第二篇:选择性的进行模型参数的保存与加载. 本篇介绍,只保存和加载神经网络的计算图,即前向传播的 ...
- Mysql下Union注入Bypass安全狗过程
文章转载于:http://www.0aa.me/index.php/archives/95/ 一次众测发现个注入,然后有安全狗就顺带看了下安全狗. 先fuzz看看安全狗拦截什么关键词union sel ...
- js定时器(执行一次、重复执行)
代码如下: <script> //定时器 异步运行 function hello(){ alert("hello"); } //使用方法名字执行方法 var t1 = ...
- (转载)【C#4.0】dynamic和var及object
dynamic a = 10;a = a + 10;Console.WriteLine(a.GetType()); 此段代码会输出 System.Int32,第二行不需要类型转换,因为在运行时识别类型 ...
- 2018 ACM ICPC 南京赛区 酱油记
Day 1: 早上6点起床打车去车站,似乎好久没有这么早起床过了,困到不行,在火车上睡啊睡就睡到了南京.南航离南京南站很近,地铁一站就到了,在学校里看到了体验坐直升机的活动,感觉很强.报道完之后去吃了 ...
- HTTP请求的过程&HTTP/1.0和HTTP/1.1的区别&HTTP怎么处理长连接
http://www.cnblogs.com/GumpYan/p/5821193.html