获取 request 中用POST方式"Content-type"是"application/x-www-form-urlencoded;charset=utf-8"发送的 json 数据
2017-12-29 17:02
691 查看
request中发送json数据用post方式发送Content-type用application/json;charset=utf-8方式发送的话,直接用springMVC的@RequestBody标签接收后面跟实体对象就行了,spring会帮你自动拼装成对象,如果Content-type设置成application/x-www-form-urlencoded;charset=utf-8就不能用spring的东西了,只能以常规的方式获取json串了
方式一:通过流的方方式
[java] view
plain copy
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
/**
* request 对象的相关操作
* @author zhangtengda
* @version 1.0
* @created 2015年5月2日 下午8:25:43
*/
public class GetRequestJsonUtils {
/***
* 获取 request 中 json 字符串的内容
*
* @param request
* @return : <code>byte[]</code>
* @throws IOException
*/
public static String getRequestJsonString(HttpServletRequest request)
throws IOException {
String submitMehtod = request.getMethod();
// GET
if (submitMehtod.equals("GET")) {
return new String(request.getQueryString().getBytes("iso-8859-1"),"utf-8").replaceAll("%22", "\"");
// POST
} else {
return getRequestPostStr(request);
}
}
/**
* 描述:获取 post 请求的 byte[] 数组
* <pre>
* 举例:
* </pre>
* @param request
* @return
* @throws IOException
*/
public static byte[] getRequestPostBytes(HttpServletRequest request)
throws IOException {
int contentLength = request.getContentLength();
if(contentLength<0){
return null;
}
byte buffer[] = new byte[contentLength];
for (int i = 0; i < contentLength;) {
int readlen = request.getInputStream().read(buffer, i,
contentLength - i);
if (readlen == -1) {
break;
}
i += readlen;
}
return buffer;
}
/**
* 描述:获取 post 请求内容
* <pre>
* 举例:
* </pre>
* @param request
* @return
* @throws IOException
*/
public static String getRequestPostStr(HttpServletRequest request)
throws IOException {
byte buffer[] = getRequestPostBytes(request);
String charEncoding = request.getCharacterEncoding();
if (charEncoding == null) {
charEncoding = "UTF-8";
}
return new String(buffer, charEncoding);
}
}
方式二:通过获取Map的方式处理
这种刚方式存在弊端,如果json数据中存在=号,数据会在等号的地方断掉,后面的数据会被存储成map的values,需要重新拼装key和values的值,拼装成原来的json串
[java] view
plain copy
/**
* 方法说明 :通过获取map的方式
*/
@SuppressWarnings("rawtypes")
private String getParameterMap(HttpServletRequest request) {
Map map = request.getParameterMap();
String text = "";
if (map != null) {
Set set = map.entrySet();
Iterator iterator = set.iterator();
while (iterator.hasNext()) {
Map.Entry entry = (Entry) iterator.next();
if (entry.getValue() instanceof String[]) {
logger.info("==A==entry的key: " + entry.getKey());
String key = (String) entry.getKey();
if (key != null && !"id".equals(key) && key.startsWith("[") && key.endsWith("]")) {
text = (String) entry.getKey();
break;
}
String[] values = (String[]) entry.getValue();
for (int i = 0; i < values.length; i++) {
logger.info("==B==entry的value: " + values[i]);
key += "="+values[i];
}
if (key.startsWith("[") && key.endsWith("]")) {
text = (String) entry.getKey();
break;
}
} else if (entry.getValue() instanceof String) {
logger.info("==========entry的key: " + entry.getKey());
logger.info("==========entry的value: " + entry.getValue());
}
}
}
return text;
}
方式三:通过获取所有参数名的方式
这种方式也存在弊端 对json串中不能传特殊字符,比如/=, \=, /, ~等的这样的符号都不能有如果存在也不会读出来,他的模式和Map的方式是差不多的,也是转成Map处理的
[java] view
plain copy
/**
* 方法说明 :通过获取所有参数名的方式
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
private String getParamNames(HttpServletRequest request) {
Map map = new HashMap();
Enumeration paramNames = request.getParameterNames();
while (paramNames.hasMoreElements()) {
String paramName = (String) paramNames.nextElement();
String[] paramValues = request.getParameterValues(paramName);
if (paramValues.length == 1) {
String paramValue = paramValues[0];
if (paramValue.length() != 0) {
map.put(paramName, paramValue);
}
}
}
Set<Map.Entry<String, String>> set = map.entrySet();
String text = "";
for (Map.Entry entry : set) {
logger.info(entry.getKey() + ":" + entry.getValue());
text += entry.getKey() + ":" + entry.getValue();
logger.info("text------->"+text);
}
if(text.startsWith("[") && text.endsWith("]")){
return text;
}
return "";
}
附上一点常用的Content-type的方式
最后附上发送方式的连接
http://blog.csdn.net/mingtianhaiyouwo/article/details/51381853
方式一:通过流的方方式
[java] view
plain copy
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
/**
* request 对象的相关操作
* @author zhangtengda
* @version 1.0
* @created 2015年5月2日 下午8:25:43
*/
public class GetRequestJsonUtils {
/***
* 获取 request 中 json 字符串的内容
*
* @param request
* @return : <code>byte[]</code>
* @throws IOException
*/
public static String getRequestJsonString(HttpServletRequest request)
throws IOException {
String submitMehtod = request.getMethod();
// GET
if (submitMehtod.equals("GET")) {
return new String(request.getQueryString().getBytes("iso-8859-1"),"utf-8").replaceAll("%22", "\"");
// POST
} else {
return getRequestPostStr(request);
}
}
/**
* 描述:获取 post 请求的 byte[] 数组
* <pre>
* 举例:
* </pre>
* @param request
* @return
* @throws IOException
*/
public static byte[] getRequestPostBytes(HttpServletRequest request)
throws IOException {
int contentLength = request.getContentLength();
if(contentLength<0){
return null;
}
byte buffer[] = new byte[contentLength];
for (int i = 0; i < contentLength;) {
int readlen = request.getInputStream().read(buffer, i,
contentLength - i);
if (readlen == -1) {
break;
}
i += readlen;
}
return buffer;
}
/**
* 描述:获取 post 请求内容
* <pre>
* 举例:
* </pre>
* @param request
* @return
* @throws IOException
*/
public static String getRequestPostStr(HttpServletRequest request)
throws IOException {
byte buffer[] = getRequestPostBytes(request);
String charEncoding = request.getCharacterEncoding();
if (charEncoding == null) {
charEncoding = "UTF-8";
}
return new String(buffer, charEncoding);
}
}
方式二:通过获取Map的方式处理
这种刚方式存在弊端,如果json数据中存在=号,数据会在等号的地方断掉,后面的数据会被存储成map的values,需要重新拼装key和values的值,拼装成原来的json串
[java] view
plain copy
/**
* 方法说明 :通过获取map的方式
*/
@SuppressWarnings("rawtypes")
private String getParameterMap(HttpServletRequest request) {
Map map = request.getParameterMap();
String text = "";
if (map != null) {
Set set = map.entrySet();
Iterator iterator = set.iterator();
while (iterator.hasNext()) {
Map.Entry entry = (Entry) iterator.next();
if (entry.getValue() instanceof String[]) {
logger.info("==A==entry的key: " + entry.getKey());
String key = (String) entry.getKey();
if (key != null && !"id".equals(key) && key.startsWith("[") && key.endsWith("]")) {
text = (String) entry.getKey();
break;
}
String[] values = (String[]) entry.getValue();
for (int i = 0; i < values.length; i++) {
logger.info("==B==entry的value: " + values[i]);
key += "="+values[i];
}
if (key.startsWith("[") && key.endsWith("]")) {
text = (String) entry.getKey();
break;
}
} else if (entry.getValue() instanceof String) {
logger.info("==========entry的key: " + entry.getKey());
logger.info("==========entry的value: " + entry.getValue());
}
}
}
return text;
}
方式三:通过获取所有参数名的方式
这种方式也存在弊端 对json串中不能传特殊字符,比如/=, \=, /, ~等的这样的符号都不能有如果存在也不会读出来,他的模式和Map的方式是差不多的,也是转成Map处理的
[java] view
plain copy
/**
* 方法说明 :通过获取所有参数名的方式
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
private String getParamNames(HttpServletRequest request) {
Map map = new HashMap();
Enumeration paramNames = request.getParameterNames();
while (paramNames.hasMoreElements()) {
String paramName = (String) paramNames.nextElement();
String[] paramValues = request.getParameterValues(paramName);
if (paramValues.length == 1) {
String paramValue = paramValues[0];
if (paramValue.length() != 0) {
map.put(paramName, paramValue);
}
}
}
Set<Map.Entry<String, String>> set = map.entrySet();
String text = "";
for (Map.Entry entry : set) {
logger.info(entry.getKey() + ":" + entry.getValue());
text += entry.getKey() + ":" + entry.getValue();
logger.info("text------->"+text);
}
if(text.startsWith("[") && text.endsWith("]")){
return text;
}
return "";
}
附上一点常用的Content-type的方式
application/x-javascript text/xml->xml数据 application/x-javascript->json对象 application/x-www-form-urlencoded->表单数据 application/json;charset=utf-8 -> json数据
最后附上发送方式的连接
http://blog.csdn.net/mingtianhaiyouwo/article/details/51381853
相关文章推荐
- 获取 request 中用POST方式"Content-type"是"application/x-www-form-urlencoded;charset=utf-8"发送的 json 数据
- 获取 request 中用POST方式"Content-type"是"application/x-www-form-urlencoded;charset=utf-8"发送的 json 数据
- (转)获取 request 中用POST方式"Content-type"是"application/x-www-form-urlencoded;charset=utf-8"发送的 json 数据
- 获取 request 中用POST方式"Content-type"是"application/json"发送的 json 数据
- 未指定Content-Type: application/x-www-form-urlencoded将会导致服务端获取Ajax POST数据失败
- 通过接口提交数据,Content-Type应该是application/x-www-form-urlencoded还是application/json?
- axios post请求数据,在Content-Type为application/x-www-form-urlencoded情况下,data如何转换为json的解决办法
- Content-Type:application/x-www-form-urlencoded; charset=UTF-8 设置方法 针对AFN或者是原生
- Retrofit学习"Content-Type: application/json;charset=UTF-8"@Body的使用
- Retrofit学习"Content-Type: application/json;charset=UTF-8"@Body的使用
- myxmlHttpRequset.setRequestHeader("Content-Type","application/x-www-form-urlencoded
- PHP content-type为"application/json"的post过来的数据$_POST接受不到的问题
- 四种常见的 POST 提交数据方式(application/x-www-form-urlencoded,multipart/form-data,application/json,text/xml)
- 解决content-type为"application/json"的post过来的数据在php端接受不到的问题
- application/x-www-form-urlencoded 的contentType,POST数据内容过大,导致tomcat的request取不到参数
- POST方式传值!context.Request.Form["UserID"];获取不到数据context.Request.Params["UserID"]却可以
- Content type expected:<application/json;charset=UTF-8> but was:<application/hal+json;charset=UTF-8>
- Content type 'application/json;charset=UTF-8' not
- dhl:ajax无法跨域改用getJSON(解决服务器返回json数据中文为乱码的问题$.ajaxSetup({ scriptCharset: "utf-8" , contentType: "application/json; chars)