7

有以下Java代码:

    public static void register(UserInfo info) throws ClientProtocolException, IOException, JSONException, RegistrationException {
        List<NameValuePair> params=new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("name", info.getName()));
        params.add(new BasicNameValuePair("email", info.getEmail()));
        params.add(new BasicNameValuePair("pass", info.getPassword()));
        params.add(new BasicNameValuePair("genus", String.valueOf(info.getGenus())));
        String response=doPostRequest(params, REGISTRATION_URL);
    }

private static String doPostRequest(List<NameValuePair> params, String url) throws ClientProtocolException, IOException {
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost(url);

    httppost.setEntity(new UrlEncodedFormEntity(params));
    HttpResponse response = httpclient.execute(httppost); 

    return getContentFromInputStream(response.getEntity().getContent());
} 

private static String getContentFromInputStream(InputStream is) throws IOException {
    String line;
    StringBuilder sb=new StringBuilder();
    BufferedReader reader=new BufferedReader(new InputStreamReader(is));
    while((line=reader.readLine())!=null) {
        sb.append(line);
    }
    reader.close();
    return sb.toString();
}

正如您在上面看到的,我发送 POST 请求并获得响应。但是在注册方法中,我使用俄语名称(西里尔字母),并且有“??????? ???” 在我的服务器上。我该如何解决?如何编码俄语文本?

4

6 回答 6

11

您需要将请求编码设置为 UTF-8。

The request or response body can be any encoding, but by default is ISO-8859-1. The encoding may be specified in the Content-Type header, for example:
Content-Type: text/html; charset=UTF-8

来自:http ://hc.apache.org/httpclient-3.x/charencodings.html

这是如何完成的一个例子:

HttpClient httpclient = new HttpClient();
httpclient.getParams().setParameter("http.protocol.version", HttpVersion.HTTP_1_1);
httpclient.getParams().setParameter("http.protocol.content-charset", "UTF-8");

此外,我看到您使用UrlEncodedFormEntity. 您应该像这样向构造函数添加编码:

new UrlEncodedFormEntity(nameValuePairs,"UTF-8");
于 2013-06-10T12:53:22.627 回答
3

你应该尝试编码参数

URLEncoder.encode(URL, "UTF-8");
于 2013-06-10T12:53:25.373 回答
0

也许您正在阅读或编写错误的响应?

确保在写入请求读取请求以及发布 http headers时使用相同的编码。

要定义读取数据的编码,请使用InputStreamReader(InputStream, Charset)构造函数。

于 2013-06-10T12:53:14.357 回答
0

如果您厌倦了搜索正确的标头/编码组合,您可以将字符串编码为 URI 格式并解码回来,这将保留任何非 ascii 字符

String cyrillicString = "ыыыыыы";

//encode string into URI format
String transportString = URLEncoder.encode(cyrillicString, "UTF-8");

//transport string somewhere 

//decode back
String decodedString = URLDecoder.decode(transportString, "UTF-8");
于 2017-04-16T22:43:41.090 回答
0

如果您以 json 格式发布:

  1. 创建客户端
HttpClientBuilder clientBuilder = HttpClients.custom();
//add header with charset UTF-8
clientBuilder.setDefaultHeaders(Arrays.asList(new BasicHeader(HttpHeaders.CONTENT_TYPE, ContentType.APPLICATION_JSON.getMimeType()))); //application/json;charset=UTF-8
CloseableHttpClient httpClient = clientBuilder.build();
  1. 创建 HttpPost
HttpPost post = new HttpPost("/hostIp:port/forexample/saveMyObject");
  1. 将对象写为字符串(字符串)
PostRequestExample request = new PostRequestExample();
request.setUsername("userName");
request.setUserAge(98));

ObjectMapper objectMapper = new ObjectMapper(); //import com.fasterxml.jackson.databind
//ContentType.APPLICATION_JSON - encode russian symbols as UTF8
post.setEntity(new StringEntity(objectMapper.writeValueAsString(request), ContentType.APPLICATION_JSON));
  1. 执行
CloseableHttpResponse response = httpClient.execute(post);
于 2020-04-29T06:30:29.463 回答
0

httpclient.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");

于 2015-12-01T12:45:11.770 回答