1
      URL url = new URL("http://soandso.com");
      String userpassword = username + ":" + password;
      conn = (HttpURLConnection)url.openConnection();
      conn.setDoOutput(true);         
      conn.setRequestMethod("POST");         
      BASE64Encoder enc = new sun.misc.BASE64Encoder();          
      String encodedAuthorization = enc.encode( userpassword.getBytes() );
      conn.setRequestProperty("Authorization", "Basic "+encodedAuthorization);
      OutputStreamWriter writer =new OutputStreamWriter(conn.getOutputStream());
      writer.write("ABC");
      writer.flush ();
      writer.close();
      BufferedReader rd =new BufferedReader(new InputStreamReader(conn.getInputStream()));
      while ((inputLine = rd.readLine()) != null)
      System.out.println(inputLine);

The Output I got follows.

ÜNİTESİ TOPLANTI SALONU

But The actual output is supposed to be -- G ÜNİTESİ TOPLANTI SALONU

Can anyone tell me how to fix this?

PS: The code is not from any servlet. Its not a java class.

2 Answers 2

2

This will use the system default character encoding:

OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream());

Likewise so will this:

BufferedReader rd = new BufferedReader(
    new InputStreamReader(conn.getInputStream()));

Using the system default encoding is almost always a bad idea, particularly for networking.

Which encoding did you want to use for the POST? You should set the Content-Type header to specify which encoding you use, and obviously also specify it in the constructor call to OutputStreamWriter. Likewise you should use the Content-Type of the response to determine which encoding to specify in the InputStreamReader call.

Generally speaking, it's things like this that make it worth using a higher-level HTTP library such as Apache HttpClient. That should be able to handle the encoding for you.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, Jon. Wondering which Content-Type will help me in this.
@harish.raj: It would be in the charset part, e.g. Content-Type: text/plain; charset=utf-8. See w3.org/International/O-charset
This sounds like a HTML code. Sorry. how to fix this in InputStream?
1

You are trying to read a byte stream (InputStream) through a character stream reader (InputStream Reader). You should be cautious while doing this. You need to specify the charset for the reader to interpret the incoming bytes correctly. So need to know the charset and encoding of the data being received and build the InputStreamReader with the same charset so the data is interpretted properly.

3 Comments

Vikas, Can you recognize which charset I should use to fix this? I tried, UTF-8 and ISO-8859-1. But no luck!
It could be ISO-8859-3, but most likely the encoding will be provided in the request headers and otherwise you will really need to ask the owner of the server you are querying.
You can read the character encoding from the HTTP response got from the server. You can then appropriately contruct your Reader.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.