gpt4 book ai didi

Java 下载 html

转载 作者:行者123 更新时间:2023-12-01 14:24:06 30 4
gpt4 key购买 nike

我正在尝试下载网站的 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;
}

最佳答案

我建议使用EntityUtils :

HttpEntity entity = response.getEntity();
String html = EntityUtils.toString(entity);

HttpEntity entity = response.getEntity();
String html = EntityUtils.toString(entity, encoding);

关于Java 下载 html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17300266/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com