最近测试,出现这种情况,错误如下:
java.net.SocketException: Connection reset
at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:118)
at java.net.SocketOutputStream.write(SocketOutputStream.java:159)
atorg.apache.http.impl.io.SessionOutputBufferImpl.streamWrite(SessionOutputBufferImpl.java:124)
atorg.apache.http.impl.io.SessionOutputBufferImpl.flushBuffer(SessionOutputBufferImpl.java:136)
atorg.apache.http.impl.io.SessionOutputBufferImpl.write(SessionOutputBufferImpl.java:167)
at org.apache.http.impl.io.ContentLengthOutputStream.write(ContentLengthOutputStream.java:113)
atorg.apache.http.entity.mime.content.FileBody.writeTo(FileBody.java:121)
atorg.apache.http.entity.mime.AbstractMultipartForm.doWriteTo(AbstractMultipartForm.java:134)
at org.apache.http.entity.mime.AbstractMultipartForm.writeTo(AbstractMultipartForm.java:157)
atorg.apache.http.entity.mime.MultipartFormEntity.writeTo(MultipartFormEntity.java:113)
at org.apache.http.impl.DefaultBHttpClientConnection.sendRequestEntity(DefaultBHttpClientConnection.java:156)
atorg.apache.http.impl.conn.CPoolProxy.sendRequestEntity(CPoolProxy.java:162)
atorg.apache.http.protocol.HttpRequestExecutor.doSendRequest(HttpRequestExecutor.java:238)
at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:123)
atorg.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:271)
atorg.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:184)
atorg.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:88)
atorg.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110)
atorg.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184)
atorg.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)
atorg.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:107)
解决方法:
解决方法:
httppost.setProtocolVersion(HttpVersion.HTTP_1_0);
httppost.addHeader(HTTP.CONN_DIRECTIVE, HTTP.CONN_CLOSE);
2.设置超时时间
int timeout = 60000;
RequestConfig defaultRequestConfig = RequestConfig.custom()
.setSocketTimeout(timeout)
.setConnectTimeout(timeout)
.setConnectionRequestTimeout(timeout)
.build();
3.使用 CloseableHttpResponse
CloseableHttpResponse response = null;
response = httpClient.execute(httpPost);
HttpEntity httpEntity = response.getEntity();
// 按照官方文档的说法:二者都释放了才可以正常的释放链接
EntityUtils.consume(httpEntity);
response.close();
完整代码:
package com.curiousby.cn.srpignbootdemo.util;
import java.io.IOException;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.net.ssl.SSLContext;
import org.apache.http.HttpEntity;
import org.apache.http.HttpVersion;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLContextBuilder;
import org.apache.http.conn.ssl.TrustStrategy;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger;
public class HttpHelper {
private final static Logger logger = Logger.getLogger(HttpHelper.class);
private static final String CHARSET = "UTF-8";
private static CloseableHttpClient httpClient = createSSLInsecureClient();
private static final Integer HTTP_OK = 200;
private static final String CONTENT_TYPE_NAME = "Content-Type";
private static final String CONTENT_TYPE_JSON = "application/json;charset=UTF-8";
private static final String CONTENT_TYPE_XML = "text/xml;charset=UTF-8";
private static final String CONTENT_TYPE_FORM = "application/x-www-form-urlencoded;charset=UTF-8";
private static final String ACCEPT_NAME = "Accept";
private static final String ACCEPT = "application/json;charset=UTF-8";
private static final int TIMEOUT = 60000;//这个要弄长点
public static void postUrl(String url, Map<String, Object> params) {
String result = null;
CloseableHttpResponse response = null; //返回结果,释放链接
List<NameValuePair> pairs = new ArrayList<NameValuePair>();
try {
if (params != null && !params.isEmpty()) {
for (Map.Entry<String, Object> entry : params.entrySet()) {
String value = Validator.get_obj_or_empty(entry.getValue()).toString();
if (value != null) {
pairs.add(new BasicNameValuePair(entry.getKey(), value));
}
}
}
RequestConfig defaultRequestConfig = RequestConfig.custom()
.setSocketTimeout(TIMEOUT)
.setConnectTimeout(TIMEOUT)
.setConnectionRequestTimeout(TIMEOUT)
.build();
HttpPost httpPost = new HttpPost(url);
httpPost.setProtocolVersion(HttpVersion.HTTP_1_0);
httpPost.addHeader(CONTENT_TYPE_NAME, CONTENT_TYPE_FORM);
httpPost.addHeader(ACCEPT_NAME, ACCEPT);
httpPost.addHeader(HTTP.CONN_DIRECTIVE, HTTP.CONN_CLOSE);
httpPost.setConfig(defaultRequestConfig);
httpPost.setEntity(new UrlEncodedFormEntity(pairs, CHARSET));
response = httpClient.execute(httpPost);
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode != HTTP_OK) {
httpPost.abort();
logger.error("请求异常, 未返回成功状态码");
return;
}
HttpEntity httpEntity = response.getEntity();
if (httpEntity == null) {
logger.error("返回结果异常");
return;
} else {
result = EntityUtils.toString(httpEntity, CHARSET);
}
EntityUtils.consume(httpEntity);
// 以下可以执行具体的业务代码
// ...
} catch (Exception e) {
logger.error("请求错误"+e.getMessage()+",错误信息"+e);
e.printStackTrace();
} finally {
if (null != response) {
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static CloseableHttpClient createSSLInsecureClient() {
try {
SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null,
new TrustStrategy() {
public boolean isTrusted(X509Certificate[] chain, String authType)
throws CertificateException {
return true;
}
}).build();
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext,
SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
return HttpClients.custom().setMaxConnTotal(100).setMaxConnPerRoute(10)
.setSSLSocketFactory(sslsf).build();
} catch (KeyManagementException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (KeyStoreException e) {
e.printStackTrace();
}
return HttpClients.createDefault();
}
}