HttpClient基础使用
引入Maven依赖:
<properties>
<httpclient-version>4.4.1</httpclient-version>
</properties>
<dependencies>
<!-- httpclient -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>${httpclient-version}</version>
</dependency>
</dependencies>
Get方式请求不带参数:
//创建一个httpclient对象
CloseableHttpClient httpClient = HttpClients.createDefault();
//创建一个GET对象
HttpGet get = new HttpGet("http://www.baidu.com");
//执行请求
CloseableHttpResponse response = httpClient.execute(get);
//获取响应状态码
int statusCode = response.getStatusLine().getStatusCode();
System.out.println(statusCode);
//获取响应结果
HttpEntity entity = response.getEntity();
String string = EntityUtils.toString(entity, "utf-8");
System.out.println(string);
//关闭httpclient
response.close();
httpClient.close();
Get方式请求携带参数:
- 使用List进行参数传递时注意参数类型为NameValuePair,需要使用new BasicNameValuePair("key","value")进行参数绑定;
URIBuilder uriBuilder = new URIBuilder("http://localhost/second"); ArrayList<NameValuePair> nvps = new ArrayList<NameValuePair>(); nvps.add(new BasicNameValuePair("username", "Steven")); nvps.add(new BasicNameValuePair("password", "Russell")); uriBuilder.addParameters(nvps); HttpGet get = new HttpGet(uriBuilder.build());
- 直接进行参数绑定。
// 创建一个uri对象 URIBuilder uriBuilder = new URIBuilder("http://localhost/second"); uriBuilder.addParameter("username", "Steven"); uriBuilder.addParameter("password","Russell"); HttpGet get = new HttpGet(uriBuilder.build());
//创建一个httpclient对象
CloseableHttpClient httpClient = HttpClients.createDefault();
//创建一个uri对象
URIBuilder uriBuilder = new URIBuilder("http://www.baidu.com/web");
//设置传递的参数
uriBuilder.addParameter("username", "Steven");
uriBuilder.addParameter("password","Russell");
//这里参数传递可以使用List也可以直接设置参数进行传递
//List<NameValuePair> nvps = new ArrayList<NameValuePair>();
//nvps.add(new BasicNameValuePair("username", "Steven"));
//nvps.add(new BasicNameValuePair("password", "Russell"));
//uriBuilder.addParameters(nvps);
HttpGet get = new HttpGet(uriBuilder.build());
//执行请求
CloseableHttpResponse response = httpClient.execute(get);
//取响应的结果
int statusCode = response.getStatusLine().getStatusCode();
System.out.println(statusCode);
HttpEntity entity = response.getEntity();
String string = EntityUtils.toString(entity, "utf-8");
System.out.println(string);
//关闭httpclient
response.close();
httpClient.close();
Post方式请求不带参数:
CloseableHttpClient httpClient = HttpClients.createDefault();
// 创建一个post对象
HttpPost post = new HttpPost("http://localhost/first");
// 执行post请求
CloseableHttpResponse response = httpClient.execute(post);
// 获取相应结果
int statusCode = response.getStatusLine().getStatusCode();
System.out.println("响应状态码为:"+statusCode);
String string = EntityUtils.toString(response.getEntity());
System.out.println(string);
response.close();
httpClient.close();
Post方式请求携带参数:
- 使用List进行参数传递模拟表单
CloseableHttpClient httpClient = HttpClients.createDefault();
// 创建一个post对象
HttpPost post = new HttpPost("http://localhost/second");
// 使用List进行参数传递(模拟表单)
List<NameValuePair> kvList = new ArrayList<>();
kvList.add(new BasicNameValuePair("username", "Steven"));
kvList.add(new BasicNameValuePair("password", "Russell"));
// 包装成一个Entity对象
StringEntity entity = new UrlEncodedFormEntity(kvList, "utf-8");
// 设置请求的内容
post.setEntity(entity);
// 执行post请求
CloseableHttpResponse response = httpClient.execute(post);
String string = EntityUtils.toString(response.getEntity());
System.out.println(string);
response.close();
httpClient.close();
2、使用reset方式进行参数传递
CloseableHttpClient httpClient = HttpClients.createDefault();
// 创建一个post对象
HttpPost post = new HttpPost("http://localhost/fourth/Steven/Russell");
post.reset();
// 执行post请求
CloseableHttpResponse response = httpClient.execute(post);
String string = EntityUtils.toString(response.getEntity());
System.out.println(string);
response.close();
httpClient.close();