gpt4 book ai didi

java - 寻找主机时如何设置套接字超时?

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:15:41 25 4
gpt4 key购买 nike

在我的单元测试中,我使用 commons httpclient 请求一个不正确的远程网站,最大超时是:

@Test(timeout = 10000)
public void should_not_be_accessible_if_configuration_is_incorrect() throws Exception {
// use httpclient to visit an invalid remote http web site
}

但在某些电脑上可能会抛出这样的异常:

java.lang.Exception: test timed out after 10000 milliseconds
at java.net.Inet4AddressImpl.lookupAllHostAddr(Native Method)
at java.net.InetAddress$1.lookupAllHostAddr(InetAddress.java:894)
at java.net.InetAddress.getAddressesFromNameService(InetAddress.java:1286)
at java.net.InetAddress.getAllByName0(InetAddress.java:1239)
at java.net.InetAddress.getAllByName(InetAddress.java:1155)
at java.net.InetAddress.getAllByName(InetAddress.java:1091)
at org.apache.http.impl.conn.SystemDefaultDnsResolver.resolve(SystemDefaultDnsResolver.java:44)
at org.apache.http.impl.conn.HttpClientConnectionOperator.connect(HttpClientConnectionOperator.java:102)
at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:314)
at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:357)
at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:218)
at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:194)
at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:85)
at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:108)
at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:186)
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)

它似乎阻塞了方法:java.net.Inet4AddressImpl.lookupAllHostAddr()

我想知道有没有办法为它设置最大超时时间?

最佳答案

您可以通过描述的超时属性控制 Java 的 DNS 超时 here :

com.example.jndi.dns.timeout.initial
com.example.jndi.dns.timeout.retries

关于java - 寻找主机时如何设置套接字超时?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21084830/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com