当前位置: 代码网 > 服务器>网络>网络协议 > restTemplate发送https请求报错I/O error on POST request for “xxxx“: Remote host terminated the handshake解决

restTemplate发送https请求报错I/O error on POST request for “xxxx“: Remote host terminated the handshake解决

2024年08月02日 网络协议 我要评论
最近在项目开发中遇到了一个问题,用restTemplate调用https接口的时候一直掉不通,报错I/O error on POST request for “xxxx”: Remote host terminated the handshake;

一开始以为是ssl证书的问题。在百度上找了半天,千篇一律都是在resttemplate实例化时加忽略证书。当然我也是加了忽略证书的,但是还是一直报那个错…

在这里插入图片描述

最后找到原因: 因为我访问的是国外的网站,我设置的代理ip是本机127.0.0.1;所以网络一直掉不通。

解决: 配置文件中将地址改为使用代理服务器的ip端口就可以了(没有可以买一个或者挂梯子)

在这里插入图片描述

下面附上完整的resttemplateutils实例化代码:

@component
public class resttemplateutils {

    private static httpproxyproperties httpproxyproperties;

    resttemplateutils(httpproxyproperties properties) {
        httpproxyproperties = properties;
    }

    @sneakythrows
    public static resttemplate getinstance(string charset) {

        truststrategy acceptingtruststrategy = (x509certificate[] chain, string authtype) -> true;

        //忽略证书
        sslcontext sslcontext = org.apache.http.ssl.sslcontexts.custom()
            .loadtrustmaterial(null, acceptingtruststrategy)
            .build();

        sslconnectionsocketfactory csf = new sslconnectionsocketfactory(sslcontext);

        registry<connectionsocketfactory> registry = registrybuilder.<connectionsocketfactory>create()
            .register("http", plainconnectionsocketfactory.getsocketfactory())
            .register("https", csf)
            .build();
        poolinghttpclientconnectionmanager connectionmanager = new poolinghttpclientconnectionmanager(registry);

        //连接池的最大连接数,0代表不限;如果取0,需要考虑连接泄露导致系统崩溃的后果
        connectionmanager.setmaxtotal(1000);
        //每个路由的最大连接数,如果只调用一个地址,可以将其设置为最大连接数
        connectionmanager.setdefaultmaxperroute(300);

        httpclientbuilder clientbuilder = httpclients.custom();
        if (objects.nonnull(httpproxyproperties) && boolean.true.equals(httpproxyproperties.getenabled())) {
            httphost proxy = new httphost(httpproxyproperties.getip(), httpproxyproperties.getport());
            clientbuilder.setproxy(proxy);
        }

        closeablehttpclient httpclient = clientbuilder.setconnectionmanager(connectionmanager)
            .build();

        httpcomponentsclienthttprequestfactory requestfactory =
            new httpcomponentsclienthttprequestfactory();

        requestfactory.sethttpclient(httpclient);
        requestfactory.setconnectionrequesttimeout(10000);
        requestfactory.setconnecttimeout(10000);
        requestfactory.setreadtimeout(30000);

        resttemplate resttemplate = new resttemplate(requestfactory);
        list<httpmessageconverter<?>> list = resttemplate.getmessageconverters();
        for (httpmessageconverter<?> httpmessageconverter : list) {
            if (httpmessageconverter instanceof stringhttpmessageconverter) {
                ((stringhttpmessageconverter) httpmessageconverter).setdefaultcharset(charset.forname(charset));
                break;
            }
        }
        return resttemplate;
    }

}

httpproxyproperties代码:

@data
@component
@configurationproperties(prefix = "http.proxy")
public class httpproxyproperties {

    private boolean enabled;

    private string ip;

    private integer port;

}

使用:

private static resttemplate resttemplate = resttemplateutils.getinstance("utf-8");

问题解决,希望能够帮到你

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com