使用httpclient 请求报错 :Software caused connection abort: recv failed

Software caused connection abort: recv failed 

java .net .SocketException: Software caused connection abort: recv failed 

at java .net .SocketInputStream.socketRead0(Native Method) 

at java .net .SocketInputStream.read(SocketInputStream.java :32) 

产生这个异常的原因有多种方面

是由于程序编写的问题,而不是网络的问题引起的. 

总结产生原因,在服务端/客户端单方面关闭连接的情况下,另一方依然以为 
tcp连接仍然建立,试图读取对方的响应数据,导致出现 
Software caused connection abort: recv failed 的异常. 

解决方式:每次请求都要释放连接,在对方释放连接后,也要释放本地的连接.

	public static JSONObject postJson(String url, String json) throws Exception{
		CloseableHttpClient httpclientPost = HttpClients.createDefault();
		HttpPost post = new HttpPost(url);
		JSONObject response = null;
		post.setConfig(requestConfig);
		System.err.println("begin at " + new Date().getTime());
		CloseableHttpResponse res = null;
		try {
			StringEntity s = new StringEntity(json, ContentType.APPLICATION_JSON);
			s.setContentEncoding("UTF-8");
			s.setContentType("application/json");// 发送json数据需要设置contentType
			post.setEntity(s);
			res = httpclientPost.execute(post);
			if (res.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
				HttpEntity entity = res.getEntity();
				String result = EntityUtils.toString(res.getEntity());// 返回json格式:
				response = JSONObject.parseObject(result);
			}
		} catch (Exception e) {
			System.err.println("end at " + new Date().getTime());
			throw e;
		} finally {
			try {
                if (res != null) {
                	res.close();
                }
                post.releaseConnection();
                httpclientPost.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
		}
		
		return response;
	}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您好!对于使用HttpClient请求https接口时出现"unable to find valid certification path to requested target"的错误,通常是因为Java运行环境无法验证服务器的证书。这可能是由于以下原因导致的: 1. 服务器证书不受信任:如果服务器使用的证书是自签名或由不受信任的证书颁发机构颁发的,则Java默认情况下会拒绝连接。您可以尝试手动导入服务器的证书到Java的信任证书库中。 2. 缺少根证书:如果您使用的Java运行环境缺少一些根证书,也可能导致验证失败。您可以尝试更新Java运行环境,或手动添加缺少的根证书。 以下是一种可能的解决方法: ```java import java.security.cert.CertificateException;import java.security.cert.X509Certificate; import javax.net.ssl.SSLContext; import javax.net.ssl.TrustManager; import javax.net.ssl.X509TrustManager; import org.apache.http.client.HttpClient; import org.apache.http.conn.ssl.NoopHostnameVerifier; import org.apache.http.conn.ssl.SSLContextBuilder; import org.apache.http.impl.client.HttpClients; public class HttpsClient { public static HttpClient createHttpClient() throws Exception { SSLContext sslContext = SSLContextBuilder.create() .loadTrustMaterial(new TrustManager[]{new X509TrustManager() { public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException { } public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException { } public X509Certificate[] getAcceptedIssuers() { return null; } }}) .build(); return HttpClients.custom() .setSSLContext(sslContext) .setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE) .build(); } public static void main(String[] args) throws Exception { HttpClient httpClient = createHttpClient(); // 使用httpClient发送https请求 // ... } } ``` 这段代码会创建一个忽略证书验证的HttpClient实例,可以尝试使用它发送https请求。请注意,在生产环境中忽略证书验证可能存在安全风险,请谨慎使用
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值