HttpClient的Get方法
1. 首先创建一个Maven项目
2. 在pom.xml中配置Maven
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.3.1</version>
</dependency>
3. 不带参数的Get方法
CloseableHttpResponse response = null;
//打开浏览器,创建HttpClient对象
CloseableHttpClient httpClient = HttpClients.createDefault();
//输入网址,发起Get请求,创建HttpGet对象
HttpGet httpGet = new HttpGet(uriBuilder .build());
try {
//发起请求,返回响应
response = httpClient.execute(httpGet);
//解析数据,获取数据
//判断状态码
if (response.getStatusLine().getStatusCode()==200){
HttpEntity entity = response.getEntity();
String content = EntityUtils.toString(entity,"utf-8");
System.out.println(content);
}
} catch (IOException e) {
e.printStackTrace();
System.out.println("发起请求失败");
}finally {
//释放资源
response.close();
httpClient.close();
}
4. 带参数的Get方法
CloseableHttpResponse response = null;
//打开浏览器,创建HttpClient对象
CloseableHttpClient httpClient = HttpClients.createDefault();
//创建URIBuilder
URIBuilder uriBuilder = new URIBuilder("http://yun.itheima.com/search");
//设置参数
uriBuilder.setParameter("keys","java");
//输入网址,发起Get请求,创建HttpGet对象
HttpGet httpGet = new HttpGet(uriBuilder .build());
try {
//发起请求,返回响应
response = httpClient.execute(httpGet);
//解析数据,获取数据
//判断状态码
if (response.getStatusLine().getStatusCode()==200){
HttpEntity entity = response.getEntity();
String content = EntityUtils.toString(entity,"utf-8");
System.out.println(content);
}
} catch (IOException e) {
e.printStackTrace();
System.out.println("发起请求失败");
}finally {
//释放资源
response.close();
httpClient.close();
}
5. 不带参数的Post方法
//创建httpclient对象
CloseableHttpClient httpClient = HttpClients.createDefault();
//创建post对象
HttpPost post = new HttpPost("http://catjava.top");
//创建响应对象
//发起请求
CloseableHttpResponse response = null;
//就是把Get请求中的HttpGet换成HttpPost对象
6. 带参数的Post方法
//创建httpclient对象
CloseableHttpClient httpClient = HttpClients.createDefault();
//创建post对象
HttpPost post = new HttpPost("http://yun.itheima.com/search");
//声明List集合,封装表单中的参数
List<NameValuePair> params = new ArrayList<NameValuePair>();
//设置请求地址
params.add(new BasicNameValuePair("keys","Java"));
//创建表单中的Entity对象
UrlEncodedFormEntity encodedFormEntity = new UrlEncodedFormEntity(params,"utf-8");
//设置Enitity到post中
post.setEntity(encodedFormEntity);
//创建响应对象
//发起请求
- Post中声明一个List用来储存表单中的数据
7. 连接池
使用连接池来管理HttpClient,就不需要每次使用都创建销毁HttpClient对象。
public static void main(String[] args) throws URISyntaxException {
//创建连接池管理器
PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
//设置最大连接数
connectionManager.setMaxTotal(100);
//设置每个主机的最大连接数
connectionManager.setDefaultMaxPerRoute(10);
//使用连接池发起请求
doGet(connectionManager);
doGet(connectionManager);
}
private static void doGet(PoolingHttpClientConnectionManager connectionManager) throws URISyntaxException {
//不是创建httpclient对象,而是从连接池获取
CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(connectionManager).build();
//下面的就是正常的请求
//httpclient不能直接close
//应该由连接池管理器来管理
- 在最后释放资源的时候**不能httpclient.close(),应该由连接池来管理**
8. 请求参数
//输入网址,发起Get请求,创建HttpGet对象
HttpGet httpGet = new HttpGet(uriBuilder.build());
//配置请求信息
RequestConfig config = RequestConfig.custom().setConnectTimeout(1000)//创建连接的最长时间
.setConnectionRequestTimeout(500)//获取连接的最长时间
.setSocketTimeout(10*1000)//数据传输的最长时间
.build();
//还有很多其他的设置
//给请求设置请求信息
httpGet.setConfig(config);
try {
//发起请求,返回响应