HTTPClient
HttpClient是客户端HTTP传输库。HTTPClient的目的就是发送和接收HTTP消息
简要介绍
- httpComponents包括httpCore和httpClient httpClient:http的执行http请求
- DefaultHttpClient: httpClient默认实现 HTTPDet/HttpPost:GET和POST方法执行类
- Httpresponse: 执行返回的Response,含http的header和执行结果的实体Entity
- HttpEntity: Http返回结果的实体,不含Header内容 HttpParam:连接参数,配合连接池使用
- PoolingClientConnectionMannager:连接池
使用httpClient获取页面内容的过程:
1.创建一个CloseableHttpClient类的实例
2.使用该实例发送HTTP请求,得到一个HttpResponse的实例
3.通过HttpResponse的实例得到返回的二进制流,将二进制流封装在HttpEntity中。
4.根据指定的字符集把二进制流转换成字符串,完成下载
示例代码1:demo
package HttpClient.httpclient.mark;
import java.io.InputStream;
import org.apache.http.HttpEntity;
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;
/**
* 手动释放连接
* @author YIMA
*
*/
public class ClientConnectionRelease {
public final static void main(String[] args) throws Exception{
CloseableHttpClient httpClient = HttpClients.createDefault();
try{
HttpGet httpGet = new HttpGet("http://httpbin.org/get");
System.out.println("Executing request: "+httpGet.getRequestLine());
CloseableHttpResponse response = httpClient.execute(httpGet);
try {
System.out.println("---------------------------------------------------------");
System.out.println(response.getStatusLine());
HttpEntity entity = response.getEntity();
if(entity != null){
InputStream inStream = entity.getContent();
try {
inStream.read();
} catch (Exception e) {
throw e;
}finally{
inStream.close();
}
}
}finally{
response.close();
}
}finally{
httpClient.close();
}
}
}
结果:
Executing request: GET http://httpbin.org/get HTTP/1.1
HTTP/1.1 200 OK
示例代码2:通过get方法方法请求
package com.mark.Crawer;
/**
* HttpClient GET方法请求
*/
import java.io.IOException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
public class httpClientGetTest {
public static void main(String[] args) {
httpClientGetTest test = new httpClientGetTest();
String response = test.getResponse2();
System.out.println(response);
}
private String getResponse2() {
final String target_url = "https://www.baidu.com/";
String response = "";
HttpClient httpClient = new DefaultHttpClient();//创建一个客户端
HttpGet httpGet = new HttpGet(target_url);//创建一个get方法
try {
HttpResponse httpresponse = httpClient.execute(httpGet);//获得网页的内容
HttpEntity entity = httpresponse.getEntity();//查看返回的内容
if(entity != null){
response = EntityUtils.toString(entity,"utf-8");//读取内容流,并以字符串形式返回
EntityUtils.consume(entity);//关闭内容流
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
httpClient.getConnectionManager().shutdown();//释放连接
}
return response;
}
//使用ResponseHandler处理响应:
private String getResponse() {
final String target_url = "https://www.baidu.com/";
String response = "";
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(target_url);
ResponseHandler<String> responseHandler = new BasicResponseHandler();//设置接收服务器响应回来的内容为字符串
try {
response = httpClient.execute(httpGet,responseHandler );
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
httpClient.getConnectionManager().shutdown();//释放连接
}
return response;
}
}
示例代码3:请求中添加一些常用方法
package com.mark.Crawer;
/**
* HttpClient GET方法请求(配置请求参数,设置request头部信息,保存cookie)
*/
import java.io.IOException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.CookieStore;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.AbstractHttpClient;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
public class httpClientGetTest {
private static final String target_url = "https://www.baidu.com/";
private final int connectTimeout = 9000;//连接超时
private final int socketTimeout = 9000;//读取数据超时
private CookieStore cookieStore ;
public static void main(String[] args) {
httpClientGetTest test = new httpClientGetTest();
String response = test.getResponse2();
System.out.println(response);
}
private String getResponse2() {
String response = "";
//设置配置信息
RequestConfig requestConfig = RequestConfig.custom()
.setConnectTimeout(connectTimeout)
.setConnectionRequestTimeout(connectTimeout)
.setSocketTimeout(socketTimeout).build();
CloseableHttpClient httpClient = HttpClientBuilder.create()
.setDefaultRequestConfig(requestConfig).build();//配置参数创建一个客户端
HttpGet httpGet = new HttpGet(target_url);//创建一个get方法
//设置头部信息
httpGet.setHeader("User-Agent", "****");
httpGet.setHeader("Accept-Charset", "");
//设置请求cookie
((AbstractHttpClient) httpClient).setCookieStore(cookieStore);
try {
HttpResponse httpresponse = httpClient.execute(httpGet);//获得网页的内容
HttpEntity entity = httpresponse.getEntity();//查看返回的内容
if(entity != null){
response = EntityUtils.toString(entity,"utf-8");//读取内容流,并以字符串形式返回
EntityUtils.consume(entity);//关闭内容流
}
//获取cookie信息
cookieStore = ((AbstractHttpClient) httpClient).getCookieStore();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
httpClient.getConnectionManager().shutdown();//释放连接
}
return response;
}
}