Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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?

share|improve this question
    
What do you pass as your content-type? What charset? –  fge Jun 10 '13 at 12:52
    
Which server are you using? –  maythesource.com Jun 10 '13 at 13:00
add comment

3 Answers

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");
share|improve this answer
    
Helpfull: stackoverflow.com/questions/5270591/… . Also, make sure your reading UTF-8 also on the server. –  maythesource.com Jun 10 '13 at 13:01
add comment

you should try encoding parameters

URLEncoder.encode(URL, "UTF-8");
share|improve this answer
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.