使用httpClient的HttpPost方法请求,请求头和请求体全部设置的和调试工具中的一样,请求体中包括中文和外面包着的标签(主要是对格式的定义),请求发送到服务器,然后存到数据库,然后再通过网页调用这些内容。我使用的namevaluepair,编码是gb2312,无论这么设置,打开网页后,中文全是问号。
但在这步之前,我还用了multipart发送post请求,那里面的中文就能正常显示。
请问大神是什么原因?谢谢!

解决方案 »

  1.   

       tomcat编码格式看看
      

  2.   

    实际上我做的东西,是用我写的网页代替原网页,因为原网页有很多不方便的地方。我是在servlet里用的httpclient,用的tomcat,但请求的服务器对我来说是未知。我觉得httpclient虽然在sevlet中,也是用的tomcat,但httpclient发送的请求应该不经过tomcat啊。我的表述可能有些混乱
      

  3.   

    new UrlEncodedFormEntity(params, UTF-8); 设置编码呢?  
      

  4.   

    我有封装了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;
    }
      

  5.   

    还是不行。我用了所有方法,各种编码解码,甚至把开发者工具中原网页的post请求体直接拿过来,而且完全按照原网页的请求头进行设置,还是不行。
    我也看了一下原网页中的编辑器,使用的是fckeditor,但我觉得不管这个编辑器怎样,都应该以最后的post请求体为准啊。。
      

  6.   

      httpHeaders.setContentType(MediaType.APPLICATION_JSON_UTF8);试试
      

  7.   

    服务器的端接收数据是nameValuepair的。
      

  8.   

    我原来也遇到过,部分示例代码如下,请参考。我使用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);
      

  9.   

    应该没这么复杂的
    <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>
      

  10.   

    // 使用忽略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)));
    ......
      

  11.   

    谢谢各位,已经解决了,司死马当活马医,加了几个请求头就好了。请求的是weblogic,很早的系统。目前也没试到底是哪个头起作用。