资讯专栏INFORMATION COLUMN

聊聊jdk httpclient的connect timeout异常

张利勇 / 3170人阅读

摘要:序本文主要研究一下的异常实例代码异常日志如下最后调用这里调用获取连接如果没有连接会新创建一个,走的是这里先是调用了获取连接,然后调用进行连接这里委托给这里如果有设置的话,则会创建一个调用进行连接,如果连接未

本文主要研究一下httpclient的connect timeout异常

实例代码

</>复制代码

  1. @Test
  2. public void testConnectTimeout() throws IOException, InterruptedException {
  3. HttpClient client = HttpClient.newBuilder()
  4. .build();
  5. HttpRequest request = HttpRequest.newBuilder()
  6. .uri(URI.create("https://twitter.com"))
  7. .build();
  8. long start = System.currentTimeMillis();
  9. try{
  10. HttpResponse result = client.send(request, HttpResponse.BodyHandlers.ofString());
  11. System.out.println(result.body());
  12. }finally {
  13. long cost = System.currentTimeMillis() - start;
  14. System.out.println("cost:"+cost);
  15. }
  16. }

</>复制代码

  1. 异常日志如下:

</>复制代码

  1. cost:75814
  2. java.net.ConnectException: Operation timed out
  3. at java.net.http/jdk.internal.net.http.HttpClientImpl.send(HttpClientImpl.java:561)
  4. at java.net.http/jdk.internal.net.http.HttpClientFacade.send(HttpClientFacade.java:119)
  5. at com.example.HttpClientTest.testConnectTimeout(HttpClientTest.java:464)
  6. at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  7. at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  8. at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  9. at java.base/java.lang.reflect.Method.invoke(Method.java:566)
  10. at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
  11. at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
  12. at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
  13. at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
  14. at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
  15. at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
  16. at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
  17. at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
  18. at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
  19. at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
  20. at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
  21. at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
  22. at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
  23. at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
  24. at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
  25. at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
  26. at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
  27. at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
  28. at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  29. at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  30. at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  31. at java.base/java.lang.reflect.Method.invoke(Method.java:566)
  32. at com.intellij.rt.execution.application.AppMainV2.main(AppMainV2.java:131)
  33. Caused by: java.net.ConnectException: Operation timed out
  34. at java.base/sun.nio.ch.SocketChannelImpl.checkConnect(Native Method)
  35. at java.base/sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:779)
  36. at java.net.http/jdk.internal.net.http.PlainHttpConnection$ConnectEvent.handle(PlainHttpConnection.java:128)
  37. at java.net.http/jdk.internal.net.http.HttpClientImpl$SelectorManager.handleEvent(HttpClientImpl.java:957)
  38. at java.net.http/jdk.internal.net.http.HttpClientImpl$SelectorManager.lambda$run$3(HttpClientImpl.java:912)
  39. at java.base/java.util.ArrayList.forEach(ArrayList.java:1540)
  40. at java.net.http/jdk.internal.net.http.HttpClientImpl$SelectorManager.run(HttpClientImpl.java:912)
Exchange.responseAsync

java.net.http/jdk/internal/net/http/Exchange.java

</>复制代码

  1. public CompletableFuture responseAsync() {
  2. return responseAsyncImpl(null);
  3. }
  4. CompletableFuture responseAsyncImpl(HttpConnection connection) {
  5. SecurityException e = checkPermissions();
  6. if (e != null) {
  7. return MinimalFuture.failedFuture(e);
  8. } else {
  9. return responseAsyncImpl0(connection);
  10. }
  11. }
  12. CompletableFuture responseAsyncImpl0(HttpConnection connection) {
  13. Function, CompletableFuture> after407Check;
  14. bodyIgnored = null;
  15. if (request.expectContinue()) {
  16. request.addSystemHeader("Expect", "100-Continue");
  17. Log.logTrace("Sending Expect: 100-Continue");
  18. // wait for 100-Continue before sending body
  19. after407Check = this::expectContinue;
  20. } else {
  21. // send request body and proceed.
  22. after407Check = this::sendRequestBody;
  23. }
  24. // The ProxyAuthorizationRequired can be triggered either by
  25. // establishExchange (case of HTTP/2 SSL tunneling through HTTP/1.1 proxy
  26. // or by sendHeaderAsync (case of HTTP/1.1 SSL tunneling through HTTP/1.1 proxy
  27. // Therefore we handle it with a call to this checkFor407(...) after these
  28. // two places.
  29. Function, CompletableFuture> afterExch407Check =
  30. (ex) -> ex.sendHeadersAsync()
  31. .handle((r,t) -> this.checkFor407(r, t, after407Check))
  32. .thenCompose(Function.identity());
  33. return establishExchange(connection)
  34. .handle((r,t) -> this.checkFor407(r,t, afterExch407Check))
  35. .thenCompose(Function.identity());
  36. }
  37. // get/set the exchange impl, solving race condition issues with
  38. // potential concurrent calls to cancel() or cancel(IOException)
  39. private CompletableFuture>
  40. establishExchange(HttpConnection connection) {
  41. if (debug.on()) {
  42. debug.log("establishing exchange for %s,%n
  43. proxy=%s",
  44. request, request.proxy());
  45. }
  46. // check if we have been cancelled first.
  47. Throwable t = getCancelCause();
  48. checkCancelled();
  49. if (t != null) {
  50. return MinimalFuture.failedFuture(t);
  51. }
  52. CompletableFuture> cf, res;
  53. cf = ExchangeImpl.get(this, connection);
  54. // We should probably use a VarHandle to get/set exchangeCF
  55. // instead - as we need CAS semantics.
  56. synchronized (this) { exchangeCF = cf; };
  57. res = cf.whenComplete((r,x) -> {
  58. synchronized(Exchange.this) {
  59. if (exchangeCF == cf) exchangeCF = null;
  60. }
  61. });
  62. checkCancelled();
  63. return res.thenCompose((eimpl) -> {
  64. // recheck for cancelled, in case of race conditions
  65. exchImpl = eimpl;
  66. IOException tt = getCancelCause();
  67. checkCancelled();
  68. if (tt != null) {
  69. return MinimalFuture.failedFuture(tt);
  70. } else {
  71. // Now we"re good to go. Because exchImpl is no longer
  72. // null cancel() will be able to propagate directly to
  73. // the impl after this point ( if needed ).
  74. return MinimalFuture.completedFuture(eimpl);
  75. } });
  76. }

responseAsync最后调用ExchangeImpl.get(this, connection)

ExchangeImpl.get

java.net.http/jdk/internal/net/http/ExchangeImpl.java

</>复制代码

  1. /**
  2. * Initiates a new exchange and assigns it to a connection if one exists
  3. * already. connection usually null.
  4. */
  5. static CompletableFuture>
  6. get(Exchange exchange, HttpConnection connection)
  7. {
  8. if (exchange.version() == HTTP_1_1) {
  9. if (debug.on())
  10. debug.log("get: HTTP/1.1: new Http1Exchange");
  11. return createHttp1Exchange(exchange, connection);
  12. } else {
  13. Http2ClientImpl c2 = exchange.client().client2(); // #### improve
  14. HttpRequestImpl request = exchange.request();
  15. CompletableFuture c2f = c2.getConnectionFor(request, exchange);
  16. if (debug.on())
  17. debug.log("get: Trying to get HTTP/2 connection");
  18. return c2f.handle((h2c, t) -> createExchangeImpl(h2c, t, exchange, connection))
  19. .thenCompose(Function.identity());
  20. }
  21. }

这里调用Http2ClientImpl.getConnectionFor获取连接

Http2ClientImpl.getConnectionFor

java.net.http/jdk/internal/net/http/Http2ClientImpl.java

</>复制代码

  1. /**
  2. * When HTTP/2 requested only. The following describes the aggregate behavior including the
  3. * calling code. In all cases, the HTTP2 connection cache
  4. * is checked first for a suitable connection and that is returned if available.
  5. * If not, a new connection is opened, except in https case when a previous negotiate failed.
  6. * In that case, we want to continue using http/1.1. When a connection is to be opened and
  7. * if multiple requests are sent in parallel then each will open a new connection.
  8. *
  9. * If negotiation/upgrade succeeds then
  10. * one connection will be put in the cache and the others will be closed
  11. * after the initial request completes (not strictly necessary for h2, only for h2c)
  12. *
  13. * If negotiate/upgrade fails, then any opened connections remain open (as http/1.1)
  14. * and will be used and cached in the http/1 cache. Note, this method handles the
  15. * https failure case only (by completing the CF with an ALPN exception, handled externally)
  16. * The h2c upgrade is handled externally also.
  17. *
  18. * Specific CF behavior of this method.
  19. * 1. completes with ALPN exception: h2 negotiate failed for first time. failure recorded.
  20. * 2. completes with other exception: failure not recorded. Caller must handle
  21. * 3. completes normally with null: no connection in cache for h2c or h2 failed previously
  22. * 4. completes normally with connection: h2 or h2c connection in cache. Use it.
  23. */
  24. CompletableFuture getConnectionFor(HttpRequestImpl req,
  25. Exchange exchange) {
  26. URI uri = req.uri();
  27. InetSocketAddress proxy = req.proxy();
  28. String key = Http2Connection.keyFor(uri, proxy);
  29. synchronized (this) {
  30. Http2Connection connection = connections.get(key);
  31. if (connection != null) {
  32. try {
  33. if (connection.closed || !connection.reserveStream(true)) {
  34. if (debug.on())
  35. debug.log("removing found closed or closing connection: %s", connection);
  36. deleteConnection(connection);
  37. } else {
  38. // fast path if connection already exists
  39. if (debug.on())
  40. debug.log("found connection in the pool: %s", connection);
  41. return MinimalFuture.completedFuture(connection);
  42. }
  43. } catch (IOException e) {
  44. // thrown by connection.reserveStream()
  45. return MinimalFuture.failedFuture(e);
  46. }
  47. }
  48. if (!req.secure() || failures.contains(key)) {
  49. // secure: negotiate failed before. Use http/1.1
  50. // !secure: no connection available in cache. Attempt upgrade
  51. if (debug.on()) debug.log("not found in connection pool");
  52. return MinimalFuture.completedFuture(null);
  53. }
  54. }
  55. return Http2Connection
  56. .createAsync(req, this, exchange)
  57. .whenComplete((conn, t) -> {
  58. synchronized (Http2ClientImpl.this) {
  59. if (conn != null) {
  60. try {
  61. conn.reserveStream(true);
  62. } catch (IOException e) {
  63. throw new UncheckedIOException(e); // shouldn"t happen
  64. }
  65. offerConnection(conn);
  66. } else {
  67. Throwable cause = Utils.getCompletionCause(t);
  68. if (cause instanceof Http2Connection.ALPNException)
  69. failures.add(key);
  70. }
  71. }
  72. });
  73. }

如果没有连接会新创建一个,走的是Http2Connection.createAsync

Http2Connection.createAsync

java.net.http/jdk/internal/net/http/Http2Connection.java

</>复制代码

  1. // Requires TLS handshake. So, is really async
  2. static CompletableFuture createAsync(HttpRequestImpl request,
  3. Http2ClientImpl h2client,
  4. Exchange exchange) {
  5. assert request.secure();
  6. AbstractAsyncSSLConnection connection = (AbstractAsyncSSLConnection)
  7. HttpConnection.getConnection(request.getAddress(),
  8. h2client.client(),
  9. request,
  10. HttpClient.Version.HTTP_2);
  11. // Expose the underlying connection to the exchange"s aborter so it can
  12. // be closed if a timeout occurs.
  13. exchange.connectionAborter.connection(connection);
  14. return connection.connectAsync(exchange)
  15. .thenCompose(unused -> connection.finishConnect())
  16. .thenCompose(unused -> checkSSLConfig(connection))
  17. .thenCompose(notused-> {
  18. CompletableFuture cf = new MinimalFuture<>();
  19. try {
  20. Http2Connection hc = new Http2Connection(request, h2client, connection);
  21. cf.complete(hc);
  22. } catch (IOException e) {
  23. cf.completeExceptionally(e);
  24. }
  25. return cf; } );
  26. }

这里先是调用了HttpConnection.getConnection获取连接,然后调用connectAsync进行连接

AsyncSSLConnection

java.net.http/jdk/internal/net/http/AsyncSSLConnection.java

</>复制代码

  1. @Override
  2. public CompletableFuture connectAsync(Exchange exchange) {
  3. return plainConnection
  4. .connectAsync(exchange)
  5. .thenApply( unused -> {
  6. // create the SSLTube wrapping the SocketTube, with the given engine
  7. flow = new SSLTube(engine,
  8. client().theExecutor(),
  9. client().getSSLBufferSupplier()::recycle,
  10. plainConnection.getConnectionFlow());
  11. return null; } );
  12. }

这里委托给plainConnection.connectAsync

PlainHttpConnection.connectAsync

java.net.http/jdk/internal/net/http/PlainHttpConnection.java

</>复制代码

  1. @Override
  2. public CompletableFuture connectAsync(Exchange exchange) {
  3. CompletableFuture cf = new MinimalFuture<>();
  4. try {
  5. assert !connected : "Already connected";
  6. assert !chan.isBlocking() : "Unexpected blocking channel";
  7. boolean finished;
  8. connectTimerEvent = newConnectTimer(exchange, cf);
  9. if (connectTimerEvent != null) {
  10. if (debug.on())
  11. debug.log("registering connect timer: " + connectTimerEvent);
  12. client().registerTimer(connectTimerEvent);
  13. }
  14. PrivilegedExceptionAction pa =
  15. () -> chan.connect(Utils.resolveAddress(address));
  16. try {
  17. finished = AccessController.doPrivileged(pa);
  18. } catch (PrivilegedActionException e) {
  19. throw e.getCause();
  20. }
  21. if (finished) {
  22. if (debug.on()) debug.log("connect finished without blocking");
  23. cf.complete(null);
  24. } else {
  25. if (debug.on()) debug.log("registering connect event");
  26. client().registerEvent(new ConnectEvent(cf));
  27. }
  28. } catch (Throwable throwable) {
  29. cf.completeExceptionally(Utils.toConnectException(throwable));
  30. try {
  31. close();
  32. } catch (Exception x) {
  33. if (debug.on())
  34. debug.log("Failed to close channel after unsuccessful connect");
  35. }
  36. }
  37. return cf;
  38. }

这里如果client有设置connectTimeout的话,则会创建一个connectTimerEvent

调用chan.connect进行连接,如果连接未完成,则注册ConnectEvent

SocketChannelImpl.connect

java.base/sun/nio/ch/SocketChannelImpl.java

</>复制代码

  1. @Override
  2. public boolean connect(SocketAddress sa) throws IOException {
  3. InetSocketAddress isa = Net.checkAddress(sa);
  4. SecurityManager sm = System.getSecurityManager();
  5. if (sm != null)
  6. sm.checkConnect(isa.getAddress().getHostAddress(), isa.getPort());
  7. InetAddress ia = isa.getAddress();
  8. if (ia.isAnyLocalAddress())
  9. ia = InetAddress.getLocalHost();
  10. try {
  11. readLock.lock();
  12. try {
  13. writeLock.lock();
  14. try {
  15. int n = 0;
  16. boolean blocking = isBlocking();
  17. try {
  18. beginConnect(blocking, isa);
  19. do {
  20. n = Net.connect(fd, ia, isa.getPort());
  21. } while (n == IOStatus.INTERRUPTED && isOpen());
  22. } finally {
  23. endConnect(blocking, (n > 0));
  24. }
  25. assert IOStatus.check(n);
  26. return n > 0;
  27. } finally {
  28. writeLock.unlock();
  29. }
  30. } finally {
  31. readLock.unlock();
  32. }
  33. } catch (IOException ioe) {
  34. // connect failed, close the channel
  35. close();
  36. throw SocketExceptions.of(ioe, isa);
  37. }
  38. }

通过Net.connect调用本地方法进行连接

ConnectEvent

java.net.http/jdk/internal/net/http/PlainHttpConnection.java

</>复制代码

  1. final class ConnectEvent extends AsyncEvent {
  2. private final CompletableFuture cf;
  3. ConnectEvent(CompletableFuture cf) {
  4. this.cf = cf;
  5. }
  6. @Override
  7. public SelectableChannel channel() {
  8. return chan;
  9. }
  10. @Override
  11. public int interestOps() {
  12. return SelectionKey.OP_CONNECT;
  13. }
  14. @Override
  15. public void handle() {
  16. try {
  17. assert !connected : "Already connected";
  18. assert !chan.isBlocking() : "Unexpected blocking channel";
  19. if (debug.on())
  20. debug.log("ConnectEvent: finishing connect");
  21. boolean finished = chan.finishConnect();
  22. assert finished : "Expected channel to be connected";
  23. if (debug.on())
  24. debug.log("ConnectEvent: connect finished: %s Local addr: %s",
  25. finished, chan.getLocalAddress());
  26. // complete async since the event runs on the SelectorManager thread
  27. cf.completeAsync(() -> null, client().theExecutor());
  28. } catch (Throwable e) {
  29. Throwable t = Utils.toConnectException(e);
  30. client().theExecutor().execute( () -> cf.completeExceptionally(t));
  31. close();
  32. }
  33. }
  34. @Override
  35. public void abort(IOException ioe) {
  36. client().theExecutor().execute( () -> cf.completeExceptionally(ioe));
  37. close();
  38. }
  39. }

SelectorManager对准备好的事件触发handle操作,对于ConnectEvent,就是调用ConnectEvent.handle

ConnectEvent的handle方法执行chan.finishConnect(),如果捕获到异常,则调用cf.completeExceptionally(t)

SocketChannelImpl.finishConnect

java.base/sun/nio/ch/SocketChannelImpl.java

</>复制代码

  1. @Override
  2. public boolean finishConnect() throws IOException {
  3. try {
  4. readLock.lock();
  5. try {
  6. writeLock.lock();
  7. try {
  8. // no-op if already connected
  9. if (isConnected())
  10. return true;
  11. boolean blocking = isBlocking();
  12. boolean connected = false;
  13. try {
  14. beginFinishConnect(blocking);
  15. int n = 0;
  16. if (blocking) {
  17. do {
  18. n = checkConnect(fd, true);
  19. } while ((n == 0 || n == IOStatus.INTERRUPTED) && isOpen());
  20. } else {
  21. n = checkConnect(fd, false);
  22. }
  23. connected = (n > 0);
  24. } finally {
  25. endFinishConnect(blocking, connected);
  26. }
  27. assert (blocking && connected) ^ !blocking;
  28. return connected;
  29. } finally {
  30. writeLock.unlock();
  31. }
  32. } finally {
  33. readLock.unlock();
  34. }
  35. } catch (IOException ioe) {
  36. // connect failed, close the channel
  37. close();
  38. throw SocketExceptions.of(ioe, remoteAddress);
  39. }
  40. }

checkConnect是一个本地方法,如果是连接超时,则抛出java.net.ConnectException: Operation timed out

tcp连接syn超时(net.ipv4.tcp_syn_retries)


当client端与server端建立连接,client发出syn包,如果等待一定时间没有收到server端发来的SYN+ACK,则会进行重试,重试次数由具体由net.ipv4.tcp_syn_retries决定

</>复制代码

  1. / # sysctl -a | grep tcp_syn_retries
  2. sysctl: error reading key "net.ipv6.conf.all.stable_secret": I/O error
  3. net.ipv4.tcp_syn_retries = 6
  4. sysctl: error reading key "net.ipv6.conf.default.stable_secret": I/O error
  5. sysctl: error reading key "net.ipv6.conf.eth0.stable_secret": I/O error
  6. sysctl: error reading key "net.ipv6.conf.lo.stable_secret": I/O error

linux默认是6次,第一次发送等待2^0秒没收到回包则重试第一次,之后等待2^1,以此类推,第六次重试等待2^6秒,因此一共是1s+2s+4s+8s+16s+32s+64s=127s,因而在linux平台下,如果httpclient没有设置connect timeout,则依赖系统tcp的syn超时,即127s之后超时,java的本地调用抛出java.net.ConnectException: Operation timed out

</>复制代码

  1. 如果是mac系统,根据Overriding the default Linux kernel 20-second TCP socket connect timeout的描述,超时是75s,与本实例代码输出的75814ms近似一致。
小结

使用jdk httpclient进行连接,如果没有设置client的connectTimeout,则具体的超时时间依赖系统的tcp相关设置

如果client端sync发送超时,则依赖tcp_syn_retries的配置来决定本地方法抛出java.net.ConnectException: Operation timed out异常的时间

linux下默认tcp_syn_retries默认为6,即重试6次,一共需要1s+2s+4s+8s+16s+32s+64s=127s,若再没有收到server端发来的SYN+ACK则抛出java.net.ConnectException: Operation timed out异常

doc

TCP协议的那些超时

Linux 建立 TCP 连接的超时时间分析

Overriding the default Linux kernel 20-second TCP socket connect timeout

设置linux中tcp默认的20秒connect超时时间

SYN丢包的几个例子

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/77351.html

相关文章

  • 聊聊jdk httpclientretry参数

    摘要:序本文主要研究一下的参数这里有一个类型的变量,用来记录请求次数另外还有一个,读取的是值,读取不到默认取,为进入该方法的时候,调用,递增请求次数,然后判断有无超出限制,有则返回带有异常的,即通过返回如果没有超出限制,但是执行请求失败,则 序 本文主要研究一下jdk httpclient的retry参数 DEFAULT_MAX_ATTEMPTS java.net.http/jdk/inte...

    ityouknow 评论0 收藏0
  • 聊聊jdk httpclientConnectionPool

    摘要:调用计算的时间,这个方法会清理移除并过期的连接除了清理过期的连接外,还通过间接触发,去清理关闭或异常的连接 序 本文主要研究一下jdk httpclient的ConnectionPool HttpConnection HttpConnection.getConnection java.net.http/jdk/internal/net/http/HttpConnection.java ...

    Worktile 评论0 收藏0
  • [case39]聊聊jdk httpclientexecutor

    摘要:序本文主要研究一下的这里如果的为,则会创建这里如果是的话,参数传递的是如果是同步的方法,则传的值是这里创建了一个,然后调用,这里使用了可以看到这里使用的是的方法注意这个方法是才有的,也是在这里使用的由于默认是使用创建的, 序 本文主要研究一下jdk httpclient的executor HttpClientImpl java.net.http/jdk/internal/net/htt...

    dabai 评论0 收藏0
  • Java11 HttpClient小试牛刀

    序 本文主要研究一下Java11的HttpClient的基本使用。 变化 从java9的jdk.incubator.httpclient模块迁移到java.net.http模块,包名由jdk.incubator.http改为java.net.http 原来的诸如HttpResponse.BodyHandler.asString()方法变更为HttpResponse.BodyHandlers.of...

    Bmob 评论0 收藏0
  • 浅析 jdk11 中 HttpClient 使用

    摘要:在中也可以直接使用返回的是,然后通过来获取结果阻塞线程,从中获取结果四一点唠叨非常的年轻,网络资料不多,且代码非常精细和复杂,目前来看底层应该是使用了线程池搭配进行异步通讯。 零 前期准备 0 版本 JDK 版本 : OpenJDK 11.0.1 IDE : idea 2018.3 1 HttpClient 简介 java.net.http.HttpClient 是 jdk11 中正式...

    Eminjannn 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<