apache httpclient 4.3 没有超时 [英] apache httpclient 4.3 not timing out

查看:36
本文介绍了apache httpclient 4.3 没有超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法使用以下代码使 Apache HttpClient (4.3) 发布请求超时:

I am having trouble getting the Apache HttpClient (4.3) post request to timeout using the following code:

RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(4000).setConnectTimeout(4000)
        .setSocketTimeout(4000).build();

CloseableHttpClient client = HttpClients.custom().setSslcontext(sslContext)
        .setHostnameVerifier(SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER)
        .setDefaultRequestConfig(requestConfig).build();

HttpPost post = new HttpPost("https://localhost:" + connection.getLocalPort() + "/API/webservice.asmx");

StringEntity stringE = new StringEntity(REQUEST);
    post.setEntity(stringE);

CloseableHttpResponse response = client.execute(post);

我通过专有 API 获取连接和端口.当一切顺利时,这会起作用,但是有时(由于我认为连接不好),代码会永远挂在 client.execute(post) 上.我在实现超时或其他一些使该调用超时的方法时做错了什么,所以我不会无限期地卡住.

I am getting the connection and port via a proprietary API. When everything goes well this works however sometimes (due to a bad connection I believe) the code hangs forever at client.execute(post). Is there something I am doing wrong here in implementing the timeout or some other method to make that call have a timeout so I'm not stuck indefinitely.

推荐答案

这是 HttpClient 4.3 中 https 连接的错误.在尝试 SSL 握手之前,套接字上未设置套接字超时.线程可能会在握手期间挂起等待套接字读取.HttpClient 4.2 没有这个错误.

This is a bug with https connections in HttpClient 4.3. The socket timeout is not set on the socket before the SSL handshake is attempted. The thread may hang waiting on socket read during the handshake. HttpClient 4.2 does not have this bug.

这篇关于apache httpclient 4.3 没有超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆