问题描述
- 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; }
解决方案
1.用本机电脑ping一下你要连接的地址是可用的嘛?
2.需要长连接嘛?你那个错误,貌似是连接问题,有可能是网络不稳定造成的。
解决方案二:
是不是你读取数据的时候异常了。服务器返回的数据你处理正确吗
时间: 2024-10-28 05:44:37