0

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...

4
  • 1
    Can you show us your code? Commented Jul 23, 2013 at 4:08
  • does setting the charset to charset=utf-8 give you any joy? connection.setRequestProperty("charset", "utf-8"); Commented Jul 23, 2013 at 4:35
  • Nope :( that's how it was originally... Commented Jul 23, 2013 at 4:43
  • if it helps even the characters I send, that I manually code utf8: String urlParameters = "reply=1&message=" + trans + "%CE%BA%CE%B1, get changed into different characters :( Commented Jul 23, 2013 at 4:46

1 Answer 1

1

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"); 
Sign up to request clarification or add additional context in comments.

2 Comments

Unfortunately I do not have access to the receiver, they claim it reads UTF8 :( and in fact the characters they claim I send are part of the UTF8 :(
Even though it might seem to be at the server side, it could also be at my side, since the character %CE%BA becomes %C2%BA both utf8 but different hex-numbers, somewhere (maybe in eclipse) I am coding it wrong or something...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.