我有一个方法从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
答案 0 :(得分:1)
看起来响应已压缩(gzip)。 尝试解压缩它:
...
InputStream is = response.getEntity().getContent();
Header contentEncoding = response.getFirstHeader("Content-Encoding");
if ((contentEncoding != null) && contentEncoding.getValue().equalsIgnoreCase("gzip")) {
InputStream gzipIs = new GZIPInputStream(is);
...