javacharacter-encodinghttprequest

How to change the charset in a HTTP Response in Java?


I'm sending a HTTP GET request with java. See code below. When I parse the response, the german "Umlaute" (e.g. ß,ä,ü) are just rubbish characters. I know, it's something about charset, but what exactly do I have to do ?

This is part of the code:

URL obj = new URL( urlAsString );
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
on.setRequestMethod( "GET" );

Do I have to set a special request property or something like that ? Or do I have to convert the result somehow ?

When I enter the "urlAsString" in a web browsers address line, I get a perfect result and all "Umlaute" are correct.

thanks for any hint! Thorsten


Solution

  • You have to set the charset in the response part, not in the request part such as

    Content-Type: text/html; charset=utf-8
    

    This part have to be correct as you said that it is ok when you test from the browser.

    Now from the client part, that is in your java program you also have to set the header Accept-Charset that indicates to the server the charset that you can accept. For example for utf-8 then iso-8859-1 you can add this header :

    Accept-Charset: utf-8, iso-8859-1;q=0.5
    

    If it doesn't work, you could also try to read the bytes from the connection by specifying the charset such as :

    DataOutputStream ds = new DataOutputStream(con.getOutputStream());
    BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(ds, "UTF-8"));
    

    Maybe that the problem is there but you don't show this part in the posted code.