我有一个方法可以从 url 获取文件的源文本并将其作为字符串返回。但它返回的是乱码垃圾而不是任何有用的东西。
我在项目的一个地方使用相同的代码,它工作得很好,但在其他地方使用它会导致一团糟
代码:
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;
}
它返回什么:
������������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);
...
关于java - HttpClient/BufferedReader 返回乱码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28334771/