【问题标题】:HttpClient URL call failsHttpClient URL 调用失败
【发布时间】:2020-08-20 03:01:18
【问题描述】:

我正在尝试从 linux 机器对 API 进行 Http POST。但是我对 URL 的调用失败并出现超时错误。我为此使用 httpclient-4.5.8 库。 背景:最初我的客户端服务器没有被列入白名单,所以我曾经从 URL 中获取“403-Forbidden”。但是加入白名单后出现超时错误。

public void PostMessage() {     
    try {

        final RequestConfig params = RequestConfig.custom().setConnectTimeout(3000).setSocketTimeout(3000).build();
        httpPost = new HttpPost(getUri());
        log4.debug("URL set up done for "+ httpPost.getURI());
        final StringEntity entity = new StringEntity(getMessage());
        httpPost.setConfig(params);
        httpPost.setEntity(entity);
        httpPost.setHeader("Content-type", "application/json");


        CloseableHttpResponse response = (CloseableHttpResponse) client.execute(httpPost);
        log4.info("Response Code:" + response.getStatusLine().getStatusCode());
        BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
        String line = "";
        while ((line = rd.readLine()) != null) {
            log4.info("Response Content:" + line);
        }
        } catch (IOException i) {
            log4.info("Error at PostClient.IOException. " + i.getMessage());
        } catch(Exception e) {
            log4.info("Error at PostClient.Exception. " + e.getMessage());
        }
}

日志跟踪:(请原谅屏蔽)

"message":"https://ABCD.com/ 的 URL 设置完成","logger":"com.tesco.ReceiptClient.PublishClient.ReceiptPostClient:PostMessage"

"message":"CookieSpec selected: default","logger":"org.apache.http.client.protocol.RequestAddCookies:process"

"message":"上下文中没有设置授权缓存","logger":"org.apache.http.client.protocol.RequestAuthCache:process"

"message":"连接请求:[路由:{s}->https://ABCD.com:443][total 保持活动状态:0;路由分配:0 of 2;总分配:0 of 20]","logger":"org. apache.http.impl.conn.PoolingHttpClientConnectionManager:requestConnection"

"message":"连接租用:[id:1][路由:{s}->https://ABCD.com:443][total保持活动状态:0;路由分配:1 of 2;总分配:1 of 20]","logger ":"org.apache.http.impl.conn.PoolingHttpClientConnectionManager:leaseConnection"

"message":"打开连接 {s}->https://ABCD.com:443","logger":"org.apache.http.impl.execchain.MainClientExec:execute"

"message":"正在连接到 ABCD.com/xxx.xx.xx.x:443","logger":"org.apache.http.impl.conn.DefaultHttpClientConnectionOperator:connect"

"message":"连接套接字到 ABCD.com/xxx.xx.xx.x:443 超时 3000","logger":"org.apache.http.conn.ssl.SSLConnectionSocketFactory:connectSocket"

"message":"http-outgoing-1: 关闭连接","logger":"org.apache.http.impl.conn.LoggingManagedHttpClientConnection:shutdown"

"message":"连接被丢弃","logger":"org.apache.http.impl.execchain.ConnectionHolder:abortConnection"

"message":"连接释放:[id:2][路由:{s}->https://ABCD.com:443][total保持活动状态:0;路由分配:0 of 2;总分配:0 of 20]","logger ":"org.apache.http.impl.conn.PoolingHttpClientConnectionManager:releaseConnection"

"message":"PostClient.IOException 出错。连接到 ABCD.com:443 [ABCD.com/xxx.xx.xx.x] 失败:连接超时","logger":"com.tesco。 ReceiptClient.PublishClient.ReceiptPostClient:PostMessage"

【问题讨论】:

  • 您是否尝试过将超时设置为一些非常大的值而不是 3000?
  • 可能是服务器坏了?还是您的要求是?或者通话时间超过 3 秒?你能证明服务器使用休息客户端工作吗?像邮递员之类的?
  • @RahulAgrawal 我现在将连接和套接字超时都设置为 30000,它等待 30 秒,但失败并出现同样的错误。
  • @thst 问题是我可以从任何 UX 客户端测试它,因为它是一台服务器机器。但是我确实尝试在 URL 上执行 wget 并且似乎在相同的超时错误上失败了。我想我可能需要请求 URL 人员来演示一个成功的请求。
  • 但是“ux 客户端”有效吗?姐姐你检查代理等设置

标签: java apache-httpclient-4.x


【解决方案1】:

检查是否与服务器连接(telnet/ping)。

通常,当您与服务器没有任何连接时,您会收到 Connection time out 错误,如果服务器花费的时间比“read -timeout”配置。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-04-20
    • 1970-01-01
    • 2017-08-02
    • 2015-08-04
    • 2015-04-13
    • 1970-01-01
    相关资源
    最近更新 更多