<div class="article-content">
<h2 class="heading">序</h2>
<p>本文主要研究一下httpclient的connect timeout异常</p>
<h2 class="heading">实例代码</h2>
<pre class="blockcode"><code class="hljs bash copyable"> @Test
public void <span class="hljs-built_in">test</span>ConnectTimeout() throws IOException, InterruptedException {
HttpClient client = HttpClient.newBuilder()
.build();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(<span class="hljs-string">"https://twitter.com"</span>))
.build();
long start = System.currentTimeMillis();
try{
HttpResponse<String> result = client.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(result.body());
}finally {
long cost = System.currentTimeMillis() - start;
System.out.println(<span class="hljs-string">"cost:"</span>+cost);
}
}
<span class="copy-code-btn">复制代码</span></code></pre>
<blockquote>
<p>异常日志如下:</p>
</blockquote>
<pre class="blockcode"><code class="hljs bash copyable">cost:75814
java.net.ConnectException: Operation timed out
at java.net.http/jdk.internal.net.http.HttpClientImpl.send(HttpClientImpl.java:561)
at java.net.http/jdk.internal.net.http.HttpClientFacade.send(HttpClientFacade.java:119)
at com.example.HttpClientTest.testConnectTimeout(HttpClientTest.java:464)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.junit.runners.model.FrameworkMethod<span class="hljs-variable">$1</span>.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner<span class="hljs-variable">$3</span>.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner<span class="hljs-variable">$1</span>.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access<span class="hljs-variable">$000</span>(ParentRunner.java:58)
at org.junit.runners.ParentRunner<span class="hljs-variable">$2</span>.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner<span class="hljs-variable">$Repeater</span>.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at com.intellij.rt.execution.application.AppMainV2.main(AppMainV2.java:131)
Caused by: java.net.ConnectException: Operation timed out
at java.base/sun.nio.ch.SocketChannelImpl.checkConnect(Native Method)
at java.base/sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:779)
at java.net.http/jdk.internal.net.http.PlainHttpConnection<span class="hljs-variable">$ConnectEvent</span>.handle(PlainHttpConnection.java:128)
at java.net.http/jdk.internal.net.http.HttpClientImpl<span class="hljs-variable">$SelectorManager</span>.handleEvent(HttpClientImpl.java:957)
at java.net.http/jdk.internal.net.http.HttpClientImpl<span class="hljs-variable">$SelectorManager</span>.lambda<span class="hljs-variable">$run</span><span class="hljs-variable">$3</span>(HttpClientImpl.java:912)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1540)
at java.net.http/jdk.internal.net.http.HttpClientImpl<span class="hljs-variable">$SelectorManager</span>.run(HttpClientImpl.java:912)
<span class="copy-code-btn">复制代码</span></code></pre>
<h2 class="heading">Exchange.responseAsync</h2>
<p>java.net.http/jdk/internal/net/http/Exchange.java</p>
<pre class="blockcode"><code class="hljs bash copyable"> public CompletableFuture<Response> <span class="hljs-function"><span class="hljs-title">responseAsync</span></span>() {
<span class="hljs-built_in">return</span> responseAsyncImpl(null);
}
CompletableFuture<Response> responseAsyncImpl(HttpConnection connection) {
SecurityException e = checkPermissions();
<span class="hljs-keyword">if</span> (e != null) {
<span class="hljs-built_in">return</span> MinimalFuture.failedF |
|