使用httpClient的HttpPost方法请求,请求头和请求体全部设置的和调试工具中的一样,请求体中包括中文和外面包着的标签(主要是对格式的定义),请求发送到服务器,然后存到数据库,然后再通过网页调用这些内容。我使用的namevaluepair,编码是gb2312,无论这么设置,打开网页后,中文全是问号。
但在这步之前,我还用了multipart发送post请求,那里面的中文就能正常显示。
请问大神是什么原因?谢谢!
但在这步之前,我还用了multipart发送post请求,那里面的中文就能正常显示。
请问大神是什么原因?谢谢!
解决方案 »
- SSH框架下运行tomcat出现异常“org.xml.sax.SAXParseException: Open quote is expected for at”
- 总是insert into错误
- 在线等啊,resin问题
- 四个问题各位大哥大姐帮帮忙快点回复急.
- 这个数据库连接有办法关闭吗?
- java程序如何直接运行jsp文件
- 菜鸟问题:为什么我的Tomcat5.5启动时,监听器会运行两次(如例),是高人一定要帮忙的?
- 急!!!关于window.open()的问题,帮帮忙吧~!
- jsp问题
- 线程实现快速遍历几个list取得所有组合
- weblogic12发布报错,tomcat能正常启动,急求解决方案
- SSM框架,搭建完启动失败...报错内容如下。。。
/*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;
}
我也看了一下原网页中的编辑器,使用的是fckeditor,但我觉得不管这个编辑器怎样,都应该以最后的post请求体为准啊。。
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);
<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>
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)));
......