HttpClient 初步——总结

来源:互联网 发布:剑三花姐捏脸数据网盘 编辑:程序博客网 时间:2024/06/05 19:21

1.基本的代码框架

CloseableHttpClient httpClient = HttpClients.createDefault();
URI uri = null;
HttpGet httpGet = null;
CloseableHttpResponse response= null;
try {

uri = new URIBuilder().setScheme("http")

   .setHost("www.google.com.hk")

   .setPath("/search")

   .setParameter("q", "httpclient")

   .setParameter("btnG", "Google Search")

   .setParameter("aq", "f")

   .setParameter("oq", "")

   .build();

httpGet = new HttpGet(uri);

response = httpClient.execute(httpGet);

HttpEntity entity = response.getEntity();

String html = EntityUtils.toString(

entity, 

ContentType.getOrDefault(entity).getCharset().toString());

Logger.println(html);

} catch (ClientProtocolException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

} catch (URISyntaxException e) {

e.printStackTrace();

} finally {
try {
response.close();
} catch (IOException e) {
e.printStackTrace();

}

}



2.基本的类之间的关系



/*上图中的MethodObject指的是HttpGet,HttpPost等类的对象,在HttpClient中http方法已经做了抽象,如果想进一步了解HttpClient的知识,请看我转载的这篇文章http://blog.csdn.net/coming_chen/article/details/24475227#t3*/

0 0
原创粉丝点击