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技术的更多相关文章

  1. .Net Core 3.0后台使用httpclient请求网络网页和图片_使用Core3.0做一个简单的代理服务器

    目标:使用.net core最新的3.0版本,借助httpclient和本机的host域名代理,实现网络请求转发和内容获取,最终显示到目标客户端! 背景:本人在core领域是个新手,对core的使用不 ...

  2. HttpClient介绍和简单使用流程

    HttpClient SpringCloud中服务和服务之间的调用全部是使用HttpClient,还有前面使用SolrJ中就封装了HttpClient,在调用SolrTemplate的saveBean ...

  3. HttpClient介绍和使用

    HttpClient介绍和使用 今天有一个需求:后台访问一个接口,获取返回的数据.于是找到了HttpClient 1.介绍 SpringCloud中服务和服务之间的调用全部是使用HttpClient, ...

  4. HttpClient&&RestTemplate学习

    1. 什么是HttpClient HttpClient是Apache下面的子项目,可以提供高效的,最新的,功能丰富的支持HTTP协议的客户端编程工具包. 2. 为什么要学习HttpClient Htt ...

  5. springcloud微服务架构搭建:服务调用

    spring-cloud调用服务有两种方式,一种是Ribbon+RestTemplate, 另外一种是Feign. Ribbon是一个基于HTTP和TCP客户端的负载均衡器,类似nginx反向代理,可 ...

  6. SpringCloud2.0

    一.网站架构演变过程 从传统架构(单体应用)  到   分布式架构(以项目进行拆分)  到  SOA架构(面向服务架构)  到   微服务架构 1) 传统架构: 其实就是SSH或者SSM,属于单点应用 ...

  7. 项目介绍4 y有用

    在青岛做了两年开发,大大小小参与过三个项目的开发,一个是某公司内部的人员管理系统,一个是物流项目,最近做的是一个电商项目. 前两个项目采用的是ssh框架搭建的,最近的项目采用的是ssm框架搭建的.在实 ...

  8. 分布式链路监控与追踪系统Zipkin

    1.分布式链路监控与追踪产生背景2.SpringCloud Sleuth + Zipkin3.分布式服务追踪实现原理4.搭建Zipkin服务追踪系统5.搭建Zipkin集成RabbitMQ异步传输6. ...

  9. 防盗链&CSRF&API接口幂等性设计

    防盗链技术 CSRF(模拟请求) 分析防止伪造Token请求攻击 互联网API接口幂等性设计 忘记密码漏洞分析 1.Http请求防盗链 什么是防盗链 比如A网站有一张图片,被B网站直接通过img标签属 ...

随机推荐

  1. 如何通过sequel pro导入.sql脚本

    1.参考地址     https://zhidao.baidu.com/question/985373253463808219.html

  2. go生成xml

    package main import ( "encoding/xml" "fmt" // "os" ) type Servers stru ...

  3. ubuntu 16.04 系统语言汉化

    转载自:https://jingyan.baidu.com/article/5553fa82cedaa265a2393420.html

  4. sql 分组后查询最大所有列信息

    CREATE TABLE students (course varchar(10), stu_name varchar(10), city varchar(10), score int ) inser ...

  5. Python简单分布式爬虫

    分布式爬虫采用主从模式.主从模式是指由一台主机作为控制节点,负责管理所有运行网络爬虫的主机(url管理器,数据存储器,控制调度器),爬虫只需要从控制节点哪里接收任务,并把新生成任务提交给控制节点.此次 ...

  6. 防sql注入之参数绑定 SQL Injection Attacks and Defense

    http://php.net/manual/zh/pdo.prepared-statements.php 预处理语句与存储过程 很多更成熟的数据库都支持预处理语句的概念.什么是预处理语句?可以把它看作 ...

  7. extract

    w http://php.net/manual/en/function.extract.php <?php /* Suppose that $var_array is an array retu ...

  8. SOE 部署错误 ClassFactory cannot supply requested class

    问题描述: 部署完SOE,对某个服务启用部署的SOE时,出现错误信息,假如对地图服务SampleWorldCities启用刚部署的SOE,错误信息如下: service failed to start ...

  9. python并发编程&多线程(一)

    本篇理论居多,实际操作见:  python并发编程&多线程(二) 一 什么是线程 在传统操作系统中,每个进程有一个地址空间,而且默认就有一个控制线程 线程顾名思义,就是一条流水线工作的过程,一 ...

  10. Python3.6全栈开发实例[001]

    检查获取传入列表或元组对象的所有奇数位索引对应的元素,并将其作为新列表返回给调用者. li = [11,22,33,44,55,66,77,88,99,000,111,222] def func1(l ...