2 yoshiontwo YoshionTwo 于 2015.07.14 15:15 提问

http协议系统之间交互,出现的编程的报错。我该怎样修改?求大神啊

现在我们系统跟gps之间交互,我采用的httpClient,可是每次跑程序的时候都会报错

 java.net.SocketException: Software caused connection abort: recv failed
    at java.net.SocketInputStream.socketRead0(Native Method)
    at java.net.SocketInputStream.read(Unknown Source)
    at org.apache.http.impl.io.SessionInputBufferImpl.streamRead(SessionInputBufferImpl.java:139)
    at org.apache.http.impl.io.SessionInputBufferImpl.fillBuffer(SessionInputBufferImpl.java:155)
    at org.apache.http.impl.io.SessionInputBufferImpl.readLine(SessionInputBufferImpl.java:284)
    at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:140)

我的实现的代码

 public String sendMessage(String xmlMessage)throws Exception{

         // 创建默认的httpClient实例.     
               CloseableHttpClient httpclient = HttpClients.createDefault();  
               // 创建httppost     
                HttpPost httppost = new HttpPost("http://192.168.46.35:8080/BGpsWeb/gpsForeignInterface/shipLocation.do");  


         String responseMessage=null;
        List<NameValuePair> formparams = new ArrayList<NameValuePair>();
        //formparams.add(new BasicNameValuePair("cqdd", xmlMessage));
        formparams.add(new BasicNameValuePair("cqdd", xmlMessage));
        UrlEncodedFormEntity entity;

        try {  
            entity = new UrlEncodedFormEntity(formparams, "GBK");
            httppost.setEntity(entity);  
            System.out.println("executing request " + httppost.getURI());  
            CloseableHttpResponse response = httpclient.execute(httppost);  
            try {  
                HttpEntity reEntity = response.getEntity();  
               if (entity != null) {  
                    System.out.println("--------------------------------------"); 
                    responseMessage=EntityUtils.toString(reEntity);
                   //System.out.println("Response content: " + EntityUtils.toString(reEntity, "UTF-8"));  
                    System.out.println("--------------------------------------");  
                }  
            } finally {  
                response.close();  
                }  
            } catch (ClientProtocolException e) {  
                e.printStackTrace();  
            } catch (UnsupportedEncodingException e1) {  
                e1.printStackTrace();  
            } catch (IOException e) {  
                e.printStackTrace();  
            } finally {  
                // 关闭连接,释放资源     
                try {  
                  httpclient.close();  
               } catch (IOException e) {  
                    e.printStackTrace();  
                }  
            }  

        return responseMessage;

    }

2个回答

dcxy0
dcxy0   Ds   Rxr 2015.07.14 15:26

1.用本机电脑ping一下你要连接的地址是可用的嘛?

2.需要长连接嘛?你那个错误,貌似是连接问题,有可能是网络不稳定造成的。

YoshionTwo
YoshionTwo 这两个问题应该都不是的。
2 年多之前 回复
oyljerry
oyljerry   Ds   Rxr 2015.07.14 16:23

是不是你读取数据的时候异常了。服务器返回的数据你处理正确吗

YoshionTwo
YoshionTwo 其实我是像gps推送信息,都不需要返回值的。刚刚我测的时候要他设断点走一遍没报错,接着测了几次都不报错,但是我隔一段时间再去请求就报错了。这是个什么问题?
2 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片