我正在尝试下载网站的 html:
String encoding = "UTF-8";
HttpContext localContext = new BasicHttpContext();
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet(MYURL);
httpget.setHeader("User-Agent", "Mozilla/5.0 (iPhone; CPU iPhone OS 5_0 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9A334 Safari/7534.48.3");
HttpResponse response = httpclient.execute(httpget, localContext);
HttpEntity entity = response.getEntity();
InputStream instream = entity.getContent();
String html = getStringFromInputStream(encoding, instream);
在 html 字符串的 and 中,我得到:
...
21912
0
0
而且我没有得到完整的html,知道如何解决吗?
编辑
private static String getStringFromInputStream(String encoding, InputStream instream) throws UnsupportedEncodingException, IOException {
Writer writer = new StringWriter();
char[] buffer = new char[1024];
try {
Reader reader = new BufferedReader(new InputStreamReader(instream, encoding));
int n;
while ((n = reader.read(buffer)) != -1) {
writer.write(buffer, 0, n);
}
} finally {
instream.close();
}
String result = writer.toString();
return result;
}