Httpclient4.4之原理(请求执行)
apache Httpclient基于java BIO实现的,也是基于apache HttpCore项目。他最基本的功能是执行HTTP方法。HttpClient的API的主要入口就是HttpClient接口,看看这个示例:
package httpclienttest; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; public class T1 { public static void main(String[] args) { HttpGet httpget = new HttpGet("http://www.baidu.com"); try(CloseableHttpClient httpclient = HttpClients.createDefault(); CloseableHttpResponse response = httpclient.execute(httpget);){ System.out.printf("内容类型为:%s",response.getEntity().getContentType()); }catch(Exception e){ e.printStackTrace(); } }
1. HTTP请求
所有的http请求都由:方法名,请求url,HTTP协议组成。HttpClient支持HTTP/1.1支持的所有方法:GET,HEAD,POST,PUT,DELETE,TRACE和OPTIONS,HttpClient中都有一个特定的类与之对应:HttpGet,HttpHead,HttpPost,HttpPut,HttpDelete,HttpTrace和HttpOptions。
HTTP请求URI包括协议,主机名,可选的端口,资源路径,可选的查询条件等。如下例:
HttpGet httpget = new HttpGet("http://www.google.com/search?hl=en" + "&q=httpclient&btnG=Google+Search&aq=f&oq=");
HttpClient提供了URIBuilder通用类来创建或修改请求URI,如例:
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())
2. HTTP响应
HTTP响应是HTTP请求发送到服务端处理后响应到客户端的消息。响应第一行是协议与协议版本号,接着是数字状态码和一些文本信息,示例演示一下看看执行结果:
package httpclienttest; import org.apache.http.HttpResponse; import org.apache.http.HttpStatus; import org.apache.http.HttpVersion; import org.apache.http.message.BasicHttpResponse; public class T2 { public static void main(String[] args) { 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
3. HTTP消息头
HTTP消息头(header)包含多个消息描述的信息,例如:内容长度,内容类型等。HttpClient提供的方法有检索,添加,删除和枚举等操作。 示例:
package httpclienttest; import org.apache.http.Header; import org.apache.http.HttpResponse; import org.apache.http.HttpStatus; import org.apache.http.HttpVersion; import org.apache.http.message.BasicHttpResponse; public class T3 { public static void main(String[] args) { 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接口获得所有的header信息,示例:
package httpclienttest; import org.apache.http.HeaderIterator; import org.apache.http.HttpResponse; import org.apache.http.HttpStatus; import org.apache.http.HttpVersion; import org.apache.http.message.BasicHttpResponse; public class T4 { public static void main(String[] args) { 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消息并获得header中一个个独立的header元素,示例:
package httpclienttest; import org.apache.http.HeaderElement; import org.apache.http.HeaderElementIterator; import org.apache.http.HttpResponse; import org.apache.http.HttpStatus; import org.apache.http.HttpVersion; import org.apache.http.NameValuePair; import org.apache.http.message.BasicHeaderElementIterator; import org.apache.http.message.BasicHttpResponse; public class T5 { public static void main(String[] args) { 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
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。