现在我们系统跟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;
}