关于httpclient和inputstream的问题

raymondchan 2005-12-28 08:30:57
HttpClient httpClient = new HttpClient();
GetMethod getMethod = new GetMethod(uri);
InputStream responseBody = getMethod.getResponseBodyAsStream();
byte[] b= new byte[1024];
while (responseBody.read(b) >= 0) {
System.out.println(new String(b));
}

这段程序能把对应网页的代码打出来,但最后会少一小段,为什么?怎么才能把它完整输出?
...全文
613 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
剑事 2005-12-29
  • 打赏
  • 举报
回复
http://www.blogjava.net/jfy3d/archive/2005/05/11/4173.html
用这个吧 省了加第三方包了
universe01 2005-12-28
  • 打赏
  • 举报
回复
学习!
raymondchan 2005-12-28
  • 打赏
  • 举报
回复
不想用getMethod.getResponseBody(),网页太大的话会警告
funcreal 2005-12-28
  • 打赏
  • 举报
回复
BufferedReader in = new BufferedReader(new InputStreamReader(responseBody));
在线手工写难免会出错。
funcreal 2005-12-28
  • 打赏
  • 举报
回复
String responseBody = new String(getMethod.getResponseBody());是不是直接就得到了?
刚在另一个帖子上看见的。说实话没用过httpclient.
raymondchan 2005-12-28
  • 打赏
  • 举报
回复
BufferedReader in = new BufferedReader(responseBody);
这句不行的吧,没有这种构造方法的
funcreal 2005-12-28
  • 打赏
  • 举报
回复
不要用字节来处理,用
BufferedReader in = new BufferedReader(responseBody);//把字节流包装成字符
String line=null;
while((line=in.readLine())!=null){
out.println(line);
}

处理字符应该用Reader,Writer,处理字节的时候才用InputStream和OutputStream

62,614

社区成员

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

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