HttpClient技术
1.什么是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();
}
} }
}
HttpClient技术的更多相关文章
- .Net Core 3.0后台使用httpclient请求网络网页和图片_使用Core3.0做一个简单的代理服务器
目标:使用.net core最新的3.0版本,借助httpclient和本机的host域名代理,实现网络请求转发和内容获取,最终显示到目标客户端! 背景:本人在core领域是个新手,对core的使用不 ...
- HttpClient介绍和简单使用流程
HttpClient SpringCloud中服务和服务之间的调用全部是使用HttpClient,还有前面使用SolrJ中就封装了HttpClient,在调用SolrTemplate的saveBean ...
- HttpClient介绍和使用
HttpClient介绍和使用 今天有一个需求:后台访问一个接口,获取返回的数据.于是找到了HttpClient 1.介绍 SpringCloud中服务和服务之间的调用全部是使用HttpClient, ...
- HttpClient&&RestTemplate学习
1. 什么是HttpClient HttpClient是Apache下面的子项目,可以提供高效的,最新的,功能丰富的支持HTTP协议的客户端编程工具包. 2. 为什么要学习HttpClient Htt ...
- springcloud微服务架构搭建:服务调用
spring-cloud调用服务有两种方式,一种是Ribbon+RestTemplate, 另外一种是Feign. Ribbon是一个基于HTTP和TCP客户端的负载均衡器,类似nginx反向代理,可 ...
- SpringCloud2.0
一.网站架构演变过程 从传统架构(单体应用) 到 分布式架构(以项目进行拆分) 到 SOA架构(面向服务架构) 到 微服务架构 1) 传统架构: 其实就是SSH或者SSM,属于单点应用 ...
- 项目介绍4 y有用
在青岛做了两年开发,大大小小参与过三个项目的开发,一个是某公司内部的人员管理系统,一个是物流项目,最近做的是一个电商项目. 前两个项目采用的是ssh框架搭建的,最近的项目采用的是ssm框架搭建的.在实 ...
- 分布式链路监控与追踪系统Zipkin
1.分布式链路监控与追踪产生背景2.SpringCloud Sleuth + Zipkin3.分布式服务追踪实现原理4.搭建Zipkin服务追踪系统5.搭建Zipkin集成RabbitMQ异步传输6. ...
- 防盗链&CSRF&API接口幂等性设计
防盗链技术 CSRF(模拟请求) 分析防止伪造Token请求攻击 互联网API接口幂等性设计 忘记密码漏洞分析 1.Http请求防盗链 什么是防盗链 比如A网站有一张图片,被B网站直接通过img标签属 ...
随机推荐
- ios - UILabel_长按复制
1.添加长按的手势 UILongPressGestureRecognizer *longGesture = [[UILongPressGestureRecognizer alloc] initWith ...
- vmware 安装配置 ,记住这一次不要再问我了。ok?
Linux 安装配置 ,记住这一次不要再问我了.ok? 第一步 选择版本 如果遇到问题无法自动获取的 老男孩教育-李泳谊<youjiu_linux@qq.com> 17:51:43明天开 ...
- windows下用py2exe打包脚本为可双击运行程序
文件夹结构: ├── readme.txt ├── settings.py #程序参数 ├── settings.pyc ├── setup.py #安装文件 ├── spider.ico ...
- MySQL左连接查询
1.语法: select 字段列表 from table1 别名1 left join table2 别名2 on 连接条件 [where 子句]
- 构造三层时报错“程序 “D:\MyTest\....”不包含适合于入口点的静态"Main"方法”
错误 1 程序“D:\MyTest\EBookShop\Model\obj\x86\Debug\Model.exe”不包含适合于入口点的静态“Main”方法 原因:原来创建项目的时候,用的是“空项目” ...
- Xmind 8 update 4 破解教程(破解补丁+破解步骤+下载地址)
一.原始教程 原教程很详细,直接参考: http://www.voidcn.com/article/p-yyybmcqq-bnz.html 若无法访问请点击:http://df1551e3.wiz03 ...
- python多线程/多进程
thread和threading的区别 threading相对与thread是更高级别的线程管理模块 thread和threading模块中的一些属性会有冲突 thread模块拥有的同步原因实际上只有 ...
- 【python】-- Django
Django Python的WEB框架有Django.Tornado.Flask 等多种,Django相较与其他WEB框架其优势为:大而全,框架本身集成了ORM.模型绑定.模板引擎.缓存.Sessio ...
- JavaScript确定一个字符串是否包含在另一个字符串中的四种方法
一.indexOf() 1.定义 indexOf()方法返回String对象第一次出现指定字符串的索引,若未找到指定值,返回-1.(数组同一个概念) 2.语法 str.indexOf(searchVa ...
- 洛谷 P1407 [国家集训队]稳定婚姻
洛谷 这个题面很有意思,像我这样的菜鸡,完全不需考虑婚姻的稳定 性 问题. tarjan裸题,直接讲算法吧: 原配夫妻之间分别连一条边,小情人之间反向连边. 这时候我们会发现一个性质,如果婚姻稳定,那 ...