我正在我的java应用程序上设置google captcha,我得到连接超时:
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
为了测试网络是否有问题,我用Postman和cURL发出了同样的请求,都返回了预期的响应,然后我用Postman生成java代码,运行它,得到了同样的错误。
请注意,为了使请求与Postman一起工作,我必须关闭“使用系统代理”。这使我想到,也许java默认使用系统代理,并试图用System.setProperty("java.net.useSystemProxies", "false");
禁用它(也尝试了true)。仍然得到相同的错误。
下面是使用的代码示例:
HttpResponse<String> response = Unirest.post("https://www.google.com/recaptcha/api/siteverify")
.header("response", "abc")
.header("secret", "abc")
.asString();
先谢谢你。
1条答案
按热度按时间2w3rbyxf1#
就像很多时候一样,原因非常简单:
目标服务器只有一个IPv4地址。由于某种原因,Java试图使用IPv6访问目标服务器,但失败了。我假设这种情况会“暂停”正在进行的进程,这只能通过中止挂起的线程来补救(超时的有效作用)
只要我在程序调用中添加-Djava.net.preferIPv4Stack=true,就可以成功地运行它。
来源:HttpClient hits timeout but server is available and working flawlessly
也许吧?