从 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
但实际输出应该是 -- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这将使用系统默认的字符编码:
同样,
使用系统默认的编码几乎总是是一个坏主意,特别是对于网络而言。
您想要在 POST 中使用哪种编码?您应该设置 Content-Type 标头来指定您使用的编码,显然还可以在对
OutputStreamWriter
的构造函数调用中指定它。同样,您应该使用响应的Content-Type来确定在InputStreamReader
调用中指定哪种编码。一般来说,正是这样的事情才值得使用更高级别的 HTTP 库,例如 Apache HttpClient 。那应该能够为您处理编码。
This will use the system default character encoding:
Likewise so will this:
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 theInputStreamReader
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.
您正在尝试通过字符流读取器(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.