简体   繁体   English

使用 Java 请求超时,但在 cURL 和 postman 上工作

[英]Request timeout with Java but working on cURL and postman

I'm setting up google recaptcha on my java application and I'm getting connect timeout:我正在我的 java 应用程序上设置 google recaptcha,我得到连接超时:

Caused by: org.apache.http.conn.ConnectTimeoutException: Connect to www.google.com:443 [www.google.com/172.217.168.164] failed: connect timed out
    at org.apache.http.impl.conn.HttpClientConnectionOperator.connect(HttpClientConnectionOperator.java:134)
    at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:319)
    at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:363)
    at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:219)
    at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:195)
    at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:86)
    at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:108)
    at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:106)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:57)
    at com.mashape.unirest.http.HttpClientHelper.request(HttpClientHelper.java:138)
HttpClientHelper.java:138
    ... 2 more
Caused by: java.net.SocketTimeoutException: connect timed out
    at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85)
    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:244)
    at org.apache.http.impl.conn.HttpClientConnectionOperator.connect(HttpClientConnectionOperator.java:1

To test if there was any problem with the network, I made the same request using Postman and cURL, both returned the expected response.为了测试网络是否有任何问题,我使用 Postman 和 cURL 发出了相同的请求,都返回了预期的响应。 Then I used postman to generate java code, ran it and got the same error.然后我使用 postman 生成 java 代码,运行它并得到相同的错误。

Notice that in order for the request to work with Postman I had to turn off "Use System Proxy".请注意,为了使请求与 Postman 一起使用,我必须关闭“使用系统代理”。 This lead me to think that maybe java was using system proxy by default and tried to disable it with System.setProperty("java.net.useSystemProxies", "false");这让我想到 java 默认使用系统代理并尝试使用System.setProperty("java.net.useSystemProxies", "false");禁用它。 (also tried with true). (也尝试过 true)。 Still got same error.仍然有同样的错误。

Here is a example of the code used:以下是使用的代码示例:

 HttpResponse<String> response = Unirest.post("https://www.google.com/recaptcha/api/siteverify")
        .header("response", "abc")
        .header("secret", "abc")
        .asString();

Thanks in advance.提前致谢。

Like so often, the reason was dead simple:像往常一样,原因很简单:

The target server only has an IPv4 address.目标服务器只有一个 IPv4 地址。 Java for some reason tries to access the target using IPv6 and fails to do so. Java 出于某种原因尝试使用 IPv6 访问目标但未能成功。 I assume that this kind of "halts" the ongoing process and this can only be remedied by aborting the hanging thread (what the timeout effectively does)我假设这种“停止”正在进行的过程,这只能通过中止挂起的线程来解决(超时有效地做了什么)

As soon as I addded -Djava.net.preferIPv4Stack=true to the call of my program, I was able to run it successfully.一旦我将 -Djava.net.preferIPv4Stack=true 添加到我的程序调用中,我就能够成功运行它。

source: HttpClient hits timeout but server is available and working flawlessly来源: HttpClient 超时,但服务器可用并且工作正常

maybe?也许?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM