1.基本的代码框架
CloseableHttpClient httpClient = HttpClients.createDefault();
URI uri = null;
HttpGet httpGet = null;
CloseableHttpResponse response= null;
try {
} catch (ClientProtocolException e) {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);
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (URISyntaxException e) {
} finally {e.printStackTrace();
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*/