Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send UTF-8 chars via HttpURLConnection failing

I have spent half Sunday on this now I need help:

I want to send a String including special chars UTF-8 encoded to a server using Java HttpURLConnection. The correct encoding of the chars fails.

Example:

strToSend: ä ù €
strUrlEncoded: %C3%A4+%C3%B9+%E2%82%AC
strReceived:ä ù â¬

My code:

        urlConnection = (HttpURLConnection) new URL("http://localhost:8080/NetworkingServer/ServerServlet").openConnection();
        urlConnection.setUseCaches(false);
        urlConnection.setDoOutput(true); // Triggers POST.
        urlConnection.setRequestProperty("accept-charset", "UTF-8");
        urlConnection.setRequestProperty("content-type", "application/x-www-form-urlencoded");

        String strToSend = "ä ù €";
        System.out.println("strToSend: " + strToSend);
        String strUrlEncoded = URLEncoder.encode(strToSend, "UTF-8");
        System.out.println("strUrlEncoded: " + strUrlEncoded);

        OutputStreamWriter writer = new OutputStreamWriter(urlConnection.getOutputStream(), "UTF-8");
        writer.write(String.format("content=%s", strUrlEncoded));
        writer.close();

Any ideas?

like image 543
Martin Ho Avatar asked Apr 07 '13 17:04

Martin Ho


People also ask

What is the difference between URLConnection and HttpURLConnection?

URLConnection is the base class. HttpURLConnection is a derived class which you can use when you need the extra API and you are dealing with HTTP or HTTPS only. HttpsURLConnection is a 'more derived' class which you can use when you need the 'more extra' API and you are dealing with HTTPS only.

What is HttpURLConnection in Java?

public abstract class HttpURLConnection extends URLConnection. A URLConnection with support for HTTP-specific features. See the spec for details. Each HttpURLConnection instance is used to make a single request but the underlying network connection to the HTTP server may be transparently shared by other instances.

How do I encode a string in UTF-8?

In order to convert a String into UTF-8, we use the getBytes() method in Java. The getBytes() method encodes a String into a sequence of bytes and returns a byte array. where charsetName is the specific charset by which the String is encoded into an array of bytes.

Why UTF-8 is used in Java?

UTF-8 has the ability to be as condensed as ASCII but can also contain any Unicode characters with some increase in the size of the file. UTF stands for Unicode Transformation Format. The '8' signifies that it allocates 8-bit blocks to denote a character.


1 Answers

You need to set the encoding in your Content-Type header.

Set it to "application/x-www-form-urlencoded; charset=utf-8". Currently you only set the accept-charset - this tells the server what to send back.

like image 179
Boris the Spider Avatar answered Sep 22 '22 00:09

Boris the Spider