English 中文(简体)
D. 寻找不适当的途径 HttpURLConnection InputStream
原标题:Getting improper Output from HttpURLConnection InputStream
      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);

产出一如下。

ÃœNÄ°TESÄ°TOPLANTI SALONU

但 实际产出本应是: ÜNITES 51 TOPLANTI SALONU。

谁能告诉我如何解决这一问题?

PS:该法典不来自任何保留。 它不是一个 j子。

最佳回答

这将利用系统违约特性编码:

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

同样,这将:

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

使用系统违约编码几乎是always一种坏想法,特别是在联网方面。

您的want是否用于《社会、文化权利国际公约》? 您应当确定内容提要人,以具体说明哪些因素使你能够使用,而且显然在施工人打电话到<代码>时也具体指明这一点。 产出StreamWriter。 同样,你应当利用response<>/em>的内容-类型来确定哪些内容在中指明。 投入StreamReader

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.

问题回答

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.





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...