使用带有Apache HttpClient的HTTP Post获取连接超时错误

时间:2018-04-02 18:47:30

标签: rest apache-httpclient-4.x

尝试使用Java和Apache HttpClient(4.5.2)将POST请求发送到防火墙外的RESTful API。

请注意,使用curl或Postman时,对API的POST请求工作正常。

我正在使用Apache HttpClient,代码(服务器名称和主机是虚构的):

    System.setProperty("http.proxyHost","my.proxy.server");
    System.setProperty("http.proxyPort","7070");

    CredentialsProvider provider = new BasicCredentialsProvider();
    UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(
            "myusername","mypassword");

    provider.setCredentials(AuthScope.ANY, credentials);

    httpClient = HttpClientBuilder.create().setDefaultCredentialsProvider(provider).build();            

    String postUrl = "https://myrest.server.com/api/documents?document_id=" + id
            + "&title=" + URLEncoder.encode(title, "UTF-8")
            + "&path=" +  URLEncoder.encode(path,"UTF-8")
            + "&content=" +  URLEncoder.encode(content,"UTF-8");                

    HttpPost post = new HttpPost(postUrl);

    StringEntity entity = new StringEntity("");
    entity.setContentType("application/x-www-form-urlencoded");
    post.setEntity(entity);

    HttpResponse response = httpClient.execute(post);

错误:

Caused by: java.net.ConnectException: Connection timed out: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at org.apache.http.conn.ssl.SSLConnectionSocketFactory.connectSocket(SSLConnectionSocketFactory.java:337)
at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:141)
... 37 more

调试跟踪:

DEBUG [org.apache.http.client.protocol.RequestAddCookies] CookieSpec selected: default
DEBUG [org.apache.http.client.protocol.RequestAuthCache] Auth cache not set in the context
DEBUG [org.apache.http.impl.conn.PoolingHttpClientConnectionManager] Connection request: [route: {s}->https://myrest.server.com:443][total kept alive: 0; route allocated: 0 of 2; total allocated: 0 of 20]
DEBUG [org.apache.http.impl.conn.PoolingHttpClientConnectionManager] Connection leased: [id: 0][route: {s}->https://myrest.server.com:443][total kept alive: 0; route allocated: 1 of 2; total allocated: 1 of 20]
DEBUG [org.apache.http.impl.execchain.MainClientExec] Opening connection {s}->https://myrest.server.com:443
DEBUG [org.apache.http.impl.conn.DefaultHttpClientConnectionOperator] Connecting to myrest.server.com/202.202.202.202:443
DEBUG [org.apache.http.conn.ssl.SSLConnectionSocketFactory] Connecting socket to myrest.server.com/202.202.202.202:443 with timeout 0
DEBUG [org.apache.http.impl.conn.DefaultHttpClientConnectionOperator] Connect to myrest.server.com/202.202.202.202:443 timed out. Connection will be retried using another IP address
DEBUG [org.apache.http.impl.conn.DefaultHttpClientConnectionOperator] Connecting to myrest.server.com/202.202.202.202:443
DEBUG [org.apache.http.conn.ssl.SSLConnectionSocketFactory] Connecting socket to myrest.server.com/202.202.202.202:443 with timeout 0
DEBUG [org.apache.http.impl.conn.DefaultManagedHttpClientConnection] http-outgoing-0: Shutdown connection
DEBUG [org.apache.http.impl.execchain.MainClientExec] Connection discarded
DEBUG [org.apache.http.impl.conn.PoolingHttpClientConnectionManager] Connection released: [id: 0][route: {s}->https://myrest.server.com:443][total kept alive: 0; route allocated: 0 of 2; total allocated: 0 of 20]

0 个答案:

没有答案