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. Ubuntu安装特定版本安装包

    Ubuntu安装特定版本安装包可以用aptitude,aptitude是apt-get的高级版,使用起来更强大. aptitude install package=version 比如我要安装2.6. ...

  2. 添加可点击的imagebottom,有个点击动画效果

    android添加可点击的按钮,有个动画切换 ,首先定义一个 res/drawable中定义一个 btn_shutter_background.xml,其定义举例如下: <?xml versio ...

  3. OpenCV学习笔记:opencv_ml模块

    一,简介 该模块为opencv的机器学习(machine learning,ml)代码库,包含各种机器学习算法: 0, class CvStatModel ; class CvMLData; stru ...

  4. Android开发:《Gradle Recipes for Android》阅读笔记1.5

    这节讲的是如何如何添加JAVA依赖库. 默认的android项目有两个build.gradle文件,分别位于顶级目录,和应用自己的目录下(通常放在一个叫app的目录下面). gradle支持多种方式列 ...

  5. 使用jQuery lazyload 实现图片延迟加载

    下载地址 使用说明 0. 准备工作 下载jQuery和lazyload 插件(地址如上) 1. HTML 引入jQuery库和lazyload插件 <div id="imagesCon ...

  6. 前端基础------JS

    JS中的语句要以分号 ;  为结束符. JS语言基础: 1, JS 的变量名可以使用 下划线, 数字, 字母, $ 组成. 不可以是数字开头 2, 声明变量使用var 变量名. 的格式来进行声明. v ...

  7. 【BZOJ1941】[Sdoi2010]Hide and Seek KDtree

    [BZOJ1941][Sdoi2010]Hide and Seek Description 小猪iPig在PKU刚上完了无聊的猪性代数课,天资聪慧的iPig被这门对他来说无比简单的课弄得非常寂寞,为了 ...

  8. Sql注入_mysql权限入侵

    实验:测试不同数据库用户的操作权限 文件读写测试:load_file() ,into outfile 数据库用户账号密码存储在mysql.user下 Mysql最高权限用户root: Mysql普通权 ...

  9. Markov chain

    w https://en.wikipedia.org/wiki/Markov_chain https://zh.wikipedia.org/wiki/马尔科夫链 In probability theo ...

  10. java集合的部分接口

    接口 Collection<E> public interface Collection<E>extends Iterable<E> Collection 层次结构 ...