1.1 执行请求
HttpClient最重要的功能就是执行HTTP方法。HTTP方法的执行涉及到一个或几个HTTP请求和响应的交换,这通常在HttpClient内部处理。用户提供执行的请求对象,HttpClient传输此请求到目标服务器并返回对应的响应对象或者在执行不成的情况下抛出异常。
很自然地,HttpClientAPI的主要入口点是定义了上述描述的协议的HttpClient接口。
下面是一个简单的执行请求过程的示例:
CloseableHttpClient httpclient = HttpClients.createDefault();HttpGet httpget = new HttpGet("http://localhost/");CloseableHttpResponse response = httpclient.execute(httpget);try { <...>} finally { response.close();}
1.1.1 HTTP请求
所有的HTTP请求都有一个包含方法名称、请求URI和一个HTTP协议版本的请求线。HttpClient支持所有HTTP/1.1定义的方法,这些方法是:GET,HEAD,POST,PUT,DELETE,TRACE,OPTIONS。在HttpClient中和这些方法对应的类分别为HttpGet,HttpHead,HttpPost,HttpPut,HttpDelete,HttpTrace,HttpOptions。
URI(统一资源标识符)标记了一个可被请求的资源,HTTP请求URIs由协议概述、主机名称、资源路径、以及其他可选的(端口、查询参数)参数组成。
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());
输出:
http://www.google.com/search?q=httpclient&btnG=Google+Search&aq=f&oq=
1.1.2 HTTP响应
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.1200OKHTTP/1.1 200 OK
1.1.3 消息头
一个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=localhostSet-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());}
输出:
Set-Cookie: c1=a; path=/; domain=localhostSet-Cookie: c2=b; path="/", c3=c; domain="localhost"
HttpClient也提供了方便的方法把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 = apath=/domain=localhostc2 = bpath=/c3 = cdomain=localhost