httpclient 获取网页脚本 缺失内容

wangbh11 2011-10-13 06:26:17
如题 原因
...全文
55 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
wangbh11 2011-10-13
  • 打赏
  • 举报
回复
是这样的 <script>.........</script> 跟了一个table 然后 那个table 整个消失不见了
24K純帥 2011-10-13
  • 打赏
  • 举报
回复
我比较好奇的是怎么少了一部分的,难道传输的时候丢包了不成啊
wangbh11 2011-10-13
  • 打赏
  • 举报
回复
谢谢 解决了大部分问题 但是还有一点问题 就是 <script>......</script> 后的一部分html也消失了 不知道原因所在 我是通过比较chrome 的page source 和 httpclient 获取的html 比较时候发现的
pywepe 2011-10-13
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 wangbh11 的回复:]

public String GetWebsiteContent(String URL, String charset) {
String website_content;
StringBuffer StrBuf = new StringBuffer();

HttpClient httpclient = new DefaultHttpClient();
httpclient.getPa……
[/Quote]

请使用EntityUtils.toString 它有处理编码问题
wangbh11 2011-10-13
  • 打赏
  • 举报
回复
public String GetWebsiteContent(String URL, String charset) {
String website_content;
StringBuffer StrBuf = new StringBuffer();

HttpClient httpclient = new DefaultHttpClient();
httpclient.getParams().setParameter(CoreProtocolPNames.WAIT_FOR_CONTINUE, 10000 );
HttpGet httpget = new HttpGet(URL);
try {
HttpResponse response = httpclient.execute(httpget);
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
HttpEntity entity = response.getEntity();
if (entity != null) {
BufferedReader reader = new BufferedReader(new InputStreamReader(entity.getContent(), charset));
int l;
char[] cbuf = new char[2048];
while ((l = reader.read(cbuf)) != -1) {
String str = new String(cbuf);
StrBuf.append(str);
}

}
}
website_content = StrBuf.toString();

} catch (Exception e) {
System.out.println("open url error!!");
website_content = "";
}

return website_content;
}
shine333 2011-10-13
  • 打赏
  • 举报
回复
你代码怎么写的我都不知道,当然没法分析了。
wangbh11 2011-10-13
  • 打赏
  • 举报
回复
如何避免 能否列举一些解决措施 谢谢
shine333 2011-10-13
  • 打赏
  • 举报
回复
原因太多,over

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧