HttpClient的URL调用失败。

huangapple 未分类评论47阅读模式
英文:

HttpClient URL call fails

问题

以下是翻译好的内容:

我正在尝试从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":"URL set up done for https://ABCD.com/","logger":"com.tesco.ReceiptClient.PublishClient.ReceiptPostClient:PostMessage"

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

"message":"Auth cache not set in the context","logger":"org.apache.http.client.protocol.RequestAuthCache:process"

"message":"Connection request: [route: {s}->https://ABCD.com:443][total kept alive: 0; route allocated: 0 of 2; total allocated: 0 of 20]","logger":"org.apache.http.impl.conn.PoolingHttpClientConnectionManager:requestConnection"

"message":"Connection leased: [id: 1][route: {s}->https://ABCD.com:443][total kept alive: 0; route allocated: 1 of 2; total allocated: 1 of 20]","logger":"org.apache.http.impl.conn.PoolingHttpClientConnectionManager:leaseConnection"

"message":"Opening connection {s}->https://ABCD.com:443","logger":"org.apache.http.impl.execchain.MainClientExec:execute"

"message":"Connecting to ABCD.com/xxx.xx.xx.x:443","logger":"org.apache.http.impl.conn.DefaultHttpClientConnectionOperator:connect"

"message":"Connecting socket to ABCD.com/xxx.xx.xx.x:443 with timeout 3000","logger":"org.apache.http.conn.ssl.SSLConnectionSocketFactory:connectSocket"

"message":"http-outgoing-1: Shutdown connection","logger":"org.apache.http.impl.conn.LoggingManagedHttpClientConnection:shutdown"

"message":"Connection discarded","logger":"org.apache.http.impl.execchain.ConnectionHolder:abortConnection"

"message":"Connection released: [id: 2][route: {s}->https://ABCD.com:443][total kept alive: 0; route allocated: 0 of 2; total allocated: 0 of 20]","logger":"org.apache.http.impl.conn.PoolingHttpClientConnectionManager:releaseConnection"

"message":"Error at PostClient.IOException. Connect to ABCD.com:443 [ABCD.com/xxx.xx.xx.x] failed: connect timed out","logger":"com.tesco.ReceiptClient.PublishClient.ReceiptPostClient:PostMessage"

英文:

I am trying to do a Http POST to an API from a linux machine. But my call to the URL fails with timeout error. I am using httpclient-4.5.8 library for this.
Background: Initially my client server was not whitelisted so I used to get "403-Forbidden" from the URL. But after whitelisting there is timeout error.

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());
        }
}

Log trace: (Pardon for the masking)

"message":"URL set up done for https://ABCD.com/","logger":"com.tesco.ReceiptClient.PublishClient.ReceiptPostClient:PostMessage"

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

"message":"Auth cache not set in the context","logger":"org.apache.http.client.protocol.RequestAuthCache:process"

"message":"Connection request: [route: {s}->https://ABCD.com:443][total kept alive: 0; route allocated: 0 of 2; total allocated: 0 of 20]","logger":"org.apache.http.impl.conn.PoolingHttpClientConnectionManager:requestConnection"

"message":"Connection leased: [id: 1][route: {s}->https://ABCD.com:443][total kept alive: 0; route allocated: 1 of 2; total allocated: 1 of 20]","logger":"org.apache.http.impl.conn.PoolingHttpClientConnectionManager:leaseConnection"

"message":"Opening connection {s}->https://ABCD.com:443","logger":"org.apache.http.impl.execchain.MainClientExec:execute"

"message":"Connecting to ABCD.com/xxx.xx.xx.x:443","logger":"org.apache.http.impl.conn.DefaultHttpClientConnectionOperator:connect"

"message":"Connecting socket to ABCD.com/xxx.xx.xx.x:443 with timeout 3000","logger":"org.apache.http.conn.ssl.SSLConnectionSocketFactory:connectSocket"

"message":"http-outgoing-1: Shutdown connection","logger":"org.apache.http.impl.conn.LoggingManagedHttpClientConnection:shutdown"

"message":"Connection discarded","logger":"org.apache.http.impl.execchain.ConnectionHolder:abortConnection"

"message":"Connection released: [id: 2][route: {s}->https://ABCD.com:443][total kept alive: 0; route allocated: 0 of 2; total allocated: 0 of 20]","logger":"org.apache.http.impl.conn.PoolingHttpClientConnectionManager:releaseConnection"

"message":"Error at PostClient.IOException. Connect to ABCD.com:443 [ABCD.com/xxx.xx.xx.x] failed: connect timed out","logger":"com.tesco.ReceiptClient.PublishClient.ReceiptPostClient:PostMessage"

答案1

得分: 0

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

通常情况下,当您与服务器没有任何连接时,您会收到连接超时错误;如果服务器花费的时间比“read-timeout”配置所给的时间长,则会收到读取超时错误。

英文:

Check if there is connectivity with the server (telnet/ping).

Normally you will get Connection time out error when you don't have any connectivity with server and Read time out if the server takes longer time then given by "read-timeout" config.

huangapple
  • 本文由 发表于 2020年5月5日 20:38:20
  • 转载请务必保留本文链接:https://java.coder-hub.com/61613347.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定