java 中 HttpClient 和HttpURLConnection都可以用来发起Http请求。
HttpURLConnection
HttpURLConnection是一种多用途、轻量极的HTTP客户端,使用它来进行HTTP操作可以适用于大多数的应用程序。
- 优点:API比较简单,更加容易地使用和扩展
- 缺点:涉及Session、Cookie的处理时,使用HttpURLConnection 来处理这些细节,处理难度较大
HttpClient
简单来说,HttpClient就是一个增强版的HttpURLConnection,HttpURLConnection可以做的事情HttpClient全部可以做;HttpURLConnection没有提供的有些功能,HttpClient也提供了,但它只是关注于如何发送请求、接收响应,以及管理HTTP连接
- 缺点: 由于HttpClient的API数量过多,使得我们很难在不破坏兼容性的情况下对它进行升级和扩展
使用场景
在一般情况下,如果只是需要Web站点的某个简单页面提交请求并获取服务器响应,HttpURLConnection完全可以胜任。
但在绝大部分情况下,Web站点的网页可能没这么简单,这些页面并不是通过一个简单的URL就可访问的,可能需要用户登录而且具有相应的权限才可访问该页面。在这种情况下,就需要涉及Session、Cookie的处理了,如果打算使用HttpURLConnection来处理这些细节,当然也是可能实现的,只是处理起来难度就大了, 这时可以使用HttpClient。使用HttpClient来登录系统,只要应用程序使用同一个HttpClient发送请求,HttpClient会自动维护与服务器之间的Session状态,也就是说程序第一次使用HttpClient登录系统后,接下来使用HttpClient即可访问被保护页而了。
HttpClient 使用步骤
- 创建HttpClient对象。
- 如果需要发送GET请求,创建HttpGet对象;如果需要发送POST请求,创建HttpPost对象。
- 如果需要发送请求参数,可调用HttpGet、HttpPost共同的setParams(HetpParams params)方法来添加请求参数;对于HttpPost对象而言,也可调用setEntity(HttpEntity entity)方法来设置请求参数。
- 调用HttpClient对象的execute(HttpUriRequest request)发送请求,执行该方法返回一个HttpResponse。
- 调用HttpResponse的getAllHeaders()、getHeaders(String name)等方法可获取服务器的响应头;调用HttpResponse的getEntity()方法可获取HttpEntity对象,该对象包装了服务器的响应内容。程序可通过该对象获取服务器的响应内容。
HttpClient 样例(groovy)
class HttpClientUtil {
//DefaultHttpClient —> CloseableHttpClient
//HttpResponse —> CloseableHttpResponse
private static HttpClient httpClient = null;
/**
* 生产HttpClient实例
* 公开,静态的工厂方法,需要使用时才去创建该单体
*
* @return
*/
public static HttpClient getHttpClient() {
if (httpClient == null) {
httpClient = HttpClients.createDefault();
}
return httpClient;
}
/**
*
* @param url
* @return response as json str
*/
static String doGet(String url) {
HttpClient httpClient = getHttpClient();
HttpGet httpGet = new HttpGet(url);
CloseableHttpResponse response = httpClient.execute(httpGet)
try {
System.out.println(response.getStatusLine());
HttpEntity entity = response.getEntity();
// do something useful with the response body
// and ensure it is fully consumed (EntityUtils.consume(entity))
ResponseHandler<String> responseHandler = new BasicResponseHandler();
return responseHandler.handleResponse(response)
} finally {
//为了释放资源,我们必须手动消耗掉response1或者取消连接
response.close();
}
}
/**
*
* @param url
* @return response as json str
*/
static String doPost(String url, List <NameValuePair> nvps) {
HttpClient httpClient = getHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(nvps));
CloseableHttpResponse response = httpClient.execute(httpPost)
try {
System.out.println(response.getStatusLine());
HttpEntity entity = response.getEntity();
// do something useful with the response body
// and ensure it is fully consumed (EntityUtils.consume(entity))
EntityUtils.consume(entity);
} finally {
//为了释放资源,我们必须手动消耗掉response1或者取消连接
response.close();
}
}
/**
*
* @param url
* @return response as json str
*/
static String doPut(String url, List <NameValuePair> nvps) {
HttpClient httpClient = getHttpClient();
HttpPut httpPut = new HttpPut(url);
httpPut.setEntity(new UrlEncodedFormEntity(nvps));
CloseableHttpResponse response = httpClient.execute(httpPut)
try {
System.out.println(response.getStatusLine());
HttpEntity entity = response.getEntity();
// do something useful with the response body
// and ensure it is fully consumed (EntityUtils.consume(entity))
EntityUtils.consume(entity);
} finally {
//为了释放资源,我们必须手动消耗掉response1或者取消连接
response.close();
}
}
/**
*
* @param url
* @return response as json str
*/
static String doDelete(String url) {
HttpClient httpClient = getHttpClient();
HttpDelete httpDelete = new HttpDelete(url);
CloseableHttpResponse response = httpClient.execute(httpDelete)
try {
System.out.println(response.getStatusLine());
HttpEntity entity = response.getEntity();
// do something useful with the response body
// and ensure it is fully consumed (EntityUtils.consume(entity))
EntityUtils.consume(entity);
} finally {
//为了释放资源,我们必须手动消耗掉response1或者取消连接
response.close();
}
}
}
网友评论