首页 技术 正文
技术 2022年11月19日
0 收藏 524 点赞 2,166 浏览 3202 个字

1.什么是HttpClient?

HttpClient技术

2.HttpClient特点?

特点:

2.1. 基于标准、纯净的Java语言。实现了Http1.0和Http1.1

2.2. 以可扩展的面向对象的结构实现了Http全部的方法(GET, POST, PUT, DELETE, HEAD, OPTIONS, and TRACE)。

2.3. 支持HTTPS协议。

2.4. 通过Http代理建立透明的连接。

2.5. 自动处理Set-Cookie中的Cookie。

3.HttpClient作用?

其主要作用就是通过Http协议,向某个URL地址发起请求,并且获取响应结果。

4.关于httpclient的工具类:

@Service
public class ApiService { // 创建Httpclient对象
@Autowired(required=false)
private CloseableHttpClient httpclient; /**
* 无参的GET请求
* @param url
* @return
* @throws Exception
* @throws IOException
*/
public String doGet(String url) throws Exception, IOException{ // 创建http GET请求
HttpGet httpGet = new HttpGet(url); CloseableHttpResponse response = null;
try {
// 执行请求
response = httpclient.execute(httpGet);
// 判断返回状态是否为200
if (response.getStatusLine().getStatusCode() == 200) {
String data = EntityUtils.toString(response.getEntity(), "UTF-8");
return data;
}
} finally {
if (response != null) {
response.close();
}
}
return null;
} // 创建Httpclient对象 模拟浏览器发起访问 打开浏览器 public HttpclientResult doPost(String url) throws ClientProtocolException, IOException{ // 创建http POST请求 输入地址
HttpPost httpPost = new HttpPost(url); httpPost.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36");
CloseableHttpResponse response = null;
try {
// 执行请求 敲回车
response = httpclient.execute(httpPost);
// 判断返回状态是否为201
Integer code = response.getStatusLine().getStatusCode();
if (code == 201) {
String data = EntityUtils.toString(response.getEntity(), "UTF-8"); HttpclientResult result = new HttpclientResult(code, data); return result;
}
} finally {
if (response != null) {
response.close();
}
}
return null;
} /**
* 有参GET
* @param url
* @return
* @throws Exception
* @throws IOException
*/
public String doGetParam(String url,Map<String,String> params) throws Exception, IOException{ // 定义请求的参数
URIBuilder uriBuilder = new URIBuilder(url); if(params!=null && !params.isEmpty()){
for(String key:params.keySet()){
uriBuilder.setParameter(key, params.get(key));
}
} URI uri = uriBuilder.build(); // 创建http GET请求
HttpGet httpGet = new HttpGet(uri); CloseableHttpResponse response = null;
try {
// 执行请求
response = httpclient.execute(httpGet);
// 判断返回状态是否为200
if (response.getStatusLine().getStatusCode() == 200) {
String data = EntityUtils.toString(response.getEntity(), "UTF-8");
return data;
}
} finally {
if (response != null) {
response.close();
}
}
return null;
} /**
* 有参post
* @param url
* @return
* @throws ClientProtocolException
* @throws IOException
*/
public HttpclientResult doPostParam(String url,Map<String,String> params) throws ClientProtocolException, IOException{ // 创建http POST请求 输入地址
HttpPost httpPost = new HttpPost(url); if(params!=null&&!params.isEmpty()){
List<NameValuePair> parameters = new ArrayList<NameValuePair>(0);
for (String key : params.keySet()) {
parameters.add(new BasicNameValuePair(key, params.get(key)));
}
// 构造一个form表单式的实体
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(parameters);
// 将请求实体设置到httpPost对象中
httpPost.setEntity(formEntity);
}// 模拟浏览器访问
httpPost.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36");// 调用接口访问对象 CloseableHttpResponse response = null;
try {
// 执行请求 敲回车
response = httpclient.execute(httpPost);
// 判断返回状态是否为201
Integer code = response.getStatusLine().getStatusCode();
String data = EntityUtils.toString(response.getEntity(), "UTF-8"); HttpclientResult result = new HttpclientResult(code, data); return result; } finally {
if (response != null) {
response.close();
}
} }
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,083
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,558
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,407
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,180
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,817
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,900