user2527849
user2527849

Reputation: 61

java or eclipse utf8 encoding not correct

I am trying to send some utf8 data with an http post using Java and Eclipse. No matter what I try the script sends the wrong utf8 codes! For example I try to send the greek character: "κ" which in utf8 is %CE%BA the other side receives %C2%BA which is "º". I've tried everything to no avail, searched all over to same result, can someone help me?

Here's the latest iteration of my code:

         //trans = URLEncoder.encode(trans,"UTF8");

    String urlParameters =  "reply=1&message=" + trans + "%CE%BA%CE%B1%CE%BB%CE%AD%CF%83%CF%84%CE%B5%20%CF%83%CF%84%CE%BF%20%CE%A0%CE%B1%CF%81%CE%AF%CF%83%CE%B9%CF%80%CE%BF%CE%B4%CE%B9%CE%B1";
    System.out.println(urlParameters);
    URL url = new URL("https://posttestserver.com/post.php");
    //URLConnection conn = url.openConnection();
    //conn.setDoOutput(true);


    //////////////////////////
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();           
    connection.setDoOutput(true);
    connection.setDoInput(true);
    connection.setInstanceFollowRedirects(false); 
    connection.setRequestMethod("POST"); 
    connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 
    //connection.setRequestProperty("charset", "ascii");
    connection.setRequestProperty("Content-Length", "" + Integer.toString(urlParameters.getBytes().length));
    connection.setUseCaches (false);



    DataOutputStream wr = new DataOutputStream(connection.getOutputStream ());
    wr.write(urlParameters.getBytes("UTF8"));
    wr.flush();
    wr.close();
    connection.disconnect();

I've tried encoding at many instances, hence commented out parts...

I have no idea what is going on :( if I do the following to my string:

        byte[] transc = trans.getBytes("UTF-8");


    for(int i=0;i < trans.length(); i=i+1){
    System.out.println(trans.codePointAt(i));
    System.out.println(transc[i]);
    }

I get the following output on the Eclipse console: 954 -50 945 -70 955 -50 941 -79 963 -50 964 -69 949 -50 32 -83 963 -49 964 -125 959 -49 32 -124 928 -50 945 -75 961 32 943 -49 963 -125 953 -49

The plot thickens! If I send my string as part of the BODY of a POST all is FINE. If I send it as parameters of the POST it gets messed up. I asked my client if they can change it to accept it in the body, but in case they don't... what is happening here??? Please help, this has been haunting me for 2 weeks now...

Upvotes: 0

Views: 361

Answers (1)

Paul Vargas
Paul Vargas

Reputation: 42020

I think that the problem is not the sender, it's the receiver. Which character set use the receiver for decode the parameters?

You can to know that with the simple line in the server:

System.getProperty("file.encoding");

Upvotes: 1

Related Questions