今天给大家分享的是Httpclient,如有不足,敬请指教。
一、Httpclient简介
1.1 什么是httpclient
- HttpClient 是 Apache Jakarta Common 下的子项目,用来提供高效的、最新的、功能丰富的支持 HTTP 协议的客户端编程工具包,并且它支持 HTTP 协议最新的版本和建议。实现了所有 HTTP 的方法(GET,POST,PUT,HEAD 等)
- 下载地址:http://hc.apache.org/
1.2 httpclient作用
- 在java代码中,发送Http请求。通常用来实现远程接口调用
1.3 HttpClient测试
- 在工程中添加httpclient的pom依赖。
<!-- httpclient -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.3.5</version>
</dependency>
1.3.1 执行GET请求
package com.xkt.test;
import java.io.IOException;
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;
import org.apache.http.util.EntityUtils;
/**
* 测试HttpClient
*
* @author lzx
*
*/
public class HttpClientGet {
public static void main(String[] args) {
doGet();
}
private static void doGet() {
CloseableHttpClient httpClient = null;
CloseableHttpResponse response = null;
try {
// 1.创建客户端
httpClient = HttpClients.createDefault();
// 2.创建get请求
HttpGet get = new HttpGet("http://www.baidu.com");
// 3.执行请求
response = httpClient.execute(get);
// 4.解析结果
// 判断返回状态是否为200
int code = response.getStatusLine().getStatusCode();
if (200 == code) {
HttpEntity entity = response.getEntity();
String str = EntityUtils.toString(entity, "utf-8");
System.out.println(str);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != response) {
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (httpClient != null) {
try {
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
1.3.2 执行GET带参数
package com.xkt.test;
import java.io.IOException;
import java.net.URI;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
/**
* 测试HttpClient带参数
*
* @author lzx
*
*/
public class HttpClientGetWithParam {
public static void main(String[] args) {
doGetWithParam();
}
private static void doGetWithParam() {
CloseableHttpClient httpClient = null;
CloseableHttpResponse response = null;
try {
// 1.创建客户端
httpClient = HttpClients.createDefault();
// 2.创建get请求,当get请求有参数时,就在uri路径上,需要用URIBuilder去构建参数
URI uri = new URIBuilder("http://www.baidu.com/s").setParameter("wd", "java").build();
HttpGet get = new HttpGet(uri);
// 3.执行请求
response = httpClient.execute(get);
// 4.解析结果
// 判断返回状态是否为200
int code = response.getStatusLine().getStatusCode();
if (200 == code) {
HttpEntity entity = response.getEntity();
String str = EntityUtils.toString(entity, "utf-8");
System.out.println(str);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != response) {
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (httpClient != null) {
try {
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
1.3.3 执行post请求
package com.xkt.test;
import java.io.IOException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.LaxRedirectStrategy;
import org.apache.http.util.EntityUtils;
/**
* 测试HttpClientPost
*
* @author lzx
*
*/
public class HttpClientPost {
public static void main(String[] args) {
doPost();
}
private static void doPost() {
// 1.创建Httpclient对象
CloseableHttpClient httpclient = HttpClientBuilder.create().setRedirectStrategy(new LaxRedirectStrategy())
.build();
// 2.创建http POST请求
HttpPost httpPost = new HttpPost("http://www.oschina.net");
CloseableHttpResponse response = null;
/**
* HTTP/1.1 403 Forbidden 原因: 有些网站,设置了反爬虫机制 解决的办法: 设置请求头,伪装浏览器
*/
httpPost.addHeader("user-agent",
"Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36");
try {
// 3.执行请求
response = httpclient.execute(httpPost);
System.out.println(response.getStatusLine());
// 4.解析结果
// 判断返回状态是否为200
if (response.getStatusLine().getStatusCode() == 200) {
String content = EntityUtils.toString(response.getEntity(), "UTF-8");
System.out.println(content);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (response != null) {
try {
response.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try {
httpclient.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
1.3.4 执行post带参数
package com.xkt.test;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.LaxRedirectStrategy;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
/**
* 测试HttpClientPostWithParam
*
* @author lzx
*
*/
public class HttpClientPostWithParam {
public static void main(String[] args) {
doPostWithParam();
}
private static void doPostWithParam() {
CloseableHttpClient httpclient = null;
CloseableHttpResponse response = null;
try {
// 1、第一步:创建HttpClient客户端
httpclient = HttpClientBuilder.create().setRedirectStrategy(new LaxRedirectStrategy()).build();
// 2、第二步:创建post请求方式,如果有参数,需要通过UrlEncodedFormEntity来模拟form表单
// 设置2个post参数,一个是scope、一个是q
List<NameValuePair> parameters = new ArrayList<>();
parameters.add(new BasicNameValuePair("scope", "project"));
parameters.add(new BasicNameValuePair("q", "mysql"));
// 构造一个form表单式的实体
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(parameters);
HttpPost post = new HttpPost("http://www.oschina.net/search");
// 将请求实体设置到httpPost对象中
post.setEntity(formEntity);
// 有一些网站(接口)设置了反爬虫机制,禁止httpclient这种工具去请求接口代码
// 设置请求头,伪装浏览器发送请求,有的网站机制比较健全,可能会不成功
post.addHeader("user-agent",
"Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36");
// 3、第三步:执行请求
response = httpclient.execute(post);
// 4、第四步:解析结果
int statusCode = response.getStatusLine().getStatusCode();
System.out.println(statusCode);
if (200 == statusCode) {
HttpEntity entity = response.getEntity();
String str = EntityUtils.toString(entity, "utf-8");
System.out.println(str);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != response) {
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != httpclient) {
try {
httpclient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
1.3.5 封装通用工具类HttpClientUtils
package com.xkt.utils;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.client.LaxRedirectStrategy;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
/**
*
* 封装HttpClient工具类
*
* @author Administrator
*
*/
public class HttpClientUtils {
public static String doGet(String url, Map<String, String> params) {
// 获取httpclient客户端
CloseableHttpClient httpclient = HttpClients.createDefault();
String resultString = "";
CloseableHttpResponse response = null;
try {
URIBuilder builder = new URIBuilder(url);
if (null != params) {
for (String key : params.keySet()) {
builder.setParameter(key, params.get(key));
}
}
HttpGet get = new HttpGet(builder.build());
response = httpclient.execute(get);
System.out.println(response.getStatusLine());
if (200 == response.getStatusLine().getStatusCode()) {
HttpEntity entity = response.getEntity();
resultString = EntityUtils.toString(entity, "utf-8");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != response) {
try {
response.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (null != httpclient) {
try {
httpclient.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return resultString;
}
public static String doGet(String url) {
return doGet(url, null);
}
public static String doPost(String url, Map<String, String> params) {
/**
* 在4.0及以上httpclient版本中,post需要指定重定向的策略,如果不指定则按默认的重定向策略。
*
* 获取httpclient客户端
*/
CloseableHttpClient httpclient = HttpClientBuilder.create().setRedirectStrategy(new LaxRedirectStrategy())
.build();
String resultString = "";
CloseableHttpResponse response = null;
try {
HttpPost post = new HttpPost(url);
List<NameValuePair> paramaters = new ArrayList<>();
if (null != params) {
for (String key : params.keySet()) {
paramaters.add(new BasicNameValuePair(key, params.get(key)));
}
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(paramaters);
post.setEntity(formEntity);
}
/**
* HTTP/1.1 403 Forbidden 原因: 有些网站,设置了反爬虫机制 解决的办法: 设置请求头,伪装浏览器
*/
post.addHeader("user-agent",
"Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36");
response = httpclient.execute(post);
System.out.println(response.getStatusLine());
if (200 == response.getStatusLine().getStatusCode()) {
HttpEntity entity = response.getEntity();
resultString = EntityUtils.toString(entity, "utf-8");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != response) {
try {
response.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (null != httpclient) {
try {
httpclient.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return resultString;
}
public static String doPost(String url) {
return doPost(url, null);
}
}
1.4 httpclient常见问题及解决方案
1.4.1 请求参数乱码
- 设置请求的编码格式:
obj.addHeader("Content-Type","application/x-www-form-urlencoded;charset=UTF-8");
1.4.2 响应HTTP/1.1 403 Forbidden
- 原因:网站设置了反爬虫机制,禁止非法访问。
- 解决方案:伪装浏览器。
obj.addHeader("User-Agent"," Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537. 36 (KHTML, like Gecko) Chrome/31.0.1650.63");
版权说明:欢迎以任何方式进行转载,但请在转载后注明出处!
来源:oschina
链接:https://my.oschina.net/u/4118479/blog/3046483