HttpClient/BufferedReaderreturns乱码乱码
HttpClient/BufferedReader returns garbled mess
我有一种方法可以从 url 和 return 中获取文件的源文本作为字符串。但它 return 是乱码,没有任何用处。
我在我的项目中的一个地方使用了相同的代码并且它工作得很好,但是在其他地方使用它return一团糟
代码:
private static String getWebSource(String Url) throws IOException {
HttpClient httpclient = new DefaultHttpClient(); // Create HTTP Client
HttpGet httpget = new HttpGet(Url); // Set the action you want to do
HttpResponse response = httpclient.execute(httpget); // Executeit
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent(); // Create an InputStream with the response
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) // Read line by line
sb.append(line + "\n");
String resString = sb.toString(); // Result is here
return resString;
}
什么 returns:
������������mRËnÛ0<Û@þaÃCÑ¡hÅ=4¤CíM¦A£Àí×TIúÕ¯/õpìCt ÃÝá.ËÅyþûé*_+xzùüp?B[Nç-òüú8@n¹vÒK£¹bìî��©¼ofív»h7-Yþí[®¸-BêÏ*#áÉÆI'¸¯vé4ñÍÍM_MÚ¤âºL j¯Q6%¬6R ÷®@A½ 4»_%ä"Ãð%5z"Å¿¹MÉÜhÚÓüÐ ¢ß¥ÄãÞ³ÖÁ-·}ú¡;öNóS"°0{cÏXÐZcͳôC½éo®9W
响应似乎已压缩 (gzip)。
尝试解压缩它:
...
InputStream is = response.getEntity().getContent();
Header contentEncoding = response.getFirstHeader("Content-Encoding");
if ((contentEncoding != null) && contentEncoding.getValue().equalsIgnoreCase("gzip")) {
InputStream gzipIs = new GZIPInputStream(is);
...
我有一种方法可以从 url 和 return 中获取文件的源文本作为字符串。但它 return 是乱码,没有任何用处。
我在我的项目中的一个地方使用了相同的代码并且它工作得很好,但是在其他地方使用它return一团糟
代码:
private static String getWebSource(String Url) throws IOException {
HttpClient httpclient = new DefaultHttpClient(); // Create HTTP Client
HttpGet httpget = new HttpGet(Url); // Set the action you want to do
HttpResponse response = httpclient.execute(httpget); // Executeit
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent(); // Create an InputStream with the response
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) // Read line by line
sb.append(line + "\n");
String resString = sb.toString(); // Result is here
return resString;
}
什么 returns:
������������mRËnÛ0<Û@þaÃCÑ¡hÅ=4¤CíM¦A£Àí×TIúÕ¯/õpìCt ÃÝá.ËÅyþûé*_+xzùüp?B[Nç-òüú8@n¹vÒK£¹bìî��©¼ofív»h7-Yþí[®¸-BêÏ*#áÉÆI'¸¯vé4ñÍÍM_MÚ¤âºL j¯Q6%¬6R ÷®@A½ 4»_%ä"Ãð%5z"Å¿¹MÉÜhÚÓüÐ ¢ß¥ÄãÞ³ÖÁ-·}ú¡;öNóS"°0{cÏXÐZcͳôC½éo®9W
响应似乎已压缩 (gzip)。 尝试解压缩它:
...
InputStream is = response.getEntity().getContent();
Header contentEncoding = response.getFirstHeader("Content-Encoding");
if ((contentEncoding != null) && contentEncoding.getValue().equalsIgnoreCase("gzip")) {
InputStream gzipIs = new GZIPInputStream(is);
...