如何抓取网络数据

muyebo 2009-07-14 11:06:39
            url = new URL("http://www.sina.com.cn"); 
URLConnection urlConn = url.openConnection();
DataInputStream din = new DataInputStream(urlConn.getInputStream());
byte[] buf = new byte[1024];
int length = din.available();
System.out.println("######" + length + "######");
int readlength = 0;
while (true) {
readlength += din.read(buf);
System.out.print(new String(buf));
if (readlength >= length) {
break;
}
}
System.out.println("\n######" + readlength + "######");

如何抓取查看是编译后的html代码
相当于C#的WebResponse
...全文
111 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
muyebo 2009-07-14
  • 打赏
  • 举报
回复
在二楼的提醒下,又重新看了一遍一楼,确实可以.
感谢
Ant 2009-07-14
  • 打赏
  • 举报
回复
HttpURLConnection.HTTP_OK
flyxxxxx 2009-07-14
  • 打赏
  • 举报
回复
URL url = new URL("http://www.sina.com.cn");
HttpURLConnection urlConn = (HttpURLConnection )url.openConnection();
if(urlConn.getResponseCode()==HttpURLConnection.HTTP_OK){//这表示成功返回
InputStream is=urlConn.getInputStream();//输入流,从这里读取数据就行了

}

62,615

社区成员

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

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