《HttpClient官方文档》1.1 执行请求(一)
HttpClient最基本的功能就是执行HTTP方法。 一个HTTP方法的执行包含一次或多次HTTP请求与响应,通常由HttpClient的内部处理。
用户提供一个请求对象,HttpClient发送该请求到目标服务器,服务器返回相应的响应对象,如果执行未成功则抛出一个异常。
很自然地,HttpClient的API的主要入口点就是定义了上述协议的HttpClient接口。下面是一个最简单的请求执行过程例子
CloseableHttpClient httpclient = HttpClients.createDefault(); HttpGet httpget = new HttpGet("http://localhost/"); CloseableHttpResponse response = httpclient.execute(httpget); try { ... } finally { response.close();
所有HTTP请求都有由方法名,请求URI和HTTP协议版本组成的请求行。
HttpClient支持开箱即用HTTP/1.1规范中定义的所有HTTP方法:GET, HEAD,POST, PUT, DELETE,TRACE and OPTIONS。它们都有一个特定的类对应这些方法类型: HttpGet,HttpHead, HttpPost,HttpPut, HttpDelete,HttpTrace, and HttpOptions.
请求的URI是统一资源定位符,它标识了应用于哪个请求之上的资源。HTTP请求的URI包含协议方案,主机名,可选的端口,资源路径,可选查询和可选片段。
HttpGet httpget = new HttpGet( "http://www.google.com/search?hl=en q=httpclient btnG=Google+Search aq=f oq=");
HttpClient 提供 URIBuilder 实用类来简化请求 URL的创建和修改.
URI uri = new URIBuilder() .setScheme("http") .setHost("www.google.com") .setPath("/search") .setParameter("q", "httpclient") .setParameter("btnG", "Google Search") .setParameter("aq", "f") .setParameter("oq", "") .build(); HttpGet httpget = new HttpGet(uri); System.out.println(httpget.getURI());
输出内容为
http://www.google.com/search?q=httpclient btnG=Google+Search aq=f oq=
HTTP响应是服务器端在接收和解释客户端请求消息后,返回客户端的消息。该消息的第一行包含协议版本以及后面跟着的数字形式的状态代码和相关的文本段。
HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK"); System.out.println(response.getProtocolVersion()); System.out.println(response.getStatusLine().getStatusCode()); System.out.println(response.getStatusLine().getReasonPhrase()); System.out.println(response.getStatusLine().toString());
输出内容为
HTTP/1.1 HTTP/1.1 200 OK
HTTP消息可以包含多个描述该消息属性的头部诸如内容长度,内容类型等,HttpClient的提供方法来检索,添加,删除和枚举这些头部。
HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK"); response.addHeader("Set-Cookie", "c1=a; path=/; domain=localhost"); response.addHeader("Set-Cookie", "c2=b; path=\"/\", c3=c; domain=\"localhost\""); Header h1 = response.getFirstHeader("Set-Cookie"); System.out.println(h1); Header h2 = response.getLastHeader("Set-Cookie"); System.out.println(h2); Header[] hs = response.getHeaders("Set-Cookie"); System.out.println(hs.length);
输出内容为
Set-Cookie: c1=a; path=/; domain=localhost Set-Cookie: c2=b; path="/", c3=c; domain="localhost"
获得所有头部给定类型的最有效的方法是使用
HeaderIterator 接口.
HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK"); response.addHeader("Set-Cookie", "c1=a; path=/; domain=localhost"); response.addHeader("Set-Cookie", "c2=b; path=\"/\", c3=c; domain=\"localhost\""); HeaderIterator it = response.headerIterator("Set-Cookie"); while (it.hasNext()) { System.out.println(it.next());
输出内容为
Set-Cookie: c1=a; path=/; domain=localhost Set-Cookie: c2=b; path="/", c3=c; domain="localhost"
它还提供了方便的方法来解析HTTP消息成为独立头部元素。
HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK"); response.addHeader("Set-Cookie", "c1=a; path=/; domain=localhost"); response.addHeader("Set-Cookie", "c2=b; path=\"/\", c3=c; domain=\"localhost\""); HeaderElementIterator it = new BasicHeaderElementIterator( response.headerIterator("Set-Cookie")); while (it.hasNext()) { HeaderElement elem = it.nextElement(); System.out.println(elem.getName() + " = " + elem.getValue()); NameValuePair[] params = elem.getParameters(); for (int i = 0; i params.length; i++) { System.out.println(" " + params[i]);
输出内容为
c1 = a path=/ domain=localhost c2 = b path=/ c3 = c domain=localhost
相关文章
- 通读Python官方文档之wsgiref(未完成)
- 关于如何生成代码的帮助文档的链接
- 【官方文档】《暗黑世界V1.4》API说明!
- Firefly官方教程之DBentrust使用文档
- Firefly官方教程之Distributed使用文档
- ReactiveCocoa 4 官方文档翻译
- VS2013 "当前不会命中断点.还没有为该文档家在任何符号" 解决办法
- JS魔法堂:浏览器模式和文档模式怎么玩?
- Java实现 蓝桥杯 算法提高 新建Microsoft world文档
- 第一百一十四节,JavaScript文档对象,DOM进阶
- 支付宝安全支付服务Android应用开发指南(摘取官方文档)
- 【资料汇编】结巴中文分词官方文档和源码分析系列文章
- 【Gson】简介 文档 基本使用 示例
- SAP官方文档里关于CI和CD区别的解释:Continuous Integration vs Continuous Delivery
- ORACLE 官方文档
- 一起学微软Power BI系列-官方文档-入门指南(7)发布与共享-终结篇+完整PDF文档
- 一起学微软Power BI系列-官方文档-入门指南(3)Power BI建模
- elastic不错的官方文档(中文)
- Angular依赖注入官方文档的学习笔记
- 终于有阿里p9架构师分享出困扰我多年的Android系统开发进阶实战文档
- python3 asyncio官方文档中文版
- 【Android Gradle 插件】AaptOptions 配置 ① ( AaptOptions#additionalParameters 附加参数配置 | aapt 工具帮助文档 )
- Spring 官方教程:使用 Restdocs 创建 API 文档
- 006-基于hyperledger fabric1.4( 官方文档)编写第一个应用【外部nodejs调用】
- Eolinker API 接口文档神器
- 使用sphinx快速为你python注释生成API文档
- 【大数据开发运维解决方案】hadoop+kylin安装及官方cube/steam cube案例文档