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标签属 ...
随机推荐
- CMakeList相关
cmake使用示例与整理总结 http://blog.csdn.net/wzzfeitian/article/details/40963457/ 对应的demo:https://github.com/ ...
- Eclipse调试部分手机不显示日志问题解决
在拨号键盘输入一串指令,然后会进入到工程模式,最后可以在Log设置里面设置了. 华为:*#*#2846579#*#* 酷派:*20121220#
- tomcat添加crt证书
使用keytool生成证书苹果手机添加后提示未验证,可以使用Apache的openssl生成证书导入到tomcat中. 使用Apache 生成证书:openssl genrsa 4096 > s ...
- ios数据存储方式FMDB
本文转载至 http://blog.csdn.net/chen505358119/article/details/9289489 分类: ios2013-07-10 14:05 2471人阅读 评论( ...
- Android自定义Button字体颜色和背景颜色
http://blog.csdn.net/breeze666/article/details/7747649
- 阻塞(sleep等等)区别 中断(interrupt)+ 中断的意义
不客气地说,至少有一半人认为,线程的"中断"就是让线程停止.如果你也这么认为,那你对多线程编程还没有入门. 在java中,线程的中断(interrupt)只是改变了线程的中断状态, ...
- Avoiding Full Table Scans
w MySQL :: MySQL 5.7 Reference Manual :: 9.2.1.19 Avoiding Full Table Scanshttps://dev.mysql.com/doc ...
- MySQL中自适应哈希索引
自适应哈希索引采用之前讨论的哈希表的方式实现,不同的是,这仅是数据库自身创建并使用的,DBA本身并不能对其进行干预.自适应哈希索引近哈希函数映射到一个哈希表中,因此对于字典类型的查找非常快速,如SEL ...
- Hash表的C++实现(转)
原文:Hash表(C++实现) 哈希表的几个概念: 映像:由哈希函数得到的哈希表是一个映像. 冲突:如果两个关键字的哈希函数值相等,这种现象称为冲突. 处理冲突的几个方法: 1.开放地址法:用开放地址 ...
- Python3.6全栈开发实例[004]
4.计算传入函数的字符串中, 数字.字母.空格以及其他内容的个数,并返回结果. s1 = 'wan%$#(gwdwq\nwdhuaiww3 w02041718' def func1(s1): dic ...