今天开发时,遇到利用Java中HttpClient类以POST方式提交数据,目标收到后中文乱码问题。
请求端代码:
/**
* HttpClient提交参数
* @author sunyunfang@126.com
*/
public static void main(String[] args) throws IOException {
HttpClient client = new HttpClient();
client.getHostConfiguration().setHost("127.0.0.1", 8081, "http");
// 使用POST方式提交数据
HttpMethod method = getPostMethod();
client.executeMethod(method);
// 打印服务器返回的状态
System.out.println(method.getStatusLine());
// 打印结果页面
String response = new String(method.getResponseBodyAsString().getBytes("8859_1"));
// 打印返回的信息
System.out.println(response);
method.releaseConnection();
}
// 使用POST方式提交数据
private static HttpMethod getPostMethod() {
String url = "/PushServer/notification.do?action=sendOneMsg";
NameValuePair message = new NameValuePair("message", "消息内容。");
post.setRequestBody(new NameValuePair[]{message});
return post;
}
// 使用GET方式提交数据
private static HttpMethod getGetMethod() {
return new GetMethod("/PushServer/notification.do?action=sendOneMsg&message=abcd");
}
目标端代码:
/**
* 供MsgServer远程调用
* @param request
* @param response
* @return
* @throws Exception
* @author SunYunfang@126.com
*/
public ModelAndView sendOneMsg(HttpServletRequest request,
HttpServletResponse response) throws Exception {
String message = ServletRequestUtils.getStringParameter(request, "message");
}
这段代码执行后,目标能收到信息,但是中文乱码,也没有找到转码的方法。
经分析,原来使用 NameValuePair 加入的HTTP请求的参数最终都会转化为 RequestEntity 提交到HTTP服务器。接着在PostMethod的父类 EntityEnclosingMethod 中发现,只要重载getRequestCharSet()方法就能设置提交的编码(字符集)。
修正后:
/**
* HttpClient提交参数
* @author SunYunfang@126.com
*/
public static void main(String[] args) throws IOException {
HttpClient client = new HttpClient();
client.getHostConfiguration().setHost("127.0.0.1", 8081, "http");
// 使用POST方式提交数据
HttpMethod method = getPostMethod();
client.executeMethod(method);
// 打印服务器返回的状态
System.out.println(method.getStatusLine());
// 打印结果页面
String response = new String(method.getResponseBodyAsString().getBytes("8859_1"));
// 打印返回的信息
System.out.println(response);
method.releaseConnection();
}
// 使用POST方式提交数据
private HttpMethod getPostMethod() {
String url = "/PushServer/notification.do?action=sendOneMsg";
PostMethod post = new UTF8PostMethod(url);
NameValuePair message = new NameValuePair("message", "消息内容。");
post.setRequestBody(new NameValuePair[]{message});
return post;
}
//Inner class for UTF-8 support
public static class UTF8PostMethod extends PostMethod{
public UTF8PostMethod(String url){
super(url);
}
@Override
public String getRequestCharSet() {
//return super.getRequestCharSet();
return "UTF-8";
}
}
// 使用GET方式提交数据
private static HttpMethod getGetMethod() {
return new GetMethod("/PushServer/notification.do?action=sendOneMsg&message=abcd");
}
分享到:
相关推荐
采用 HttpClient GET、POST 方式 提交数据到服务器
HttpClient Post提交多文件及多个普通参数,已经封装成工具类。 需传入 要请求的url 普通参数map 例 map.put("param1","张三"); 需要传入的文件流map 其中key为文件名 服务端接收无乱码。
(完整版)JAVA利用HttpClient进行POST请求(HTTPS).doc
java HttpClient 发送GET请求和带有表单参数的POST请求教程例子
以httpclient方式提交数据(POST和GET),实现返回服务器数据。
java动态提交网站httpclient java动态提交网站httpclient
httpclient常用封装工具 doGet(String url, Map, String> param) doPost(String url, Map, String> param) doPostJson(String url, String json)
java httpclient 抓取 数据 和jar 包
用java实现httpclient请求的类
模拟登录 不提供用户名密码
使用c#实现的HttpClient拼接multipart/form-data形式参数post提交数据,包含图片内容,有需要的可以下载,希望能帮到有需要的人,
利用c# httpclient ,post json数据到web api
httpclient.java 工具类
利用httpclient-4.3.1.jar、httpcore-4.3.jar包,很简单的用java实现httpget和httppost请求。
httpclient post方式发送json格式数据,需要的相关包。
java httpclient 所需要的包,都是最新版的 commons-logging-1.1.3.jar commons-codec-1.6.jar commons-httpclient-3.1.jar
1.高效简单池化的HttpClient工具类,提供单元测试用列。 2.支持基于SpringBoot 2.1.x的自动装载模块,引用依赖即可使用。 3.公司几十个项目都使用该工具类访问第三方的Http/Https+json协议接口。 4.经过上市公司多个...
Java Httpclient的一个小例子,只有客户端的代码,服务器端随便写一个servlet都行,我也是新学的,主要是自己下载的jar包然后自己整合的
POST提交数据加密使用HttpWatch获取的数据是加密后的 用户名user1 密码:123456