专注收集记录技术开发学习笔记、技术难点、解决方案
网站信息搜索 >> 请输入关键词:
您当前的位置: 首页 > Java Web开发

运用HttpURLConnection抓取google搜索结果,总是报错:java.net.SocketException: Connection reset

发布时间:2010-05-31 00:47:00 文章来源:www.iduyao.cn 采编人员:星星草
使用HttpURLConnection抓取google搜索结果,总是报错:java.net.SocketException: Connection reset
抓取百度或者其他网站数据没问题,可是谷歌的总是报错:java.net.SocketException: Connection reset


抓取代码如下:
  HttpURLConnection connection = null;
BufferedInputStream in = null;
BufferedReader read = null;

try {
URL url = new URL(strURL); 

String cookie = "";
int iii=0;
do {
iii++;
connection = (HttpURLConnection)url.openConnection();
System.out.println("11111111:"+strURL);
Thread.sleep(5000);
if(cookie.length() != 0) {
connection.setRequestProperty("Cookie", cookie);
}

connection.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0)");
connection.setInstanceFollowRedirects(false);
System.out.println("9999999999"+connection.getHeaderField("Set-Cookie"));
int code = connection.getResponseCode();

if(code == HttpURLConnection.HTTP_MOVED_TEMP) {
cookie += connection.getHeaderField("Set-Cookie") + ";";
}

if((connection.getResponseCode() == HttpURLConnection.HTTP_OK) || iii==4)
break;

}
while(true);

in = new BufferedInputStream(connection.getInputStream());
read = new BufferedReader(new InputStreamReader(in,"GB2312"));

------解决方案--------------------
连接重置,意思是说你的链接设置非法。可能谷歌有特别的设置吧。
------解决方案--------------------
貌似要申请一个谷歌的一个账号吧 不然就会有这个问题
------解决方案--------------------
google是做Web服务起家的,你想用程序获取人家服务的结果,google能让你这么轻易拿劳动成果吗?

google map,google translate,google doc...几乎所有的google产品你都可以集成到你自己的web应用中,但是:要给钱!!!!

记得以前集成google map的地图搜索的时候,社区版还是免费的,但是一天内只能搜索20次....
------解决方案--------------------
不是吧。是不是你有代理没配什么的。我自己写的抓取的小例子,怎么抓都行啊。。。
友情提示:
信息收集于互联网,如果您发现错误或造成侵权,请及时通知本站更正或删除,具体联系方式见页面底部联系我们,谢谢。

其他相似内容:

热门推荐: