{"id":832,"date":"2013-10-22T14:19:22","date_gmt":"2013-10-22T06:19:22","guid":{"rendered":"http:\/\/www.yeetrack.com\/?p=832"},"modified":"2013-10-22T14:19:22","modified_gmt":"2013-10-22T06:19:22","slug":"httpclient4-3-%e6%95%99%e7%a8%8b-%e7%ac%ac%e4%ba%94%e7%ab%a0-%e5%bf%ab%e9%80%9fapi","status":"publish","type":"post","link":"https:\/\/www.yeetrack.com\/?p=832","title":{"rendered":"HttpClient4.3 \u6559\u7a0b \u7b2c\u4e94\u7ae0 \u5feb\u901fAPI"},"content":{"rendered":"<p><meta http-equiv=\"content-type\" content=\"text\/html; charset=utf-8\" \/><br \/>\n<a name=\"Easy-to-use-facade-API\"><\/a><\/p>\n<h2>5.1.Easy to use facade API<\/h2>\n<p>HttpClient\u4ece4.2\u5f00\u59cb\u652f\u6301\u5feb\u901fapi\u3002\u5feb\u901fapi\u4ec5\u4ec5\u5b9e\u73b0\u4e86HttpClient\u7684\u57fa\u672c\u529f\u80fd\uff0c\u5b83\u53ea\u8981\u7528\u4e8e\u4e00\u4e9b\u4e0d\u9700\u8981\u7075\u6d3b\u6027\u7684\u7b80\u5355\u573a\u666f\u3002\u4f8b\u5982\uff0c\u5feb\u901fapi\u4e0d\u9700\u8981\u7528\u6237\u5904\u7406\u8fde\u63a5\u7ba1\u7406\u548c\u8d44\u6e90\u91ca\u653e\u3002<\/p>\n<p>\u4e0b\u9762\u662f\u51e0\u4e2a\u4f7f\u7528\u5feb\u901fapi\u7684\u4f8b\u5b50\uff1a<\/p>\n<pre><code>    \/\/ \u6267\u884c\u4e00\u4e2aget\u65b9\u6cd5\uff0c\u8bbe\u7f6e\u8d85\u65f6\u65f6\u95f4\uff0c\u5e76\u4e14\u5c06\u7ed3\u679c\u53d8\u6210\u5b57\u7b26\u4e32\n    Request.Get(\"https:\/\/www.yeetrack.com\/\")\n            .connectTimeout(1000)\n            .socketTimeout(1000)\n            .execute().returnContent().asString();<!--more-->\n\n    \/\/ \u4f7f\u7528HTTP\/1.1,\u901a\u8fc7'expect-continue' handshake\u6765\u6267\u884cpost\u65b9\u6cd5\n    \/\/ \u5185\u5bb9\u5305\u542b\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u5e76\u4e14\u5c06\u7ed3\u679c\u8f6c\u5316\u6210byte\u6570\u7ec4\n    Request.Post(\"https:\/\/www.yeetrack.com\/do-stuff\")\n        .useExpectContinue()\n        .version(HttpVersion.HTTP_1_1)\n        .bodyString(\"Important stuff\", ContentType.DEFAULT_TEXT)\n        .execute().returnContent().asBytes();\n\n    \/\/ \u901a\u8fc7\u4ee3\u7406\u670d\u52a1\u5668\u6765\u6267\u884c\u4e00\u4e2a\u5e26\u6709\u7279\u6b8aheader\u7684post\u8bf7\u6c42\uff0cpost\u8bf7\u6c42\u4e2d\u5e26\u6709form\u8868\u5355\uff0c\u5e76\u4e14\u5c06\u8fd4\u56de\u7ed3\u679c\u5199\u5165\u6587\u4ef6\n    Request.Post(\"https:\/\/www.yeetrack.com\/some-form\")\n            .addHeader(\"X-Custom-header\", \"stuff\")\n            .viaProxy(new HttpHost(\"myproxy\", 8080))\n            .bodyForm(Form.form().add(\"username\", \"vip\").add(\"password\", \"secret\").build())\n            .execute().saveContent(new File(\"result.dump\"));\n<\/code><\/pre>\n<p>\u5982\u679c\u9700\u8981\u5728\u6307\u5b9a\u7684\u5b89\u5168\u4e0a\u4e0b\u6587\u4e2d\u6267\u884c\u67d0\u4e9b\u8bf7\u6c42\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528Exector\uff0c\u8fd9\u65f6\u5019\u7528\u6237\u7684\u8ba4\u8bc1\u4fe1\u606f\u5c31\u4f1a\u88ab\u7f13\u5b58\u8d77\u6765\uff0c\u4ee5\u4fbf\u540e\u7eed\u7684\u8bf7\u6c42\u4f7f\u7528\u3002<\/p>\n<pre><code>    Executor executor = Executor.newInstance()\n            .auth(new HttpHost(\"somehost\"), \"username\", \"password\")\n            .auth(new HttpHost(\"myproxy\", 8080), \"username\", \"password\")\n            .authPreemptive(new HttpHost(\"myproxy\", 8080));\n\n    executor.execute(Request.Get(\"http:\/\/somehost\/\"))\n            .returnContent().asString();\n\n    executor.execute(Request.Post(\"http:\/\/somehost\/do-stuff\")\n            .useExpectContinue()\n            .bodyString(\"Important stuff\", ContentType.DEFAULT_TEXT))\n            .returnContent().asString();\n<\/code><\/pre>\n<p><a name=\"Response-handling\"><\/a><\/p>\n<h2>5.1.1.\u54cd\u5e94\u5904\u7406<\/h2>\n<p>\u4e00\u822c\u60c5\u51b5\u4e0b\uff0cHttpClient\u7684\u5feb\u901fapi\u4e0d\u7528\u7528\u6237\u5904\u7406\u8fde\u63a5\u7ba1\u7406\u548c\u8d44\u6e90\u91ca\u653e\u3002\u4f46\u662f\uff0c\u8fd9\u6837\u7684\u8bdd\uff0c\u5c31\u5fc5\u987b\u5728\u5185\u5b58\u4e2d\u7f13\u5b58\u8fd9\u4e9b\u54cd\u5e94\u6d88\u606f\u3002\u4e3a\u4e86\u907f\u514d\u8fd9\u4e00\u60c5\u51b5\uff0c\u5efa\u8bae\u4f7f\u7528\u4f7f\u7528ResponseHandler\u6765\u5904\u7406Http\u54cd\u5e94\u3002<\/p>\n<pre><code>    Document result = Request.Get(\"http:\/\/somehost\/content\")\n            .execute().handleResponse(new ResponseHandler&lt;Document&gt;() {\n\n        public Document handleResponse(final HttpResponse response) throws IOException {\n            StatusLine statusLine = response.getStatusLine();\n            HttpEntity entity = response.getEntity();\n            if (statusLine.getStatusCode() &gt;= 300) {\n                throw new HttpResponseException(\n                        statusLine.getStatusCode(),\n                        statusLine.getReasonPhrase());\n            }\n            if (entity == null) {\n                throw new ClientProtocolException(\"Response contains no content\");\n            }\n            DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();\n            try {\n                DocumentBuilder docBuilder = dbfac.newDocumentBuilder();\n                ContentType contentType = ContentType.getOrDefault(entity);\n                if (!contentType.equals(ContentType.APPLICATION_XML)) {\n                    throw new ClientProtocolException(\"Unexpected content type:\" +\n                        contentType);\n                }\n                String charset = contentType.getCharset();\n                if (charset == null) {\n                    charset = HTTP.DEFAULT_CONTENT_CHARSET;\n                }\n                return docBuilder.parse(entity.getContent(), charset);\n            } catch (ParserConfigurationException ex) {\n                throw new IllegalStateException(ex);\n            } catch (SAXException ex) {\n                throw new ClientProtocolException(\"Malformed XML document\", ex);\n            }\n        }\n\n        });\n<\/code><\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>5.1.Easy to use facade API HttpClient\u4ece4.2\u5f00\u59cb\u652f\u6301\u5feb\u901fapi\u3002\u5feb\u901fapi\u4ec5\u4ec5\u5b9e\u73b0\u4e86HttpClient\u7684\u57fa\u672c\u529f\u80fd\uff0c\u5b83\u53ea\u8981\u7528\u4e8e\u4e00\u4e9b\u4e0d\u9700\u8981\u7075\u6d3b\u6027\u7684\u7b80\u5355\u573a\u666f\u3002\u4f8b\u5982\uff0c\u5feb&#46;&#46;&#46;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"pgc_sgb_lightbox_settings":"","footnotes":""},"categories":[33],"tags":[8,15,7,5,42,13,41],"class_list":["post-832","post","type-post","status-publish","format-standard","hentry","category-coding","tag-java","tag-15","tag-7","tag-5","tag-42","tag-13","tag-41"],"views":7682,"_links":{"self":[{"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=\/wp\/v2\/posts\/832","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=832"}],"version-history":[{"count":1,"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=\/wp\/v2\/posts\/832\/revisions"}],"predecessor-version":[{"id":833,"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=\/wp\/v2\/posts\/832\/revisions\/833"}],"wp:attachment":[{"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=832"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=832"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=832"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}