当前位置: 首页 > 知识库问答 >
问题:

对等方重置HttpClient连接:套接字写入错误

符棋
2023-03-14

我使用HttpClient4.4发送get和post请求。并且我刚刚创建了一个httpclient的simpile包装器以方便使用:


    package com.u8.server.sdk;

    import com.sun.net.httpserver.Headers;
    import com.u8.server.log.Log;
    import org.apache.http.Header;
    import org.apache.http.HttpEntity;
    import org.apache.http.HttpResponse;
    import org.apache.http.NameValuePair;
    import org.apache.http.client.ClientProtocolException;
    import org.apache.http.client.HttpClient;
    import org.apache.http.client.HttpRequestRetryHandler;
    import org.apache.http.client.ResponseHandler;
    import org.apache.http.client.config.RequestConfig;
    import org.apache.http.client.entity.UrlEncodedFormEntity;
    import org.apache.http.client.methods.HttpGet;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.config.Registry;
    import org.apache.http.config.RegistryBuilder;
    import org.apache.http.conn.socket.ConnectionSocketFactory;
    import org.apache.http.conn.socket.PlainConnectionSocketFactory;
    import org.apache.http.conn.ssl.NoopHostnameVerifier;
    import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
    import org.apache.http.conn.ssl.TrustStrategy;
    import org.apache.http.entity.ByteArrayEntity;
    import org.apache.http.impl.client.BasicCookieStore;
    import org.apache.http.impl.client.CloseableHttpClient;
    import org.apache.http.impl.client.HttpClientBuilder;
    import org.apache.http.impl.client.HttpClients;
    import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
    import org.apache.http.impl.cookie.BasicClientCookie;
    import org.apache.http.message.BasicHeader;
    import org.apache.http.message.BasicNameValuePair;
    import org.apache.http.protocol.HttpContext;
    import org.apache.http.ssl.SSLContextBuilder;
    import org.apache.http.util.EntityUtils;
    import org.springframework.util.StringUtils;

    import javax.net.ssl.HostnameVerifier;
    import javax.net.ssl.SSLContext;
    import java.io.IOException;
    import java.io.UnsupportedEncodingException;
    import java.net.CookiePolicy;
    import java.net.URLEncoder;
    import java.nio.charset.Charset;
    import java.security.cert.CertificateException;
    import java.security.cert.X509Certificate;
    import java.util.ArrayList;
    import java.util.Iterator;
    import java.util.List;
    import java.util.Map;

    /**
     * Created by ant on 2015/10/12.
     */
    public class UHttpAgent {

        private int connectTimeout = 5000;  //5s
        private int socketTimeout = 5000;   //5s
        private int maxTotalConnections = 200; 

        private static UHttpAgent instance;

        private CloseableHttpClient httpClient;

        private UHttpAgent(){

        }

        public static UHttpAgent getInstance(){
            if(instance == null){
                instance = new UHttpAgent();

            }
            return instance;
        }

        public static UHttpAgent newInstance(){
            return new UHttpAgent();
        }


        public String get(String url, Map params){

            return get(url, null, params, "UTF-8");
        }


        public String post(String url, Map params){

            return post(url, null, params, "UTF-8");
        }


        public String get(String url , Map headers, Map params, String encoding){

            if(this.httpClient == null){
                init();
            }

            String fullUrl = url;
            String urlParams = parseGetParams(params, encoding);

            if (urlParams != null)
            {
                if (url.contains("?"))
                {
                    fullUrl = url + "&" + urlParams;
                }
                else
                {
                    fullUrl = url + "?" + urlParams;
                }
            }

            Log.d("the full url is "+ fullUrl);
            HttpGet getReq = new HttpGet(fullUrl.trim());
            getReq.setHeaders(parseHeaders(headers));
            ResponseHandler responseHandler = new ResponseHandler() {
                @Override
                public String handleResponse(HttpResponse httpResponse) throws IOException {
                    HttpEntity entity = httpResponse.getEntity();
                    return entity != null ? EntityUtils.toString(entity) : null;
                }
            };

            try {

                String res = httpClient.execute(getReq, responseHandler);

                return res;

            } catch (Exception e) {
                e.printStackTrace();
            }finally {
                getReq.releaseConnection();
            }

            return null;
        }


        public String post(String url, Map headers, Map params, String encoding){

            List pairs = new ArrayList();
            if(params != null){

                for(String key : params.keySet()){
                    pairs.add(new BasicNameValuePair(key, params.get(key)));
                }
            }

            return post(url, headers, new UrlEncodedFormEntity(pairs, Charset.forName(encoding)));
        }


        public String post(String url, Map headers, HttpEntity entity){

            if(this.httpClient == null) {
                init();
            }

            HttpPost httpPost = new HttpPost(url);
            httpPost.setHeaders(parseHeaders(headers));
            httpPost.setEntity(entity);

            ResponseHandler responseHandler = new ResponseHandler() {
                @Override
                public String handleResponse(HttpResponse httpResponse) throws IOException {
                    HttpEntity entity = httpResponse.getEntity();
                    return entity != null ? EntityUtils.toString(entity) : null;
                }
            };

            try {


                String body = httpClient.execute(httpPost, responseHandler);

                return body;

            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                httpPost.releaseConnection();
            }

            return null;

        }


        private Header[] parseHeaders(Map headers){
            if(null == headers || headers.isEmpty()){
                return getDefaultHeaders();
            }

            Header[] hs = new BasicHeader[headers.size()];
            int i = 0;
            for(String key : headers.keySet()){
                hs[i] = new BasicHeader(key, headers.get(key));
                i++;
            }

            return hs;
        }


        private Header[] getDefaultHeaders(){
            Header[] hs = new BasicHeader[2];
            hs[0] = new BasicHeader("Content-Type", "application/x-www-form-urlencoded");
            hs[1] = new BasicHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.146 Safari/537.36");
            return hs;
        }


        private String parseGetParams(Map data, String encoding){
            if(data == null || data.size()  keyItor = data.keySet().iterator();
            while(keyItor.hasNext()){
                String key = keyItor.next();
                String val = data.get(key);

                try {
                    result.append(key).append("=").append(URLEncoder.encode(val, encoding).replace("+", "%2B")).append("&");
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            }

            return result.deleteCharAt(result.length() - 1).toString();

        }


        private void init(){

            RequestConfig requestConfig = RequestConfig.custom()
                    .setConnectTimeout(connectTimeout)
                    .setSocketTimeout(socketTimeout)
                    .setExpectContinueEnabled(true)
                    .setAuthenticationEnabled(true)
                    .build();

            HttpRequestRetryHandler retryHandler = new HttpRequestRetryHandler() {
                @Override
                public boolean retryRequest(IOException e, int retryNum, HttpContext httpContext) {

                    if(retryNum >= 3){
                        return false;
                    }


                    if(e instanceof org.apache.http.NoHttpResponseException
                            || e instanceof org.apache.http.client.ClientProtocolException
                            || e instanceof java.net.SocketException){

                        return true;
                    }

                    return false;
                }
            };


            try{

                SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {

                    @Override
                    public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
                        return true;
                    }
                }).build();

                HostnameVerifier hostnameVerifier = NoopHostnameVerifier.INSTANCE;
                SSLConnectionSocketFactory sslFactory = new SSLConnectionSocketFactory(sslContext, hostnameVerifier);

                Registry socketFactoryRegistry = RegistryBuilder.create()
                        .register("http", PlainConnectionSocketFactory.getSocketFactory())
                        .register("https", sslFactory)
                        .build();

                PoolingHttpClientConnectionManager connMgr = new PoolingHttpClientConnectionManager( socketFactoryRegistry);
                connMgr.setMaxTotal(maxTotalConnections);
                connMgr.setDefaultMaxPerRoute((connMgr.getMaxTotal()));

                HttpClientBuilder builder = HttpClients.custom()
                        .setDefaultRequestConfig(requestConfig)
                        .setSslcontext(sslContext)
                        .setConnectionManager(connMgr)
                        .setRetryHandler(retryHandler);

                this.httpClient = builder.build();


            }catch (Exception e){
                e.printStackTrace();
            }

        }

        public HttpClient getHttpClient(){

            return this.httpClient;
        }

        public void destroy(){

            if(this.httpClient != null){
                try{
                    this.httpClient.close();
                    this.httpClient = null;
                }catch (Exception e){
                    e.printStackTrace();
                }

            }
        }


    }

当我使用这个类发送post请求时。奇怪的事情发生了:

第一次,我向服务器发送一个post请求,一分钟后就可以了,我向服务器发送一个同样的请求,也可以了。但几分钟后,我发出了一个同样的请求,有些不对劲:


    java.net.SocketException: Connection reset by peer: socket write error
        at java.net.SocketOutputStream.socketWrite0(Native Method)
        at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:92)
        at java.net.SocketOutputStream.write(SocketOutputStream.java:136)
        at org.apache.http.impl.conn.LoggingOutputStream.write(LoggingOutputStream.java:77)
        at org.apache.http.impl.io.SessionOutputBufferImpl.streamWrite(SessionOutputBufferImpl.java:126)
        at org.apache.http.impl.io.SessionOutputBufferImpl.flushBuffer(SessionOutputBufferImpl.java:138)
        at org.apache.http.impl.io.SessionOutputBufferImpl.flush(SessionOutputBufferImpl.java:146)
        at org.apache.http.impl.BHttpConnectionBase.doFlush(BHttpConnectionBase.java:175)
        at org.apache.http.impl.DefaultBHttpClientConnection.flush(DefaultBHttpClientConnection.java:185)
        at org.apache.http.impl.conn.CPoolProxy.flush(CPoolProxy.java:177)
        at org.apache.http.protocol.HttpRequestExecutor.doSendRequest(HttpRequestExecutor.java:215)
        at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:122)
        at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:271)
        at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:184)
        at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:88)
        at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110)
        at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184)
        at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:71)
        at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:220)
        at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:164)
        at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:139)
        at com.u8.server.sdk.UHttpAgent.post(UHttpAgent.java:259)
        at com.u8.server.sdk.UHttpAgent.post(UHttpAgent.java:147)
        at com.u8.server.sdk.baidu.BaiduSDK.verify(BaiduSDK.java:30)
        at com.u8.server.web.UserAction.getLoginToken(UserAction.java:100)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)

但是,我仍然向服务器发送一个相同的请求,它又可以了。

每次我按照上面的步骤尝试,都发生了同样的事情。

有人能帮我吗?多谢.

共有1个答案

龙宣
2023-03-14

客户端的对等方是服务器。所以“连接重置由对等端”意味着服务器重置套接字。重置意味着强制关闭。

这可能是因为服务器中的bug。如果您还编写了服务器或servlet(就像自定义协议的情况一样),则需要检查服务器的行为以检查其原因。服务器的日志文件可能提供线索。

如果服务器具有针对行为不端或恶意客户端的保护,则服务器可能已重置套接字,因为它已将您的客户端归类为行为不端。如果您实现了客户端协议代码,这可能是由于协议实现中的一个bug。如果您正在使用第三方代码来处理通信协议,那么您可能仍然在误用它;例如发送过大的请求。HTTP服务器通过对报头字段和正文施加最大长度来保护自己免受拒绝服务攻击,并要求客户端以合理的速度发送数据(而不是长时间暂停),这种情况并不少见。你的当事人可能违反了这些保护措施。

 类似资料:
  • 问题内容: 我使用httpclient 4.4发送get和post请求。我刚刚创建了一个简单的httpclient包装器,以方便使用: 当我使用此类发送帖子请求时。发生了一些奇怪的事情: 第一次,我将发布请求发送到服务器,几分钟后没问题,我将相同的请求发送到服务器,也行。但是几分钟后,我发送了相同的请求,出现了错误: 但是之后,我仍然向服务器发送了相同的请求,再次可以。 每当我按照上述步骤尝试时,

  • 我正在使用apache HttpClient上传固件文件(.bin)。当我上传一个大小为37MB的这样的文件时,我得到一个错误:连接被对等体重置:套接字写入错误。我不知道怎么解决。但当我尝试其他较小大小的文件(如.txt或.bin)时,没有任何错误。

  • 我还尝试提供私有IP和端口4444,运行脚本后,服务器代理立即关闭,并且收到错误java.net.socketException:Connection reset by Peer:socket write error。 > JMeter控制台日志 我做了telnet privateip:4444,并通过了test命令。服务器代理返回yep数据。此设置正在本地框上工作。不在AWS盒子上。 请让我知道

  • 连接错误:写入套接字时出错104。连接由对等重置。 环境: ubuntu: 16.04 python: 3.6 PC总内存: 32G 我已经安装了redis'3.0.6'。 当您尝试插入500000个数据时,它会成功,但当您尝试插入4000万个大数据时,它会失败。 当尝试将Python数据帧插入redis时,由于容量太大而失败。 数据插入成功: 数据插入失败: ConnectionResetErr

  • 我使用的是是一款开源的100%纯驱动程序,适用于。 -我有连接池配置下面 有时,我会遇到以下异常情况:无法从sql server获取连接。我不知道为什么? java.sql.SQLExc0019: I/O错误:连接重置由对等:套接字写入错误net.sourceforge.jtds.jdbc.TdsCore.executeSQL(TdsCore.java:1052)net.sourceforge.j

  • 我正在尝试使用套接字实现一个HTTP服务器。如果客户端(例如浏览器)请求目录,服务器将显示可用文件列表。当客户端请求文件时就会出现问题。我得到以下错误: stacktrace显示问题来自方法: 但我想不出有什么问题。你能帮帮我吗? 编辑 谢谢大家的回答。在我读了你的答案后,我明白了问题是插座出现错误的时候。这是我的错误代码: 我的新代码是这样的: