3

如何将UTF编码设置为Http Post请求?

这是我的代码片段。

HttpClient httpClient = getNewHttpClient();

    String user_agent = (String) httpClient.getParams().getParameter(
            CoreProtocolPNames.USER_AGENT)
            + " Android";
    httpClient.getParams().setParameter(CoreProtocolPNames.USER_AGENT,
            user_agent);
    httpClient.getParams().setBooleanParameter(
            "http.protocol.expect-continue", false);
    HttpResponse response;
    HttpPost post = new HttpPost(remoteUrl);
    System.out.println("Url:" + remoteUrl + "  values:"
            + nameValuePair.toString());
    post.setEntity(new UrlEncodedFormEntity(nameValuePair));


    response = httpClient.execute(post);
    int postStatus = response.getStatusLine().getStatusCode();
    String result = EntityUtils.toString(response.getEntity());
    System.out.println("result:" + result);
4

1 回答 1

2

您可以通过调用设置任何标题 post.setHeader("HEADER NAME","HEADER VALUE");

在你的情况下,它应该是

post.setHeader("Accept-Charset","utf-8");

于 2013-09-26T14:34:35.950 回答