从 HttpURLConnection InputStream 获取不正确的输出

发布于 2024-12-15 00:57:52 字数 979 浏览 4 评论 0原文

      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

但实际输出应该是 -- G ÜNîTESı TOPLANTI SALONU

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

PS:该代码不是来自任何servlet。它不是一个java类。

      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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

少女的英雄梦 2024-12-22 00:57:52

这将使用系统默认的字符编码:

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

同样,

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

使用系统默认的编码几乎总是是一个坏主意,特别是对于网络而言。

想要在 POST 中使用哪种编码?您应该设置 Content-Type 标头来指定您使用的编码,显然还可以在对 OutputStreamWriter 的构造函数调用中指定它。同样,您应该使用响应的Content-Type来确定在InputStreamReader调用中指定哪种编码。

一般来说,正是这样的事情才值得使用更高级别的 HTTP 库,例如 Apache HttpClient 。那应该能够为您处理编码。

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.

风渺 2024-12-22 00:57:52

您正在尝试通过字符流读取器(InputStream Reader)读取字节流(InputStream)。这样做时你应该小心谨慎。您需要指定阅读器的字符集才能正确解释传入的字节。因此需要知道接收到的数据的字符集和编码,并使用相同的字符集构建InputStreamReader,以便正确解释数据。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文