How to encode russian text in Post request using A

2020-06-16 08:52发布

There is the following Java code:

    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();
}

As you can see above, I send POST request and get response. But in register method I use russian name (cyrillic), and there is "????? ???" on my server. How can I fix it? How can I encode russian text?

6条回答
家丑人穷心不美
2楼-- · 2020-06-16 09:24

You need to set your request encoding to 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

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

An example of how this is accomplished:

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

Additionally, I see your using UrlEncodedFormEntity. You should add encoding to the constructor as so:

new UrlEncodedFormEntity(nameValuePairs,"UTF-8");
查看更多
狗以群分
3楼-- · 2020-06-16 09:26

If you tired of searching for correct headers / encoding combinations, you can encode string to URI format and decode back, this will preserve any non-ascii chars

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");
查看更多
等我变得足够好
4楼-- · 2020-06-16 09:35

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

查看更多
▲ chillily
5楼-- · 2020-06-16 09:36

you should try encoding parameters

URLEncoder.encode(URL, "UTF-8");
查看更多
劳资没心,怎么记你
6楼-- · 2020-06-16 09:37

If you post as json:

  1. Create client
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. Create HttpPost
HttpPost post = new HttpPost("/hostIp:port/forexample/saveMyObject");
  1. Write object as string (String)
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. execute
CloseableHttpResponse response = httpClient.execute(post);
查看更多
Emotional °昔
7楼-- · 2020-06-16 09:42

Perhaps you are reading or writing the response wrong?

Make sure you use the same encoding in both writing the request and reading it and in the post http headers.

To define encoding for read data use InputStreamReader(InputStream, Charset) constructor.

查看更多
登录 后发表回答