

学习 HttpComponents
source link: https://www.tuicool.com/articles/yMR3ueu
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Apache HttpComponents 是 Commons HttpClient 在 ASF 下新的项目,创建并维护了 HTTP 相关的底层 Java 组件工具集。
项目包括:
- HttpComponents Core HttpCore 是用于构建自定义的 HTTP 客户端和服务端的底层 HTTP 传输组件。支持两种 I/O 模型:基于传统 Java I/O 的阻塞 I/O 模型和基于 Java NIO 的事件驱动 I/O 模型。
- HttpComponents Client HttpClient 是构建在 HttpCore 上 HTTP/1.1 兼容的 HTTP 客户端实现。
- HttpComponents AsyncClint Async HttpClient 是构建在 HttpCore NIO 和 HttpClient 组件上 HTTP/1.1 兼容的 HTTP 客户端实现。
HttpClient
依赖
编辑 pom.xml 文件,添加依赖:
<dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.9</version> </dependency>
GET
CloseableHttpClient httpClient = HttpClients.createDefault(); // ① HttpGet request = new HttpGet(url); // ② HttpResponse response = httpClient.execute(request); // ③ StatusLine status = response.getStatusLine(); // ④ if (status.getStatusCode() >= 200 && status.getStatusCode() < 300) { HttpEntity entity = response.getEntity(); // ⑤ } else { throw new ClientProtocolException(status.getStatusCode() + " " + status.getReasonPhrase()); } httpClient.close();
① 创建默认的 HTTP 客户端;
② 根据 URL 创建 HTTP GET 请求;
③ 执行 HTTP GET 请求,并返回响应;
④ 判断响应状态,状态码 [200, 300) 之间,代表正常否则抛出异常;
⑤ 获取响应内容,可以使用工具类 EntityUtils
获取字符串或字节内容。
POST
HTTP POST 提交 JSON 数据:
CloseableHttpClient httpClient = HttpClients.createDefault(); // ① HttpPost request = new HttpPost(url); // ② request.setEntity(new StringEntity(json, "utf-8")); HttpResponse response = httpClient.execute(request); // ③ StatusLine status = response.getStatusLine(); // ④ if (status.getStatusCode() >= 300) { throw new ClientProtocolException(status.getStatusCode() + " " + status.getReasonPhrase()); } httpClient.close();
① 创建默认的 HTTP 客户端;
② 根据 URL 创建 HTTP POST 请求,并设置 HTTP Body;
③ 执行 HTTP POST 请求,并返回响应;
④ 判断响应状态,状态码 [300, +∞) 抛出异常。
HTTP POST 提交表单数据:
TODO
HTTP POST 上传文件:
TODO
Recommend
-
172
文章从分布式系统的角度开展针对当前一些机器学习平台的研究,分析了这些平台在通信和控制上的瓶颈,并考虑了这些平台的容错性和易编程性。文章最后提供了一些结论性要点,并对分布式机器学习平台的未来研究工作提出了一些建议。
-
208
This site can’t be reached jizhi.im’s server IP address could not be found.
-
123
李航NSR论文:深度学习NLP的现有优势与未来挑战
-
130
小QA学习前端系列之练习实践vuex-shopping-cart代码地址https://github.com/vuejs/vuex/tree/dev/examples/shopping-cart 直接 npm install 或者yarn ,安装依赖 之后,npm run dev 运行 页面如下 废话不多说,直接看源码。 源码解析先来看看文件目录 该demo包含...
-
112
技术成就梦想51CTO-中国领先的IT技术网站 您浏览的内容找不到或已被删除
-
145
从20世纪60年代开始,人们就在期待像哈尔(HAL)这样的科幻级别的AI,然而直到最近,PC和机器人还是非常愚笨。现在,科技巨头和创业公司宣告了AI革命的到来:无人驾驶汽车、机器人医生、机器投资者等等。普华永道认为,到2030年,AI将会向世界经济贡献15.7万亿美元...
-
126
本文欢迎转载,原始地址:http://www.cnblogs.com/DjlNet/p/7603654.html 先说一点废话,在此之前博主也在早期就接触了或者看了些许AOP相关的文章,然...
-
85
深度学习难得的深入浅出的教材(李宏毅 老师的ppt)
-
107
-
62
HttpAsyncClient Quick Start Download 'Binary' package of the latest HttpAsyncClient 4.1 release or configure dependency on HttpAsyncClient module using a...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK