httpclient发送post请求中文全是问号

java_bear 2018-08-20 01:27:11
使用httpClient的HttpPost方法请求,请求头和请求体全部设置的和调试工具中的一样,请求体中包括中文和外面包着的标签(主要是对格式的定义),请求发送到服务器,然后存到数据库,然后再通过网页调用这些内容。我使用的namevaluepair,编码是gb2312,无论这么设置,打开网页后,中文全是问号。
但在这步之前,我还用了multipart发送post请求,那里面的中文就能正常显示。
请问大神是什么原因?谢谢!
...全文
1588 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
java_bear 2018-08-27
  • 打赏
  • 举报
回复
谢谢各位,已经解决了,司死马当活马医,加了几个请求头就好了。请求的是weblogic,很早的系统。目前也没试到底是哪个头起作用。
k10509806 2018-08-27
  • 打赏
  • 举报
回复
应该没这么复杂的
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

象话 2018-08-27
  • 打赏
  • 举报
回复
// 使用忽略token验证的中台URL
HttpPost httpPost = new HttpPost(url);
try {
//设置请求头
httpPost.setHeader("Accept","application/json, text/plain, */*");
httpPost.setHeader("Cache-Control","no-cache");
httpPost.setHeader("Content-Type", "application/json;charset=utf-8");

// 在请求body中放入(JSON字符串)参数
httpPost.setEntity(new StringEntity(JSON.toJSONString(targets)));
......
黑白猿 2018-08-26
  • 打赏
  • 举报
回复 1
我原来也遇到过,部分示例代码如下,请参考。我使用NameValuePair封装的参数,并发送POST请求,注意代码中标记为蓝色部分。

// 首先Header部分需要设定字符集为:uft-8
HttpPost httpPost = new HttpPost(imUrl);
httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");

// 设置请求的参数
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("userId", userId));
nvps.add(new BasicNameValuePair("owner", owner));
// 此处也需要设定
httpPost.setEntity(new UrlEncodedFormEntity(nvps, "utf-8"));

// 执行请求
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpResponse response = httpClient.execute(httpPost);
yong_dan 2018-08-23
  • 打赏
  • 举报
回复
httpHeaders.setContentType(MediaType.APPLICATION_JSON_UTF8);试试
java_bear 2018-08-23
  • 打赏
  • 举报
回复
只是部分代码,怎么试都不行
java_bear 2018-08-23
  • 打赏
  • 举报
回复
这是部分代码,怎么试都不行
java_bear 2018-08-23
  • 打赏
  • 举报
回复
引用 7 楼 周yong的回复:
httpHeaders.setContentType(MediaType.APPLICATION_JSON_UTF8);试试
服务器的端接收数据是nameValuepair的。
java_bear 2018-08-22
  • 打赏
  • 举报
回复
还是不行。我用了所有方法,各种编码解码,甚至把开发者工具中原网页的post请求体直接拿过来,而且完全按照原网页的请求头进行设置,还是不行。 我也看了一下原网页中的编辑器,使用的是fckeditor,但我觉得不管这个编辑器怎样,都应该以最后的post请求体为准啊。。
天行归来 2018-08-20
  • 打赏
  • 举报
回复
我有封装了post方法,看下对你是否有用。


/*how to create NameValuePair: example
* List<NameValuePair> list = new ArrayList<NameValuePair>();
* list.add(new BasicNameValuePair("username", "admin"));
* @param encode can be "UTF-8" and so on
*/
public String httpPost(String url,List<NameValuePair> params,String encode){
String ret = "";

try {
HttpPost post = new HttpPost(url);
updateRequestHeader(post);

if (cTimeout>0 && rTimeout>0){
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(cTimeout).setSocketTimeout(rTimeout).build();
post.setConfig(requestConfig);
}

UrlEncodedFormEntity uefEntity = new UrlEncodedFormEntity(params,encode);
post.setEntity(uefEntity);
CloseableHttpResponse httpResponse = httpClient.execute(post);
if (httpResponse==null){
post.abort();
return null;
}

HttpEntity entity = httpResponse.getEntity();
if (entity!=null){
ret = EntityUtils.toString(entity,"UTF-8");
}
closeHttpResponse(httpResponse);
} catch (Exception e){
e.printStackTrace();
}
return ret;
}
ZLY_Smile 2018-08-20
  • 打赏
  • 举报
回复
new UrlEncodedFormEntity(params, UTF-8); 设置编码呢?
明天的地平线 2018-08-20
  • 打赏
  • 举报
回复
应该是编码格式问题,将请求回来的数据进行统一格式编码处理
java_bear 2018-08-20
  • 打赏
  • 举报
回复
实际上我做的东西,是用我写的网页代替原网页,因为原网页有很多不方便的地方。我是在servlet里用的httpclient,用的tomcat,但请求的服务器对我来说是未知。我觉得httpclient虽然在sevlet中,也是用的tomcat,但httpclient发送的请求应该不经过tomcat啊。我的表述可能有些混乱
鱿鱼ing 2018-08-20
  • 打赏
  • 举报
回复
tomcat编码格式看看

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧