client.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 1000000);
client.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 1000000);
HttpPost post = new HttpPost(url);
NameValuePair rq = new BasicNameValuePair("rq", requestObj.toString());
BasicNameValuePair data_type = new BasicNameValuePair("type", req_type);
NameValuePair sid = new BasicNameValuePair("sid", "web123");
List<NameValuePair> list = new ArrayList<NameValuePair>();
list.add(rq);
list.add(sid);
list.add(data_type);
StringEntity entity = new UrlEncodedFormEntity(list, HTTP.UTF_8);
post.setEntity(entity);
HttpResponse res = client.execute(post);
String respStr = EntityUtils.toString(res.getEntity(),HTTP.UTF_8);
this.inputStream = new ByteArrayInputStream(respStr.toString().getBytes("utf-8"));
return SUCCESS;

方式二:

 //1、URL对象创建一个应用程序与url之间链接urlconnection对象
URL connectUrl = new URL(url);
HttpURLConnection conn = (HttpURLConnection) connectUrl.openConnection();
//2、设置属性
//post请求必须设置的两个
conn.setDoInput(true);
conn.setDoOutput(true);
//设置属性
conn.setUseCaches(false);
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("connection", "Keep-Alive");
//打开与url之间的连接
conn.connect();
//如果使用URLconnection既要读取输入流 又要传参数 那么一定要先使用输出流 在使用输入流
//getOutputStream 中包含了connect 也就是说使用了getoutputStream的时候connect可以不写
OutputStream os = conn.getOutputStream();
//设置编码 防止到服务端出现中文乱码
OutputStreamWriter ow = new OutputStreamWriter(os, HTTP.UTF_8);
PrintWriter pw = new PrintWriter(ow,true);
//之前所有的参数只是写入写出流的缓存中并没有发送到服务端,执行下面这句话后表示将参数信息发送到服务端
pw.println("rq="+requestObj.toString()+"&sid=web123&type="+req_type);
pw.flush();
//获取服务端返回的信息
InputStream is = conn.getInputStream();
//设置编码 防止读取到的数据乱码
BufferedReader br = new BufferedReader(new InputStreamReader(is,HTTP.UTF_8)); String line = null;
String respStr = "";
while((line=br.readLine())!=null){
respStr+=line;
}
this.inputStream = new ByteArrayInputStream(respStr.toString().getBytes("utf-8"));
return SUCCESS;

在服务端接收到requestObj.toString()中文乱码 可用在创建Entity时指定编码  StringEntity entity = new UrlEncodedFormEntity(list, HTTP.UTF_8);

在返回的数据中也出现了中文乱码 可使用EntityUtils.toString指定字符编码     String respStr = EntityUtils.toString(res.getEntity(),HTTP.UTF_8);

HttpPost (URLConnection)传参数中文乱码的更多相关文章

  1. springmvc框架下ajax请求传参数中文乱码解决

    springmvc框架下jsp界面通过ajax请求后台数据,传递中文参数到后台显示乱码 解决方法:js代码 运用encodeURI处理两次 /* *掩码处理 */ function maskWord( ...

  2. 关于url传参中文乱码问题

    之前都一直很不了解中文编码得问题,之前在做项目中没碰到那么头痛的问题.所以一直没有了解中文乱码的问题. 问题描述: 地址: http://localhost:8080/sun-government/c ...

  3. 详解get请求和post请求参数中文乱码的解决办法

    首先出现中文乱码的原因是tomcat默认的编码方式是"ISO-8859-1",这种编码方式以单个字节作为一个字符,而汉字是以两个字节表示一个字符的. 一,get请求参数中文乱码的解 ...

  4. jsp页面间传递参数 中文乱码问题(zz)

      jsp页面间传递参数 中文乱码问题 1.传递参数 var url = "*****Test.jsp?param1="+encodeURI(encodeURI(str));//对 ...

  5. soapUI参数中文乱码问题解决方法 (groovy脚本中文乱码)

    soapUI参数中文乱码问题解决方法 可能方案1: 字体不支持中文,将字体修改即可: file-preferences-editor settings-select font 修改字体,改成能显示中文 ...

  6. 使用过滤器(Filter)解决请求参数中文乱码问题(复杂方式)

    前述:      在写这篇笔记之前,对笔记中的设计模式进行介绍:      本篇笔记中将要使用到的设计模式是:装饰(包装)设计模式           (1)装饰(包装)设计模式口诀:         ...

  7. java web项目get,post请求参数中文乱码解决

    [转载]原文地址:https://www.cnblogs.com/tom-plus/p/6392279.html 在开发过程中,有时候会碰到get,post请求参数中文乱码. 原因: Http请求传输 ...

  8. 解决get方法提交参数中文乱码问题:

    解决get方法提交参数中文乱码问题: 1找到你们的tomcat的目录 2在这个目录下面\tomcat61-32\tomcat61\conf 3找到server.xml ,用notepad打开(没有就下 ...

  9. 解决Fiddler查看Post参数中文乱码的问题

    解决Fiddler查看Post参数中文乱码的问题 解决方法: 1.win+R 2.打开注册表编辑器:输入regedit +回车+是 3.HKEY_CURRENT_USER\Software\Micro ...

随机推荐

  1. TopCoder SRM 722 Div1 Problem 600 DominoTiling(简单插头DP)

    题意  给定一个$12*12$的矩阵,每个元素是'.'或'X'.现在要求$1*2$的骨牌铺满整个矩阵, 'X'处不能放置骨牌.求方案数. 这道题其实和 Uva11270 是差不多的,就是加了一些条件. ...

  2. google搜索打不开?提供 国内几个给力的服务器

    http://203.208.46.145/ 这是北京的机器,快到飞起来. http://74.125.224.232/, 屡试不爽 用编辑器打开C:\WINDOWS\system32\drivers ...

  3. Longest Increasing Subsequence - LeetCode

    Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...

  4. delphi中如何将string类型的字符串数据转化成byte[]字节数组类型的数据

    var  S:String;  P:PChar;  B:array of Byte;begin  S:='Hello';  SetLength(B,Length(S)+1);  P:=PChar(S) ...

  5. apache mina框架

    http://blog.csdn.net/ljx8928358/article/details/7759024

  6. TypeError at /post/ render_to_response() got an unexpected keyword argument 'context_instance'

    Exception Type: TypeError at /post/ Exception Value: render_to_response() got an unexpected keyword ...

  7. (转)python request用法

    强烈推荐!requests官方文档已有了中文版,请见http://cn.python-requests.org/zh_CN/latest/ requests是python的一个HTTP客户端库,跟ur ...

  8. nodejs session 设计

    会话管理 { //保存会话 _data : {}, /** 会话基本操作 ***/ //查找会话 getSession : function(id){}, //创建会话 createSession : ...

  9. Java架构师与开发者提高效率的10个工具

    Java受到全球百万计开发者的追捧,已经演变为一门出色的编程语言.最终,这门语言随着技术的变化,不断的被改善以迎合变化的市场需求. 无论你是否拥有一家科技公司,软件已经成为几乎每一个企业不可或缺的一部 ...

  10. Win7如何修复开机画面

    将下面文件保存为"修复Win7开机画面.bat"双击运行即可   bcdedit /set {current} locale zh-CN