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解决。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

最近在项目开发中遇到了一个问题,用restTemplate调用https接口的时候一直掉不通,报错I/O error on POST request for “xxxx”: Remote host terminated the handshake;nested exception is javax.net.ssl.SSLHandshakeException: Remote host terminated the handshake 远程主机终止了握手

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

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

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

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

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

下面附上完整的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");

问题解决,希望能够帮到你文章来源地址https://www.toymoban.com/news/detail-503580.html

到了这里,关于restTemplate发送https请求报错I/O error on POST request for “xxxx“: Remote host terminated the handshake解决的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包赞助服务器费用

相关文章

  • requests  发送一个 json 格式的 post 请求

    requests 发送一个 json 格式的 post 请求

    今天给一位同学解决post发送数据格式为json格式的请求,顺便确认一下问题归属。 背景: 用postman工具发送一个数据格式为json的请求,得到了服务器的响应。 用python的requests库写的请求,却报错了。没有得到该有的结果。 解决方法: 先确认自己的请求信息和函数使用正确。包

    2024年02月07日
    浏览(12)
  • postman发送https post请求

    postman发送https post请求

    postman发送https post请求 1、点击File选择Setting 2、General中选择ssl认证关闭 3、Certificates中选择点击Add Certificate 4、输入请求服务的IP,点击add 5、选择File new postman window 6、选择请求方式为post 填入url ,request 输入headers参数和依赖token 7、输入body中的值 ,如发送json选择raw,选择js

    2024年02月11日
    浏览(14)
  • Python爬虫requests判断请求超时并重新post/get发送请求

    在上面的示例中,send_request_get函数接受一个URL作为参数,并可选地指定最大重试次数和超时时间。函数使用 requests.get 发送GET请求,并设置了超时时间为5秒。如果请求超时,会捕获 requests.exceptions.Timeout 异常,并输出重试信息。如果发生其他异常,会捕获 requests.exceptions.Req

    2024年02月11日
    浏览(25)
  • ruoyi 后端发送http/https post请求

    1.1 JSONObject转换为String类型后进行发送 1.1.1头部请求添加:` conn.setRequestProperty(“Content-Type”, “application/json”); ` 1.1.2 发送操作主要部分 2.1 JSONObject转换为String类型后进行发送 2.1.1头部请求添加:` conn.setRequestProperty(“Content-Type”, “application/json”); ` 2.1.2 发送操作主要部分

    2024年02月11日
    浏览(11)
  • Python使用POST方法发送HTTP请求的15个示例(基于requests)

    以下是使用 requests 库调用HTTP接口进行POST请求的15个示例: 发送简单的POST请求: 发送JSON格式的POST请求: 发送XML格式的POST请求: 发送文件的POST请求:

    2024年02月10日
    浏览(15)
  • Postman发送post请求时报400错误,Required request body is missing

    Postman发送post请求时报400错误,Required request body is missing

    项目形参位置存在@RequestBody注解,用Postman发送post请求时报400错误,Required request body is missing。 错误图示: 解决方法: 方法一: 项目中形参位置不使用@RequestBody,在Postman进行Post请求时,在请求路径后直接拼接参数。 方法二: 项目中形参位置使用@RequestBody,在Postman进行Po

    2024年02月11日
    浏览(13)
  • 【微信小程序】使用 wx.request 方法来发送POST网络请求,携带RequestBody参数

    在微信小程序中,你可以使用 wx.request 方法来发送网络请求。以下是将上述 Java 代码转换为微信小程序版本的示例: 在上述代码中,我们使用 wx.request 方法发送 POST 请求,并将请求的 URL、请求体数据、请求头等信息进行相应的设置。请求成功后,会在回调函数的 success 中处

    2024年02月15日
    浏览(16)
  • RestTemplate发起HTTPS请求Unsupported or unrecognized SSL message 报错解决

    原因:RestTemplate默认是不支持HTTPS请求的,那么如果想使用RestTemplate发送一个HTTPS的请求 ,就需要对证书进行处理,这里记录一下一个可行的方法忽略证书的校验。 使用 RestTemplateBuilder 来构建一个 RestTemplate ,而非使用默认。 requestFactory() 方法用来设置 ClientHttpRequestFactory 。

    2024年02月11日
    浏览(12)
  • I/O error on POST request for “http://localhost:9411/api/v2/spans“

    I/O error on POST request for “http://localhost:9411/api/v2/spans“

    报错信息展示 整合微服务架构的时候,控制台出现报错信息: I/O error on POST request for \\\"http://localhost:9411/api/v2/spans\\\" 问题分析         由上图可以得知是zipkin导致的报错,但是目前项目中并没有整合zipkin,查阅资料后后发现是依赖中含有zipkin 依赖,但是没有配置zipkin-serve

    2024年02月12日
    浏览(11)
  • 使用Python的requests库发送HTTPS请求时的SSL证书验证问题

    问题描述 使用python的requests库去发送https请求,有时候不设置verify=False不报错,有时候又报错。 问题原因 使用Python的requests库发送HTTPS请求时,设置verify=False参数可以跳过SSL证书验证。默认情况下,requests库会验证SSL证书以确保请求的安全性。然而,在某些情况下,可能会出现

    2024年02月17日
    浏览(17)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包