httpClient Post 加号会被转换为空格
2016-05-14 20:35
501 查看
在调试接口时,服务器采用tomcat,ssm架构,post发送数据时遇到一个问题,参数中加号会被转换为空格,
这个主要是url编码协议导致的,关于传输的特殊字符可以参考这篇博客 http://blog.csdn.net/scyatcs/article/details/26766437
记录一下自己解决这个问题的2个方法:
一种是利用 NameValuePair 将数据以键值对的形式中间用&分隔发送到服务端,此时在发送前需要进行urlEncoded编码,代码如下:
HttpClient httpclient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(uri);
HttpMessage msg = new HttpMessage(map);
httpPost.setEntity(new UrlEncodedFormEntity(msg.toList()));
第二种就是以JSONObject的形式发送
httpPost.setHeader("Content-Type", "application/json;charset=UTF-8");
StringEntity s = new StringEntity(msg.getJsonString());
s.setContentEncoding("UTF-8");
s.setContentType("application/json");
httpPost.setEntity(s);
HttpResponse response = httpclient.execute(httpPost);
public class HttpMessage {
private HashMap<String,Object> map;
public HttpMessage(HashMap<String,Object> map)
{
this.map=map;
}
// post 数据 参数以 & 连接
public List<NameValuePair> toList(){
List <NameValuePair> nvps = new ArrayList<NameValuePair>();
if(map != null && !map.isEmpty()){
for(Map.Entry<String, Object> entry : map.entrySet()){
nvps.add(new BasicNameValuePair(entry.getKey(), String.valueOf(entry.getValue())));
}
}
return nvps;
}
// post 数据 参数 JSON格式
public String getJsonString() throws JSONException{
JSONObject jsonStr = new JSONObject();
if(map != null && !map.isEmpty()){
for(Map.Entry<String, Object> entry : map.entrySet()){
jsonStr.put(entry.getKey(), String.valueOf(entry.getValue()));
}
}
return jsonStr.toString();
}
}
这个主要是url编码协议导致的,关于传输的特殊字符可以参考这篇博客 http://blog.csdn.net/scyatcs/article/details/26766437
记录一下自己解决这个问题的2个方法:
一种是利用 NameValuePair 将数据以键值对的形式中间用&分隔发送到服务端,此时在发送前需要进行urlEncoded编码,代码如下:
HttpClient httpclient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(uri);
HttpMessage msg = new HttpMessage(map);
httpPost.setEntity(new UrlEncodedFormEntity(msg.toList()));
第二种就是以JSONObject的形式发送
httpPost.setHeader("Content-Type", "application/json;charset=UTF-8");
StringEntity s = new StringEntity(msg.getJsonString());
s.setContentEncoding("UTF-8");
s.setContentType("application/json");
httpPost.setEntity(s);
HttpResponse response = httpclient.execute(httpPost);
public class HttpMessage {
private HashMap<String,Object> map;
public HttpMessage(HashMap<String,Object> map)
{
this.map=map;
}
// post 数据 参数以 & 连接
public List<NameValuePair> toList(){
List <NameValuePair> nvps = new ArrayList<NameValuePair>();
if(map != null && !map.isEmpty()){
for(Map.Entry<String, Object> entry : map.entrySet()){
nvps.add(new BasicNameValuePair(entry.getKey(), String.valueOf(entry.getValue())));
}
}
return nvps;
}
// post 数据 参数 JSON格式
public String getJsonString() throws JSONException{
JSONObject jsonStr = new JSONObject();
if(map != null && !map.isEmpty()){
for(Map.Entry<String, Object> entry : map.entrySet()){
jsonStr.put(entry.getKey(), String.valueOf(entry.getValue()));
}
}
return jsonStr.toString();
}
}
相关文章推荐
- 【caffe配置】在vs2013用已经训练好的Alexnet网络提取fc6特征
- Windows网络编程学习笔记(4) 编写一个TCP客户端
- Httpservlet cannot be resolved to a type的原因与解决方法
- debian8.4下安装Snort处理tcpdump文件技术细节
- 如何简单形象又有趣地讲解神经网络是什么?
- 1、神经网络
- 深入浅出TCP/IP簇
- 【Java8网络编程】第3章. HTTP和URLConnection类
- Android通过Http连接MySQL 实现登陆/注册(数据库+服务器+客户端)
- Http请求中Content-Type讲解以及在Spring MVC中的应用
- 网络数据加密, 数据安全
- 神经网络编程入门
- spring mvc 实现http异步请求处理
- 网络请求——XML解析
- 机器学习中使用的神经网络第五讲笔记
- Linux网络编程:socket文件传输
- Map<String, HttpSession> map=(Map<String, HttpSession>)
- NAT技术和代理服务器
- HTTP的请求头标签 If-Modified-Since
- 网络流二十四题之十 —— 星际转移问题(HOME)