Apache HttpClient ( http://jakarta.apache.org/commons/httpclient/ ) 是一个纯 Java 的HTTP 协议的客户端编程工具包, 对 HTTP 协议的支持相当全面, 更多细节也可以参考IBM 网站上的这篇文章 HttpClient入门 (http://www.ibm.com/developerworks/cn/opensource/os-httpclient/ ).
不过在实际使用中, 还是发现按照最基本的方式调用 HttpClient 时, 并不支持 UTF-8 编码。
现在给出解决方案:
1可以在调用PostMethod方法时设置字符编码
PostMethod post=new PostMethod("/capaa/dialip/run.public");
post.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET,"utf-8");
NameValuePair userName = new NameValuePair( "userName" , "lwj" );
NameValuePair passWord = new NameValuePair( "passWord" , "123456" );
NameValuePair appName = new NameValuePair( "appName" , "PLSQL" );
NameValuePair validTotalCount = new NameValuePair( "validTotalCount" , "永不过期" );
post.setRequestBody( new NameValuePair[] { userName,passWord,appName,validTotalCount});
return post;
2在服务器端做如下的设置
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html");
response.setCharacterEncoding("UTF-8");