httpclient4.5 入门教程 1.1执行请求
来源:互联网 发布:免费顶级永久域名注册 编辑:程序博客网 时间:2024/05/16 07:31
第一章 :基础
1.1执行请求
HttpClient最基本的功能是执行HTTP方法,一个 HTTP 方法的执行包含一个或多个 HTTP 请求/HTTP 相应的交换,通常由 HttpClient的内部来处理。使用者被要求提供一个Request对象来执行,HttpClient就会把请求传送给目标服务器并返回一个相对应的response对象,如果执行不成功,将会抛出一个异常。
显然,HttpClient API 的主要切入点就是定义描述上述契约的HttpClient接口。一个简单的请求执行事例:
- CloseableHttpClient httpclient = HttpClients.createDefault();
- HttpGet httpget = new HttpGet(“http://localhost/”);
- CloseableHttpResponse response = httpclient.execute(httpget);
- try {
- <…>
- } finally {
- response.close();
- }
CloseableHttpClient httpclient = HttpClients.createDefault();HttpGet httpget = new HttpGet("http://localhost/");CloseableHttpResponse response = httpclient.execute(httpget);try { <...>} finally { response.close();}
1.1.1. HTTP 请求(Request)
所有 HTTP 请求都有一个请求起始行,这个起始行由方法名,请求 URI 和 HTTP 协议版本组成。HttpClient很好地支持了HTTP/1.1规范中所有的HTTP方法:GET,HEAD, POST,PUT, DELETE, TRACE 和 OPTIONS。每个方法都有一个特别的类:HttpGet,HttpHead, HttpPost,HttpPut,HttpDelete,HttpTrace和HttpOptions。URI是统一资源标识符的缩写,用来标识与请求相符合的资源。HTTP 请求URI包含了一个协议名称,主机名,可选端口,资源路径,可选的参数,可选的片段。
- HttpGet httpget = new HttpGet(
- ”http://www.google.com/search?hl=en&q=httpclient&btnG=Google+Search&aq=f&oq=”);
HttpGet httpget = new HttpGet( "http://www.google.com/search?hl=en&q=httpclient&btnG=Google+Search&aq=f&oq=");
HttpClient提供了URIBuilder工具类来简化创建、修改请求 URIs。
- 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());</span>
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());</span>输出:
- http://www.google.com/search?q=httpclient&btnG=Google+Search&aq=f&oq=
http://www.google.com/search?q=httpclient&btnG=Google+Search&aq=f&oq=
1.1.2.HTTP 响应(Response)
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());
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
- 200
- OK
- HTTP/1.1 200 OK
HTTP/1.1200OKHTTP/1.1 200 OK
1.1.3.处理报文首部(Headers)
一个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);
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”
2
获得所有指定类型首部最有效的方式是使用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());
- }
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”
HttpClient也提供了其他便利的方法吧HTTP报文转化为单个的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]);
- }
- }
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
======================================总结区域(非原文)============================
1.对于HttpClient你可以把它简单理解成浏览器就好了。
2.HttpClient请求是必不可少的,所以构建请求很重要,要对HTTP报文有一定的了解。
3.响应不是必须的,因为HttpClient是客户端编程,只是为了方便测试使用。
4.学会处理首部很重要,会面有更加详细的内容。
5.对HTTP协议不是很了解的同学,推荐看《HTTP权威指南》这本书。
其他内容请查看目录贴点击打开链接
- httpclient4.5 入门教程 1.1执行请求
- httpclient4.5 入门教程 1.1执行请求<二>
- HttpClient4.5 基础教程 执行请求<三>
- HttpClient4.5入门教程
- 【HttpClient4.5中文教程】【第一章 :基础】1.1执行请求(一)
- 【HttpClient4.5中文教程】【第一章 :基础】1.1执行请求(二)
- 【HttpClient4.5中文教程】【第一章 :基础】1.1执行请求(三)
- 【HttpClient4.5中文教程】【第一章 :基础】1.1执行请求(一)
- 【HttpClient4.5中文教程】【第一章 :基础】1.1执行请求(二)
- 【HttpClient4.5中文教程】【第一章 :基础】1.1执行请求(三)
- HttpClient4.5发送post请求
- httpclient4.5入门教程之连接管理
- 使用HttpClient4.5 PoolingHttpClientConnectionManager 提交Http请求
- HttpClient4.5 post请求xml到服务器
- httpclient4.x代理请求
- HttpClient4.x 多线程请求
- HttpClient4.3.3 https请求
- 【HttpClient4.5实训】一.HttpClient4.5模拟浏览器GET请求访问新浪网(非原文教程)
- AndroidStudio基础教程(一)
- redis的数据安全性和性能保障
- Revit二次开发 自带的钢筋
- 原生js封装AJAX(代码+注释)
- Atitit 技术管理项目管理的职责列表attilax总结 v2 r74 brch4com.docx
- httpclient4.5 入门教程 1.1执行请求
- mybatis-generator结合freemarker生成简单的service
- 无处不在的字符串和包装类,看完你就超神
- mysql 查询时间相关
- WUST OJ 1482:find clouds
- Eclipse无法打开,一闪而过的解决方法。
- Atitit 界面接口技术 cli gui nui cui管理 attilax总结 1. NUI1 1.1. 问:什么是“自然用户界面”?1 2. Cli到gui到nui CUI2 2.1.
- 在caffe训练的数据源的格式
- HDU5952(66/600)