为了账号安全,请及时绑定邮箱和手机立即绑定

报错java.net.SocketException: Software caused

报错java.net.SocketException: Software caused

小怪兽爱吃肉 2019-03-14 14:15:35
写道在利用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 回答

?
慕仙森

TA贡献1827条经验 获得超7个赞

出现这个情况一般是客户端那边写完流后,就立即关闭了socket。服务器端这边还没读完,所以就报错了,你可以让客户端那边写完对象后,等服务器端回一个状态给客户端。客户端再关闭流。


查看完整回答
反对 回复 2019-04-26
  • 4 回答
  • 0 关注
  • 789 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信