{"id":10021077,"date":"2025-11-14T18:41:48","date_gmt":"2025-11-14T10:41:48","guid":{"rendered":"https:\/\/itmax.cn\/?p=10021077"},"modified":"2025-11-14T18:41:48","modified_gmt":"2025-11-14T10:41:48","slug":"%e6%8e%a2%e7%b4%a2-java-%e4%b8%ad%e7%9a%84%e6%96%b0-http-%e5%ae%a2%e6%88%b7%e7%ab%af","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10021077","title":{"rendered":"\u63a2\u7d22 Java \u4e2d\u7684\u65b0 HTTP \u5ba2\u6237\u7aef"},"content":{"rendered":"<p>\u4f60\u662f\u5426\u4e5f\u9047\u5230\u8fc7\u8fd9\u6837\u7684\u65f6\u523b\uff1a\u53ea\u662f\u60f3\u53d1\u4e2a HTTP \u8bf7\u6c42\uff0c\u5374\u88ab\u8fde\u63a5\u7ba1\u7406\u3001\u91cd\u5b9a\u5411\u3001\u8d85\u65f6\u4e0e\u7ebf\u7a0b\u963b\u585e\u6298\u817e\u5f97\u4e0d\u4ea6\u4e50\u4e4e\uff1f\u90a3\u5c31\u8bd5\u8bd5 Java 11 \u6b63\u5f0f\u6807\u51c6\u5316\u4e86\u5168\u65b0\u7684 HttpClient\uff0c\u539f\u751f\u652f\u6301 HTTP\/2\u3001\u5f02\u6b65\u4e0e WebSocket\uff0c\u6781\u5927\u7b80\u5316\u4e86\u5ba2\u6237\u7aef\u7f51\u7edc\u7f16\u7a0b\u3002<\/p>\n<h2>1. \u6982\u89c8<\/h2>\n<p>\u672c\u6587\u5c06\u4ecb\u7ecd Java 11 \u5bf9\u5168\u65b0 <strong>HTTP \u5ba2\u6237\u7aef API\uff08\u652f\u6301 HTTP\/2 \u4e0e WebSocket\uff09<\/strong> \u7684\u6807\u51c6\u5316\u3002<\/p>\n<p>\u5b83\u65e8\u5728\u66ff\u4ee3 JDK \u65e9\u671f\u5c31\u5b58\u5728\u7684\u65e7\u7c7b <code>HttpURLConnection<\/code>\uff08\u6587\u6863\u89c1\uff1a<a href=\"https:\/\/link.segmentfault.com\/?enc=0PliW2jrZGWaV41rLTDmug%3D%3D.KNkFLLfZk4CtD8wXOlugxx%2BK28h0DOmxGWXEZUwBcyDkKR1Xog3CZHKYCIReWd6JXrCFjo7GSkVx5%2BhuggWi0t5Po%2F7ardML9uWW1eef1SDnvhdEjYhAa4BuOTNcYoCT\" rel=\"nofollow noopener noreferrer\" target=\"_blank\">https:\/\/docs.oracle.com\/en\/java\/javase\/21\/docs\/api\/java.base\/&#8230;<\/a>\uff09\u3002<\/p>\n<p>\u5728\u4e0d\u4e45\u4e4b\u524d\uff0cJava \u53ea\u6709\u8f83\u4e3a\u5e95\u5c42\u3001\u529f\u80fd\u6709\u9650\u4e14\u4e0d\u591f\u53cb\u597d\u7684 <code>HttpURLConnection<\/code> API\u3002\u56e0\u6b64\u793e\u533a\u666e\u904d\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\uff0c\u5982 <a href=\"https:\/\/link.segmentfault.com\/?enc=pSrqW8sGvA6WryToUu7fAw%3D%3D.YpJoldtNUOxd40Ux6wWlDwOQlnMH24CF7QO8Dx%2FGqBS2VgqyHlno8iX%2FmvZt4vnU\" rel=\"nofollow noopener noreferrer\" target=\"_blank\">Apache HttpClient<\/a>\u3001<a href=\"https:\/\/link.segmentfault.com\/?enc=EtlIU8lgCUluXVwvh978Uw%3D%3D.H%2BA2vSW7twlSf2%2FDTx%2B6jbzPnSRZdv7hI6F5ErTKpHzwzF9vuLFoi50BzwLB9hamRPnpw2q2alU1nqICHCH3%2FdKWoSrOywMTxtA6ARIZtU0%3D\" rel=\"nofollow noopener noreferrer\" target=\"_blank\">Jetty<\/a> \u4ee5\u53ca Spring \u7684 RestTemplate\u3002<\/p>\n<h2>2. \u80cc\u666f<\/h2>\n<p>\u8be5\u53d8\u66f4\u7531 JEP 321 \u5f15\u5165\u5e76\u6700\u7ec8\u5728 Java 11 \u4e2d\u5b9a\u578b\u3002<\/p>\n<h3>2.1. JEP 321 \u7684\u4e3b\u8981\u53d8\u66f4<\/h3>\n<ol>\n<li>Java 9 \u7684\u5b75\u5316\u7248 HTTP API \u5df2\u6b63\u5f0f\u5e76\u5165 Java SE API\u3002\u65b0\u7684 <a href=\"https:\/\/link.segmentfault.com\/?enc=N2ob6LrwwKZryb4rMGdakQ%3D%3D.O4OCWzqMFSEKm%2FO6wGA6515NTHCl6KVnTvL0gIw8Oo%2BpWeF3kYOx%2BL3rUO0mTZqCIZuxy1fxbdqrM14dNNIR%2Bw%2BcHnbM3zAKyx70tjEokIDWsn3qfFLUqU%2FEjDDmLg1F4Qy1hWqGzxg17mg3FdaxHw%3D%3D\" rel=\"nofollow noopener noreferrer\" target=\"_blank\">HTTP APIs<\/a> \u4f4d\u4e8e <code>java.net.http.*<\/code>\u3002<\/li>\n<li>\u65b0\u7248\u672c\u7684 HTTP \u534f\u8bae\u65e8\u5728\u63d0\u5347\u5ba2\u6237\u7aef\u8bf7\u6c42\u4e0e\u670d\u52a1\u5668\u54cd\u5e94\u7684\u6574\u4f53\u6027\u80fd\uff0c\u5305\u62ec\u591a\u8def\u590d\u7528\u3001\u5934\u538b\u7f29\u4e0e\u63a8\u9001\u627f\u8bfa\uff08push promise\uff09\u7b49\u7279\u6027\u3002<\/li>\n<li>\u81ea Java 11 \u8d77\uff0c<strong>API \u5168\u9762\u652f\u6301\u5f02\u6b65<\/strong>\uff08\u76f8\u6bd4\u4e4b\u4e0b\uff0c\u65e7\u7684 HTTP\/1.1 \u5b9e\u73b0\u662f\u963b\u585e\u5f0f\u7684\uff09\u3002\u5f02\u6b65\u4ee5 <code>CompletableFuture<\/code> \u5b9e\u73b0\uff0c\u9636\u6bb5\u5f0f\u6d41\u6c34\u7ebf\u5728\u524d\u4e00\u9636\u6bb5\u5b8c\u6210\u540e\u81ea\u52a8\u8854\u63a5\u6267\u884c\u3002<\/li>\n<li>\u65b0\u7684 HTTP \u5ba2\u6237\u7aef\u63d0\u4f9b\u4e86\u6807\u51c6\u65b9\u5f0f\u6267\u884c\u7f51\u7edc\u64cd\u4f5c\uff0c\u539f\u751f\u652f\u6301\u73b0\u4ee3 Web \u80fd\u529b\uff08\u5982 HTTP\/2\uff09\uff0c\u65e0\u9700\u5f15\u5165\u7b2c\u4e09\u65b9\u4f9d\u8d56\u3002<\/li>\n<li>\n<p>\u65b0 API \u539f\u751f\u652f\u6301 HTTP\/1.1 \u4e0e HTTP\/2 \u7684 WebSocket\u3002\u6838\u5fc3\u7c7b\u578b\u5305\u62ec\uff1a<\/p>\n<ul>\n<li><code>HttpClient<\/code>\uff08<code>java.net.http.HttpClient<\/code>\uff09<\/li>\n<li><code>HttpRequest<\/code>\uff08<code>java.net.http.HttpRequest<\/code>\uff09<\/li>\n<li><code>HttpResponse&lt;T&gt;<\/code>\uff08<code>java.net.http.HttpResponse<\/code>\uff09<\/li>\n<li><code>WebSocket<\/code>\uff08<code>java.net.http.WebSocket<\/code>\uff09<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<h3>2.2. Java 11 \u4e4b\u524d\u5ba2\u6237\u7aef\u7684\u95ee\u9898<\/h3>\n<p>\u65e7\u7248 <code>HttpURLConnection<\/code> \u53ca\u5176\u5b9e\u73b0\u5b58\u5728\u8bf8\u591a\u95ee\u9898\uff1a<\/p>\n<ul>\n<li><code>URLConnection<\/code> \u4e3a\u591a\u4e2a\u5982\u4eca\u5df2\u4e0d\u518d\u4f7f\u7528\u7684\u534f\u8bae\uff08FTP\u3001gopher \u7b49\uff09\u800c\u8bbe\u8ba1\uff1b<\/li>\n<li>API \u65e9\u4e8e HTTP\/1.1\uff0c\u62bd\u8c61\u5c42\u7ea7\u4e0d\u5408\u65f6\u5b9c\uff1b<\/li>\n<li>\u4ec5\u652f\u6301\u963b\u585e\u6a21\u5f0f\uff08\u4e00\u6b21\u8bf7\u6c42\/\u54cd\u5e94\u5360\u7528\u4e00\u4e2a\u7ebf\u7a0b\uff09\uff1b<\/li>\n<li>\u7ef4\u62a4\u56f0\u96be\u3002<\/li>\n<\/ul>\n<h2>3. HTTP Client API \u603b\u89c8<\/h2>\n<p>\u4e0e <code>HttpURLConnection<\/code> \u4e0d\u540c\uff0c\u65b0 HTTP \u5ba2\u6237\u7aef\u540c\u65f6\u63d0\u4f9b\u540c\u6b65\u4e0e\u5f02\u6b65\u4e24\u79cd\u8bf7\u6c42\u673a\u5236\u3002<\/p>\n<p>API \u7684\u4e09\u5927\u6838\u5fc3\uff1a<\/p>\n<ul>\n<li><code>HttpRequest<\/code>\uff1a\u8981\u53d1\u9001\u7684\u8bf7\u6c42\uff1b<\/li>\n<li><code>HttpClient<\/code>\uff1a\u8de8\u8bf7\u6c42\u7684\u901a\u7528\u914d\u7f6e\u5bb9\u5668\uff1b<\/li>\n<li><code>HttpResponse<\/code>\uff1a\u8bf7\u6c42\u7684\u54cd\u5e94\u7ed3\u679c\u3002<\/li>\n<\/ul>\n<p>\u4e0b\u9762\u5206\u522b\u5c55\u5f00\uff0c\u5148\u4ece\u8bf7\u6c42\u5f00\u59cb\u3002<\/p>\n<h2>4. HttpRequest<\/h2>\n<p><code>HttpRequest<\/code> \u8868\u793a\u5c06\u8981\u53d1\u9001\u7684\u8bf7\u6c42\uff0c\u53ef\u901a\u8fc7 <code>HttpRequest.newBuilder()<\/code> \u83b7\u53d6\u6784\u5efa\u5668\uff1b\u6784\u5efa\u5668\u63d0\u4f9b\u591a\u79cd\u4fbf\u6377\u65b9\u6cd5\u914d\u7f6e\u8bf7\u6c42\u3002<\/p>\n<p>\u6ce8\uff1aJDK 16 \u65b0\u589e <code>HttpRequest.newBuilder(HttpRequest request, BiPredicate&lt;String,String&gt; filter)<\/code>\uff0c\u53ef\u57fa\u4e8e\u5df2\u6709\u8bf7\u6c42\u590d\u5236\u521d\u59cb\u72b6\u6001\uff0c\u518d\u5728\u6784\u5efa\u524d\u505a\u4fee\u6539\uff08\u5982\u79fb\u9664\u90e8\u5206\u5934\uff09\uff1a<\/p>\n<pre><code class=\"java\">HttpRequest.newBuilder(request, (name, value) -&gt; !name.equalsIgnoreCase(&quot;Foo-Bar&quot;));<\/code><\/pre>\n<h3>4.1. \u8bbe\u7f6e URI<\/h3>\n<p>\u53ef\u76f4\u63a5\u7528\u5e26 <code>URI<\/code> \u7684\u6784\u9020\u65b9\u5f0f\uff0c\u6216\u5728\u6784\u5efa\u5668\u4e0a\u8c03\u7528 <code>uri(URI)<\/code>\uff1a<\/p>\n<pre><code class=\"java\">HttpRequest.newBuilder(new URI(&quot;https:\/\/postman-echo.com\/get&quot;));\n\nHttpRequest.newBuilder()\n  .uri(new URI(&quot;https:\/\/postman-echo.com\/get&quot;));<\/code><\/pre>\n<h3>4.2. \u6307\u5b9a HTTP \u65b9\u6cd5<\/h3>\n<p>\u6784\u5efa\u5668\u63d0\u4f9b\u4ee5\u4e0b\u65b9\u6cd5\uff1a<\/p>\n<ul>\n<li><code>GET()<\/code><\/li>\n<li><code>POST(BodyPublisher body)<\/code><\/li>\n<li><code>PUT(BodyPublisher body)<\/code><\/li>\n<li><code>DELETE()<\/code><\/li>\n<\/ul>\n<p>\u4e00\u4e2a\u6700\u7b80\u5355\u7684 GET \u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"java\">HttpRequest request = HttpRequest.newBuilder()\n  .uri(new URI(&quot;https:\/\/postman-echo.com\/get&quot;))\n  .GET()\n  .build();<\/code><\/pre>\n<p>\u5e38\u89c1\u7684\u9644\u52a0\u53c2\u6570\u5305\u62ec\uff1aHTTP \u534f\u8bae\u7248\u672c\u3001\u8bf7\u6c42\u5934\u4e0e\u8d85\u65f6\u3002<\/p>\n<h3>4.3. \u8bbe\u7f6e\u534f\u8bae\u7248\u672c<\/h3>\n<p>API \u9ed8\u8ba4\u5145\u5206\u5229\u7528 HTTP\/2\uff0c\u4e5f\u53ef\u663e\u5f0f\u6307\u5b9a\uff1a<\/p>\n<pre><code class=\"java\">HttpRequest request = HttpRequest.newBuilder()\n  .uri(new URI(&quot;https:\/\/postman-echo.com\/get&quot;))\n  .version(HttpClient.Version.HTTP_2)\n  .GET()\n  .build();<\/code><\/pre>\n<p>\u6ce8\u610f\uff1a\u82e5\u5bf9\u7aef\u4e0d\u652f\u6301 HTTP\/2\uff0c\u5ba2\u6237\u7aef\u4f1a\u56de\u9000\u5230 HTTP\/1.1\u3002<\/p>\n<h3>4.4. \u8bbe\u7f6e\u8bf7\u6c42\u5934<\/h3>\n<p>\u53ef\u7528 <code>headers(k1,v1,k2,v2,...)<\/code> \u4e00\u6b21\u6027\u4f20\u5165\uff0c\u6216\u591a\u6b21\u8c03\u7528 <code>header(k,v)<\/code>\uff1a<\/p>\n<pre><code class=\"java\">HttpRequest request = HttpRequest.newBuilder()\n  .uri(new URI(&quot;https:\/\/postman-echo.com\/get&quot;))\n  .headers(&quot;key1&quot;, &quot;value1&quot;, &quot;key2&quot;, &quot;value2&quot;)\n  .GET()\n  .build();\n\nHttpRequest request2 = HttpRequest.newBuilder()\n  .uri(new URI(&quot;https:\/\/postman-echo.com\/get&quot;))\n  .header(&quot;key1&quot;, &quot;value1&quot;)\n  .header(&quot;key2&quot;, &quot;value2&quot;)\n  .GET()\n  .build();<\/code><\/pre>\n<h3>4.5. \u8bbe\u7f6e\u8d85\u65f6<\/h3>\n<p>\u9ed8\u8ba4\u65e0\u7a77\u5927\u3002\u53ef\u7528 <code>Duration<\/code> \u8bbe\u7f6e\uff0c\u8d85\u65f6\u4f1a\u629b\u51fa <code>HttpTimeoutException<\/code>\uff1a<\/p>\n<pre><code class=\"java\">HttpRequest request = HttpRequest.newBuilder()\n  .uri(new URI(&quot;https:\/\/postman-echo.com\/get&quot;))\n  .timeout(Duration.ofSeconds(10))\n  .GET()\n  .build();<\/code><\/pre>\n<h2>5. \u8bbe\u7f6e\u8bf7\u6c42\u4f53<\/h2>\n<p><code>POST(BodyPublisher)<\/code>, <code>PUT(BodyPublisher)<\/code> \u53ef\u643a\u5e26\u8bf7\u6c42\u4f53\uff08<code>DELETE()<\/code> \u4e5f\u652f\u6301\u4e0d\u5e26\u4f53\u7684\u5220\u9664\uff09\u3002\u5e38\u7528\u7684 <code>BodyPublisher<\/code> \u5de5\u5382\u6709\uff1a<\/p>\n<ul>\n<li><code>HttpRequest.BodyPublishers.ofString<\/code>\uff1a\u57fa\u4e8e\u5b57\u7b26\u4e32\uff1b<\/li>\n<li><code>HttpRequest.BodyPublishers.ofInputStream<\/code>\uff1a\u57fa\u4e8e\u8f93\u5165\u6d41\uff08\u4ee5 <code>Supplier&lt;InputStream&gt;<\/code> \u5f62\u5f0f\u5ef6\u8fdf\u521b\u5efa\uff09\uff1b<\/li>\n<li><code>HttpRequest.BodyPublishers.ofByteArray<\/code>\uff1a\u57fa\u4e8e\u5b57\u8282\u6570\u7ec4\uff1b<\/li>\n<li><code>HttpRequest.BodyPublishers.ofFile<\/code>\uff1a\u57fa\u4e8e\u6587\u4ef6\u8def\u5f84\u5185\u5bb9\uff1b<\/li>\n<li>\u65e0\u8bf7\u6c42\u4f53\uff1a<code>HttpRequest.BodyPublishers.noBody()<\/code>\u3002<\/li>\n<\/ul>\n<p>JDK 16 \u65b0\u589e <code>BodyPublishers.concat(...)<\/code>\uff0c\u53ef\u628a\u591a\u4e2a publisher \u7684\u5185\u5bb9\u987a\u5e8f\u62fc\u63a5\u4e3a\u4e00\u4e2a\u8bf7\u6c42\u4f53\u3002<\/p>\n<h3>5.1. \u5b57\u7b26\u4e32\u8bf7\u6c42\u4f53<\/h3>\n<pre><code class=\"java\">HttpRequest request = HttpRequest.newBuilder()\n  .uri(new URI(&quot;https:\/\/postman-echo.com\/post&quot;))\n  .headers(&quot;Content-Type&quot;, &quot;text\/plain;charset=UTF-8&quot;)\n  .POST(HttpRequest.BodyPublishers.ofString(&quot;Sample request body&quot;))\n  .build();<\/code><\/pre>\n<h3>5.2. \u8f93\u5165\u6d41\u8bf7\u6c42\u4f53<\/h3>\n<pre><code class=\"java\">byte[] sampleData = &quot;Sample request body&quot;.getBytes();\nHttpRequest request = HttpRequest.newBuilder()\n  .uri(new URI(&quot;https:\/\/postman-echo.com\/post&quot;))\n  .headers(&quot;Content-Type&quot;, &quot;text\/plain;charset=UTF-8&quot;)\n  .POST(HttpRequest.BodyPublishers\n   .ofInputStream(() -&gt; new ByteArrayInputStream(sampleData)))\n  .build();<\/code><\/pre>\n<h3>5.3. \u5b57\u8282\u6570\u7ec4\u8bf7\u6c42\u4f53<\/h3>\n<pre><code class=\"java\">byte[] sampleData = &quot;Sample request body&quot;.getBytes();\nHttpRequest request = HttpRequest.newBuilder()\n  .uri(new URI(&quot;https:\/\/postman-echo.com\/post&quot;))\n  .headers(&quot;Content-Type&quot;, &quot;text\/plain;charset=UTF-8&quot;)\n  .POST(HttpRequest.BodyPublishers.ofByteArray(sampleData))\n  .build();<\/code><\/pre>\n<h3>5.4. \u6587\u4ef6\u8bf7\u6c42\u4f53<\/h3>\n<pre><code class=\"java\">HttpRequest request = HttpRequest.newBuilder()\n  .uri(new URI(&quot;https:\/\/postman-echo.com\/post&quot;))\n  .headers(&quot;Content-Type&quot;, &quot;text\/plain;charset=UTF-8&quot;)\n  .POST(HttpRequest.BodyPublishers.ofFile(\n    Paths.get(&quot;src\/test\/resources\/sample.txt&quot;)))\n  .build();<\/code><\/pre>\n<h2>6. HttpClient<\/h2>\n<p>\u6240\u6709\u8bf7\u6c42\u90fd\u7531 <code>HttpClient<\/code> \u53d1\u9001\uff0c\u53ef\u901a\u8fc7 <code>HttpClient.newBuilder()<\/code> \u6216 <code>HttpClient.newHttpClient()<\/code> \u83b7\u53d6\u3002\u4e0b\u9762\u770b\u51e0\u4e2a\u5e38\u7528\u80fd\u529b\u3002<\/p>\n<h3>6.1. \u5904\u7406\u54cd\u5e94\u4f53<\/h3>\n<p>\u65b0\u7684 <code>BodyHandlers<\/code> \u5de5\u5382\u63d0\u4f9b\u5e38\u89c1\u7c7b\u578b\u7684\u54cd\u5e94\u4f53\u5904\u7406\u5668\uff1a<\/p>\n<pre><code class=\"java\">BodyHandlers.ofByteArray;\nBodyHandlers.ofString;\nBodyHandlers.ofFile;\nBodyHandlers.discarding;\nBodyHandlers.replacing;\nBodyHandlers.ofLines;\nBodyHandlers.fromLineSubscriber;<\/code><\/pre>\n<p>Java 11 \u4e4b\u524d\uff1a<\/p>\n<pre><code class=\"java\">HttpResponse&lt;String&gt; response = client.send(request, HttpResponse.BodyHandler.asString());<\/code><\/pre>\n<p>\u73b0\u5728\u53ef\u7b80\u5316\u4e3a\uff1a<\/p>\n<pre><code class=\"java\">HttpResponse&lt;String&gt; response = client.send(request, BodyHandlers.ofString());<\/code><\/pre>\n<h3>6.2. \u8bbe\u7f6e\u4ee3\u7406<\/h3>\n<pre><code class=\"java\">HttpResponse&lt;String&gt; response = HttpClient\n  .newBuilder()\n  .proxy(ProxySelector.getDefault())\n  .build()\n  .send(request, BodyHandlers.ofString());<\/code><\/pre>\n<h3>6.3. \u8ddf\u968f\u91cd\u5b9a\u5411\u7b56\u7565<\/h3>\n<pre><code class=\"java\">HttpResponse&lt;String&gt; response = HttpClient.newBuilder()\n  .followRedirects(HttpClient.Redirect.ALWAYS)\n  .build()\n  .send(request, BodyHandlers.ofString());<\/code><\/pre>\n<h3>6.4. \u8ba4\u8bc1\u5668\uff08Authenticator\uff09<\/h3>\n<pre><code class=\"java\">HttpResponse&lt;String&gt; response = HttpClient.newBuilder()\n  .authenticator(new Authenticator() {\n    @Override\n    protected PasswordAuthentication getPasswordAuthentication() {\n      return new PasswordAuthentication(\n        &quot;username&quot;,\n        &quot;password&quot;.toCharArray());\n    }\n  })\n  .build()\n  .send(request, BodyHandlers.ofString());<\/code><\/pre>\n<h3>6.5. \u540c\u6b65\u4e0e\u5f02\u6b65\u53d1\u9001<\/h3>\n<ul>\n<li>\u540c\u6b65\uff1a<code>send(...)<\/code>\uff08\u963b\u585e\u76f4\u5230\u54cd\u5e94\u8fd4\u56de\uff09<\/li>\n<li>\u5f02\u6b65\uff1a<code>sendAsync(...)<\/code>\uff08\u7acb\u5373\u8fd4\u56de <code>CompletableFuture&lt;HttpResponse&lt;T&gt;&gt;<\/code>\uff09<\/li>\n<\/ul>\n<p>\u540c\u6b65\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"java\">HttpResponse&lt;String&gt; response = HttpClient.newBuilder()\n  .build()\n  .send(request, BodyHandlers.ofString());<\/code><\/pre>\n<p>\u5f02\u6b65\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"java\">CompletableFuture&lt;HttpResponse&lt;String&gt;&gt; response = HttpClient.newBuilder()\n  .build()\n  .sendAsync(request, HttpResponse.BodyHandlers.ofString());<\/code><\/pre>\n<p>\u6279\u91cf\u5e76\u53d1\u8bf7\u6c42\uff1a<\/p>\n<pre><code class=\"java\">List&lt;URI&gt; targets = Arrays.asList(\n  new URI(&quot;https:\/\/postman-echo.com\/get?foo1=bar1&quot;),\n  new URI(&quot;https:\/\/postman-echo.com\/get?foo2=bar2&quot;));\nHttpClient client = HttpClient.newHttpClient();\nList&lt;CompletableFuture&lt;String&gt;&gt; futures = targets.stream()\n  .map(target -&gt; client\n    .sendAsync(\n      HttpRequest.newBuilder(target).GET().build(),\n      HttpResponse.BodyHandlers.ofString())\n    .thenApply(HttpResponse::body))\n  .collect(Collectors.toList());<\/code><\/pre>\n<h3>6.6. \u6307\u5b9a\u5f02\u6b65\u6267\u884c\u5668\uff08Executor\uff09<\/h3>\n<pre><code class=\"java\">ExecutorService executorService = Executors.newFixedThreadPool(2);\n\nCompletableFuture&lt;HttpResponse&lt;String&gt;&gt; response1 = HttpClient.newBuilder()\n  .executor(executorService)\n  .build()\n  .sendAsync(request, HttpResponse.BodyHandlers.ofString());\n\nCompletableFuture&lt;HttpResponse&lt;String&gt;&gt; response2 = HttpClient.newBuilder()\n  .executor(executorService)\n  .build()\n  .sendAsync(request, HttpResponse.BodyHandlers.ofString());<\/code><\/pre>\n<p>\u9ed8\u8ba4\u6267\u884c\u5668\u4e3a <code>Executors.newCachedThreadPool()<\/code>\u3002<\/p>\n<h3>6.7. CookieHandler<\/h3>\n<p>\u8bbe\u7f6e\u5ba2\u6237\u7aef\u7ea7 <code>CookieHandler<\/code>\uff1a<\/p>\n<pre><code class=\"java\">HttpClient.newBuilder()\n  .cookieHandler(new CookieManager(null, CookiePolicy.ACCEPT_NONE))\n  .build();<\/code><\/pre>\n<p>\u82e5\u5141\u8bb8\u5b58\u50a8 Cookie\uff0c\u53ef\u4ece <code>CookieManager<\/code> \u8bfb\u53d6\uff1a<\/p>\n<pre><code class=\"java\">((CookieManager) httpClient.cookieHandler().get()).getCookieStore();<\/code><\/pre>\n<h2>7. HttpResponse<\/h2>\n<p><code>HttpResponse<\/code> \u8868\u793a\u670d\u52a1\u7aef\u54cd\u5e94\uff0c\u6838\u5fc3\u65b9\u6cd5\uff1a<\/p>\n<ul>\n<li><code>statusCode()<\/code>\uff1a\u8fd4\u56de\u6574\u578b\u72b6\u6001\u7801\uff1b<\/li>\n<li><code>body()<\/code>\uff1a\u8fd4\u56de\u54cd\u5e94\u4f53\uff08\u7c7b\u578b\u53d6\u51b3\u4e8e\u53d1\u9001\u65f6\u7684 <code>BodyHandler<\/code>\uff09\u3002<\/li>\n<\/ul>\n<p>\u5176\u4ed6\u5e38\u7528\u65b9\u6cd5\u8fd8\u5305\u62ec <code>uri()<\/code>\u3001<code>headers()<\/code>\u3001<code>trailers()<\/code> \u4e0e <code>version()<\/code>\u3002<\/p>\n<h3>7.1. \u54cd\u5e94\u7684 URI<\/h3>\n<p>\u7531\u4e8e\u91cd\u5b9a\u5411\uff0c\u54cd\u5e94\u8fd4\u56de\u7684 <code>uri()<\/code> \u53ef\u80fd\u4e0e\u8bf7\u6c42\u4e0d\u540c\uff1a<\/p>\n<pre><code class=\"java\">assertThat(request.uri().toString(), equalTo(&quot;http:\/\/stackoverflow.com&quot;));\nassertThat(response.uri().toString(), equalTo(&quot;https:\/\/stackoverflow.com\/&quot;));<\/code><\/pre>\n<h3>7.2. \u54cd\u5e94\u5934<\/h3>\n<pre><code class=\"java\">HttpResponse&lt;String&gt; response = HttpClient.newHttpClient()\n  .send(request, HttpResponse.BodyHandlers.ofString());\nHttpHeaders responseHeaders = response.headers();<\/code><\/pre>\n<h3>7.3. \u54cd\u5e94\u534f\u8bae\u7248\u672c<\/h3>\n<p>\u5373\u4f7f\u8bf7\u6c42\u8bbe\u7f6e\u4e3a HTTP\/2\uff0c\u670d\u52a1\u7aef\u4e5f\u53ef\u80fd\u4ee5 HTTP\/1.1 \u54cd\u5e94\uff0c\u5b9e\u9645\u7248\u672c\u53ef\u4ece\u54cd\u5e94\u8bfb\u53d6\uff1a<\/p>\n<pre><code class=\"java\">HttpRequest request = HttpRequest.newBuilder()\n  .uri(new URI(&quot;https:\/\/postman-echo.com\/get&quot;))\n  .version(HttpClient.Version.HTTP_2)\n  .GET()\n  .build();\nHttpResponse&lt;String&gt; response = HttpClient.newHttpClient()\n  .send(request, HttpResponse.BodyHandlers.ofString());\nassertThat(response.version(), equalTo(HttpClient.Version.HTTP_1_1));<\/code><\/pre>\n<h2>8. HTTP\/2 \u63a8\u9001\u627f\u8bfa\uff08Push Promise\uff09<\/h2>\n<p>\u65b0\u7684 <code>HttpClient<\/code> \u901a\u8fc7 <code>PushPromiseHandler<\/code> \u652f\u6301\u670d\u52a1\u7aef\u4e3b\u52a8\u63a8\u9001\u3002\u5f53\u5ba2\u6237\u7aef\u8bf7\u6c42\u4e3b\u8d44\u6e90\u65f6\uff0c\u670d\u52a1\u5668\u53ef\u4ee5\u540c\u65f6\u201c\u63a8\u9001\u201d\u989d\u5916\u8d44\u6e90\uff0c\u4ece\u800c\u51cf\u5c11\u5f80\u8fd4\u6b21\u6570\u3001\u52a0\u5feb\u9875\u9762\u6e32\u67d3\u3002\u8be5\u80fd\u529b\u5f97\u76ca\u4e8e HTTP\/2 \u7684\u591a\u8def\u590d\u7528\u3002<\/p>\n<p>\u5982\u6709\u63a8\u9001\u627f\u8bfa\uff0c\u5c06\u7531\u63d0\u4f9b\u7684 <code>PushPromiseHandler<\/code> \u5904\u7406\uff1b\u82e5\u4f20\u5165 <code>null<\/code>\uff0c\u5219\u62d2\u7edd\u6240\u6709\u63a8\u9001\u3002<\/p>\n<p><code>HttpClient<\/code> \u7684\u91cd\u8f7d <code>sendAsync<\/code> \u53ef\u7528\u4e8e\u5904\u7406 push promise\u3002\u5148\u5b9a\u4e49\u5904\u7406\u5668\uff1a<\/p>\n<pre><code class=\"java\">private static PushPromiseHandler&lt;String&gt; pushPromiseHandler() {\n    return (HttpRequest initiatingRequest,\n        HttpRequest pushPromiseRequest,\n        Function&lt;HttpResponse.BodyHandler&lt;String&gt;,\n        CompletableFuture&lt;HttpResponse&lt;String&gt;&gt;&gt; acceptor) -&gt; {\n        acceptor.apply(BodyHandlers.ofString())\n            .thenAccept(resp -&gt; {\n                System.out.println(&quot;Pushed response: &quot; + resp.uri() + &quot;, headers: &quot; + resp.headers());\n            });\n        System.out.println(&quot;Promise request: &quot; + pushPromiseRequest.uri());\n        System.out.println(&quot;Promise request headers: &quot; + pushPromiseRequest.headers());\n    };\n}<\/code><\/pre>\n<p>\u518d\u7528 <code>sendAsync<\/code> \u6d88\u8d39\u5b83\uff1a<\/p>\n<pre><code class=\"java\">httpClient.sendAsync(pageRequest, BodyHandlers.ofString(), pushPromiseHandler())\n    .thenAccept(pageResponse -&gt; {\n        System.out.println(&quot;Page response status code: &quot; + pageResponse.statusCode());\n        System.out.println(&quot;Page response headers: &quot; + pageResponse.headers());\n        String responseBody = pageResponse.body();\n        System.out.println(responseBody);\n    })\n    .join();<\/code><\/pre>\n<h2>9. \u603b\u7ed3<\/h2>\n<p>\u672c\u6587\u63a2\u8ba8\u4e86 Java 11 \u4e2d\u6807\u51c6\u5316\u540e\u7684 <code>HttpClient<\/code> API\uff1a\u5728\u4fdd\u7559\u6613\u7528\u6027\u7684\u540c\u65f6\uff0c\u5f15\u5165\u4e86 HTTP\/2\u3001\u5f02\u6b65\u3001\u63a8\u9001\u627f\u8bfa\u3001\u4ee3\u7406\u3001\u91cd\u5b9a\u5411\u7b56\u7565\u3001\u8ba4\u8bc1\u5668\u3001Cookie \u7ba1\u7406\u7b49\u73b0\u4ee3\u5316\u80fd\u529b\uff0c\u8ba9 Java \u7684 HTTP \u7f16\u7a0b\u66f4\u9ad8\u6548\u3001\u66f4\u73b0\u4ee3\u3002<\/p>\n<p>\u66f4\u591a Java \u76f8\u5173\u5185\u5bb9\uff0c\u4e5f\u53ef\u4ee5\u5173\u6ce8\u6211\u7684\u8fd9\u4e2a\u5206\u7c7b\uff1a<a href=\"https:\/\/link.segmentfault.com\/?enc=KuOl7I%2BW1DNDKXtfaBQbjA%3D%3D.61etSK0Yy5BH4WOWbz5QjgTuyuLg4iq5t8atExz%2Bbxjdbta2zaWLyEJ8txvZQAI9\" rel=\"nofollow noopener noreferrer\" target=\"_blank\">Java\u4e13\u9898<\/a><\/p>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000047397416\">https:\/\/segmentfault.com\/a\/1190000047397416<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4f60\u662f\u5426\u4e5f\u9047\u5230\u8fc7\u8fd9\u6837\u7684\u65f6\u523b\uff1a\u53ea\u662f\u60f3\u53d1\u4e2a HTTP \u8bf7\u6c42\uff0c\u5374\u88ab\u8fde\u63a5\u7ba1\u7406\u3001\u91cd\u5b9a\u5411\u3001\u8d85\u65f6\u4e0e\u7ebf\u7a0b\u963b\u585e\u6298\u817e\u5f97\u4e0d\u4ea6\u4e50\u4e4e\uff1f\u90a3\u5c31 [&hellip;]<\/p>\n","protected":false},"author":8,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-10021077","post","type-post","status-publish","format-standard","hentry","category-share"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10021077","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/users\/8"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10021077"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10021077\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10021077"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10021077"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10021077"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}