报错java.net.SocketException: Software caused

写道

在利用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"); 
}

 


小怪兽爱吃肉
浏览 790回答 4
4回答

慕仙森

出现这个情况一般是客户端那边写完流后,就立即关闭了socket。服务器端这边还没读完,所以就报错了,你可以让客户端那边写完对象后,等服务器端回一个状态给客户端。客户端再关闭流。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java