聊聊jdk httpclient的connect timeout异常

论坛 期权论坛     
选择匿名的用户   2021-5-30 02:03   228   0
<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">    &#64;Test
    public void <span class="hljs-built_in">test</span>ConnectTimeout() throws IOException, InterruptedException {
        HttpClient client &#61; HttpClient.newBuilder()
                .build();

        HttpRequest request &#61; HttpRequest.newBuilder()
                .uri(URI.create(<span class="hljs-string">&#34;https://twitter.com&#34;</span>))
                .build();
        long start &#61; System.currentTimeMillis();
        try{
            HttpResponse&lt;String&gt; result &#61; client.send(request, HttpResponse.BodyHandlers.ofString());
            System.out.println(result.body());
        }finally {
            long cost &#61; System.currentTimeMillis() - start;
            System.out.println(<span class="hljs-string">&#34;cost:&#34;</span>&#43;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&lt;Response&gt; <span class="hljs-function"><span class="hljs-title">responseAsync</span></span>() {
        <span class="hljs-built_in">return</span> responseAsyncImpl(null);
    }

    CompletableFuture&lt;Response&gt; responseAsyncImpl(HttpConnection connection) {
        SecurityException e &#61; checkPermissions();
        <span class="hljs-keyword">if</span> (e !&#61; null) {
            <span class="hljs-built_in">return</span> MinimalFuture.failedF
分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP