elainetmd
2012-07-04 10:15
浏览 2.8k
已采纳

报错java.net.SocketException: Software caused connection abort: recv failed 怎么办??

写道
在利用google搜索抓取结果页面的时候总抛出异常:
java.net.SocketException: Software caused connection abort: recv failed
 at java.net.SocketInputStream.socketRead0(Native Method)
 at java.net.SocketInputStream.read(SocketInputStream.java:147)
 at java.io.BufferedInputStream.fill(BufferedInputStream.java:218)
 at java.io.BufferedInputStream.read1(BufferedInputStream.java:258)
 at java.io.BufferedInputStream.read(BufferedInputStream.java:317)
 at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:697)
 at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:640)
 at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:662)
 at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1195)
 at AttrExtractor.search(AttrExtractor.java:96)
 at AttrExtractor.main(AttrExtractor.java:56)
错误的地方说是在BufferedReader reader=new BufferedReader(new InputStreamReader(connection.getInputStream()));
我是个菜鸟,不知道为什么报错,有没有大牛可以详细告诉我怎么办?!!!

url = new URL("http://www.google.com.hk/search?hl=en&q="+ss+"&ie=utf-8&oe=utf-8&start=0");
HttpURLConnection connection=(HttpURLConnection) url.openConnection();
connection.setConnectTimeout(12000);
connection.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)");
connection.setRequestProperty("Accept", " images/gif, image/x-xbitmap, image/jpeg ,*/*");
connection.setRequestProperty("Accept-Encoding", "deflate");
connection.setRequestProperty("Accept-Charset", "null");
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setDoInput(true);
connection.setInstanceFollowRedirects(false); ///防止Server redirected too many times
connection.setRequestMethod("GET");
connection.connect();

BufferedReader reader=new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line=null;
String s = "";
StringBuffer sb = new StringBuffer("");
//读取页面内容
while ((s = reader.readLine()) != null) {
sb.append(s+"\r\n");
}

 

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

相关推荐 更多相似问题