简体   繁体   English

从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);

The Output I got follows. 我得到的输出如下。

ÃœNÄ°TESÄ° TOPLANTI SALONU ÃœNÄ°TESÄ°TOPLANTI SALONU

But The actual output is supposed to be -- G ÜNİTESİ TOPLANTI SALONU 但是实际输出应该是-GÜNİTESİTOPLANTI SALONU

Can anyone tell me how to fix this? 谁能告诉我如何解决这个问题?

PS: The code is not from any servlet. PS:该代码不是来自任何servlet。 Its not a java class. 它不是一个java类。

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? 希望将哪种编码用于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 . 您应该设置Content-Type标头以指定要使用的编码,并且显然还要在对OutputStreamWriter的构造函数调用中指定它。 Likewise you should use the Content-Type of the response to determine which encoding to specify in the InputStreamReader call. 同样,您应该使用响应的Content-Type来确定在InputStreamReader调用中指定的编码。

Generally speaking, it's things like this that make it worth using a higher-level HTTP library such as Apache HttpClient . 一般来说,这样的事情使得值得使用更高级别的HTTP库,如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). 您正尝试通过字符流阅读器(InputStream Reader)读取字节流(InputStream)。 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. 因此需要知道正在接收的数据的字符集和编码,并使用相同的字符集构建InputStreamReader,以便正确解释数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM