HttpClient的简单使用
目录
- HttpClient的使用
- 一、maven坐标
- 二、 主要API
- 2.1 CloseableHttpClient
- 2.2 HttpClients
- 2.3 URIBuilder
- 2.4 HttpGet
- 2.5 HttpPost
- 2.6 HttpEntity
- 2.7 StringEntity
- 2.8 NameValuePair
- 2.9 UrlEncodedFormEntity
- 2.10 InputStreamEntity
- 2.11 CloseableHttpResponse
- 2.12 HttpEntity
- 2.13 EntityUtils
- 三、 案例代码
- 3.1 发送
不带请求参数的get请求
- 3.2 发送
携带请求参数的get请求
- 3.3 发送
不带请求参数以及其他请求体数据的post请求
- 3.4 发送
携带表单格式请求参数的post请求
- 3.5 发送
携带一般字符串请求体数据的post请求
- 3.6 发送
https的get请求
- 3.1 发送
- 四、 一个简单的HttpClientUtil
HttpClient的使用
一、maven坐标
<dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpclient</artifactId><version>4.5.2</version>
</dependency>
二、 主要API
2.1 CloseableHttpClient
表示Http客户端对象,用来发送请求并得到响应。线程安全,如果项目中只是偶尔httpclinet,建议每次使用时都新建、关闭此对象。如果项目中频繁使用httpclinet,建议把此对象作为单例使用;
httpClient.execute(request); // 执行get、post等请求
httpClient.close(); // 关闭此客户端对象,释放资源
2.2 HttpClients
用来创建HttpClinet对象的工具类
createDefault(); /创建默认的CloseableHttpClient对象
2.3 URIBuilder
用来构建URI,可以方便的在请求路径后面追加查询字符串
URI uri = new URIBuilder("http://localhost:8080/test")// 设置请求路径.setCharset(Charset.forName("UTF-8"))// 设置编码(默认为项目编码),一般不用设置.addParameter("k1", "v1")// 连续调用此方法可以设置多个请求参数.build();// 构建并返回URI对象
URI
表示一个资源路径,可以认为等价于URL
2.4 HttpGet
表示一个get请求对象
HttpGet httpGet = new HttpGet(uri);
httpGet.addHeader("h1", "v1");
2.5 HttpPost
表示一个post请求对象
HttpPost httpPost = new HttpPost("http://localhost:8080/test/test2");
httpPost.addHeader("h1", "v1");
2.6 HttpEntity
表示一个请求体
2.7 StringEntity
表示字符串请求体
StringEntity entity = new StringEntity("hello httpclient !", "UTF-8");
2.8 NameValuePair
表示一对键值对数据
NameValuePair param = new BasicNameValuePair("age", "16");
2.9 UrlEncodedFormEntity
表示进行URL编码的、符合表单提交格式的字符串请求体
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("name", "蛋蛋"));
params.add(new BasicNameValuePair("age", "16"));
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(params, "UTF-8");
httpPost.setEntity(entity);
2.10 InputStreamEntity
表示字节流形式的请求体
InputStreamEntity entity = new InputStreamEntity(instream);
2.11 CloseableHttpResponse
表示一个响应对象
CloseableHttpResponse httpResponse = httpClient.execute(httpPost);
httpResponse.headerIterator();//响应头迭代器
HttpEntity resultEntity = httpResponse.getEntity();//响应体数据
2.12 HttpEntity
表示一个响应体(和请求体是同一个类)
HttpEntity resultEntity = httpResponse.getEntity();
// 使用流的方式操作响应体数据
InputStream inputStream = resultEntity.getContent();
// 如果响应体是字符串数据,可能需要字符编码信息
Header header = resultEntity.getContentEncoding();
2.13 EntityUtils
可以方便取出响应体数据的工具类,因为此工具类会把所有响应体数据全部缓存到内存中,所以如果响应体数据量较大的话建议直接使用httpEntity.getContent()获取到响应体的读取流,进行流操作;
HttpEntity resultEntity = httpResponse.getEntity();
//此方法会自动根据响应头中的编码信息对响应体内容进行编码,也可以手动指定编码
String result = EntityUtils.toString(resultEntity);
三、 案例代码
注意:
httpclinet可自动管理cookie,所以支持session会话
- 如果发送请求或者接收响应过程中出现的IOException,httpclinet默认会重新发送请求(尝试5次),可能会造成服务器重复处理此请求,如果此请求会修改数据库,就可能造成数据错乱。所以如果某个请求不允许这种情况出现的话,可以禁用重复发送功能;
CloseableHttpClient httpclient = HttpClients.custom()
.setRetryHandler(new StandardHttpRequestRetryHandler(0, false)).build();
3.1 发送不带请求参数的get请求
CloseableHttpClient httpClient = null;
CloseableHttpResponse httpResponse = null;
try {httpClient = HttpClients.createDefault();HttpGet httpGet = new HttpGet("http://localhost:8080/test");httpResponse httpResponse = httpClient.execute(httpGet);HttpEntity entity = httpResponse.getEntity();String result = EntityUtils.toString(entity);// 处理result的代码...
} finally {if (httpResponse != null) {httpResponse.close();}if (httpClient != null) {httpClient.close();}
}
3.2 发送携带请求参数的get请求
CloseableHttpClient httpClient = null;
CloseableHttpResponse httpResponse = null;
try {httpClient = HttpClients.createDefault();URI uri = new URIBuilder("http://localhost:8080/test")// 设置请求路径.setCharset(Charset.forName("UTF-8"))// 设置编码(默认为项目编码),一般不用设置.addParameter("k1", "v1")// 连续调用此方法可以设置多个请求参数.build();// 构建并返回URI对象HttpGet httpGet = new HttpGet(uri);httpResponse = httpClient.execute(httpGet);HttpEntity entity = httpResponse.getEntity();String result = EntityUtils.toString(entity);// 处理result的代码...
} finally {if (httpResponse != null) {httpResponse.close();}if (httpClient != null) {httpClient.close();}
}
3.3 发送不带请求参数以及其他请求体数据的post请求
CloseableHttpClient httpClient = null;
CloseableHttpResponse httpResponse = null;
try {httpClient = HttpClients.createDefault();HttpPost httpPost = new HttpPost("http://localhost:8080/test");httpResponse = httpClient.execute(httpPost);HttpEntity entity = httpResponse.getEntity();String result = EntityUtils.toString(entity);// 处理result的代码...
} finally {if (httpResponse != null) {httpResponse.close();}if (httpClient != null) {httpClient.close();}
}
3.4 发送携带表单格式请求参数的post请求
CloseableHttpClient httpClient = null;
CloseableHttpResponse httpResponse = null;
try {httpClient = HttpClients.createDefault();HttpPost httpPost = new HttpPost("http://localhost:8080/test");List<NameValuePair> params = new ArrayList<NameValuePair>();params.add(new BasicNameValuePair("name", "Hali"));params.add(new BasicNameValuePair("age", "16"));UrlEncodedFormEntity entity = new UrlEncodedFormEntity(params, "UTF-8");httpPost.setEntity(entity);httpResponse = httpClient.execute(httpPost);HttpEntity resultEntity = httpResponse.getEntity();String result = EntityUtils.toString(resultEntity);// 处理result的代码...
} finally {if (httpResponse != null) {httpResponse.close();}if (httpClient != null) {httpClient.close();}
}
3.5 发送携带一般字符串请求体数据的post请求
CloseableHttpClient httpClient = null;
CloseableHttpResponse httpResponse = null;
try {httpClient = HttpClients.createDefault();HttpPost httpPost = new HttpPost("http://localhost:8080/test");StringEntity entity = new StringEntity("hello httpclient !", "UTF-8");httpPost.setEntity(entity);httpResponse = httpClient.execute(httpPost);HttpEntity resultEntity = httpResponse.getEntity();String result = EntityUtils.toString(resultEntity);// 处理result的代码...
} finally {if (httpResponse != null) {httpResponse.close();}if (httpClient != null) {httpClient.close();}
}
3.6 发送 https的get请求
/*** 创建一个可以访问Https类型URL的工具类,返回一个CloseableHttpClient实例*/
public static CloseableHttpClient createSSLClientDefault(){try {SSLContext sslContext=new SSLContextBuilder().loadTrustMaterial(null,new TrustStrategy() {//信任所有public boolean isTrusted(X509Certificate[] chain, String authType)throws CertificateException {return true;}}).build();SSLConnectionSocketFactory sslsf=new SSLConnectionSocketFactory(sslContext);return HttpClients.custom().setSSLSocketFactory(sslsf).build();} catch (KeyManagementException e) {e.printStackTrace();} catch (NoSuchAlgorithmException e) {e.printStackTrace();} catch (KeyStoreException e) {e.printStackTrace();}return HttpClients.createDefault();
}
/*** @throws IOException * @throws ClientProtocolException * */
public static void main(String[] args) throws ClientProtocolException, IOException {//从工具方法中获得对应的可以访问Https的httpClientCloseableHttpClient httpClient =createSSLClientDefault(); HttpGet httpGet=new HttpGet("https://etrade.ccbfund.cn/etrading/tradereq/main.do?method=doInit&isHome=1&menuId=10000");//自己先在浏览器登录一下,自行复制具体的CookiehttpGet.setHeader("Cookie", "HS_ETS_SID=4jSFY2wWwT0gPrWJ45ly!-1286216704; Null=31111111.51237.0000; logtype=2; certtype=0; certNo=33****************; isorgloginpage_cookie=0; hs_etrading_customskin=app_css");//设置代理,方便Fiddle捕获具体信息RequestConfig config=RequestConfig.custom().setProxy(HttpHost.create("127.0.0.1:8888")).build();httpGet.setConfig(config);//执行get请求,获得对应的响应实例CloseableHttpResponse response=httpClient.execute(httpGet);//打印响应的到的html正文HttpEntity entity =response.getEntity();String html=EntityUtils.toString(entity);System.out.println(html);//关闭连接response.close();httpClient.close();
}
四、 一个简单的HttpClientUtil
地址: https://github.com/lunarku/Code_Snippets/blob/master/CodeSnippets/jdk/src/main/java/jdk/util/http/HttpClientUtils.java
转载于:https://www.cnblogs.com/jxkun/p/9399102.html
HttpClient的简单使用相关推荐
- HttpURLConnection和HttpClient的简单用法
HttpURLConnection的简单用法:先通过一个URL创建一个conn对象,然后就是可以设置get或者是post方法,接着用流来读取响应结果即可 String html = null;long ...
- java爬虫之基于httpclient的简单Demo(二)
转载自 java爬虫之基于httpclient的简单Demo(二) 延续demo1的 java爬虫的2种爬取方式(HTTP||Socket)简单Demo(一),demo2出炉啦,大家想学爬虫都可以从这 ...
- httpclient 的简单示例
2019独角兽企业重金招聘Python工程师标准>>> 建立project,从maven repositories中导入httpclient.版本 java 1.8 httpcli ...
- JAVA——基于HttpComponents(HttpClient)的简单网络爬虫DEMO
基本概念 HttpComponents(HttpClient): 超文本传输协议(HTTP)可能是当今Internet上使用的最重要的协议.Web服务,支持网络的设备和网络计算的增长继续将HTTP ...
- HttpURLConnection与HttpClient提交FORM表单参数请求工具类
来吧,小宝贝!!!!!!一个小白在项目过程中遇到的问题,给你们分享一下哈!!!!! 先看下我们请求的方式与请求体: 不难看出哈,请求的参数体并没有什么难点,那我为什么还要做一下总结呢?真的可能因为我太 ...
- Android客户端连接服务器- OKHttp的简单实用方法
文章目录 一 .OKHttp简介 二. OkHttp3使用 1.创建HTTPClient实例 2.Get请求 3.POST请求 三.Timeouts(超时) **写在前面:本文只是对OKHttp3的简 ...
- JAVA网络爬爬学习之HttpClient+Jsoup
JAVA网络爬爬学习 HttpClient用法简单整理 GET请求 无参 带参 POST请求 无参 带参 连接池 请求request的相关配置 httpclient用法详解 Jsoup用法简单整理 j ...
- 模拟登陆CSDN——就是这么简单
工具介绍 本篇文章主要是讲解如何模拟登陆CSDN,使用的工具是HttpClient+Jsoup 其中HttpClient主要是负责发送请求,而Jsoup主要是解析HTML 你可能对HttpClient ...
- HttpComponents和HttpClient基本用法
HttpClient是什么? HTTP 协议是 Internet 上使用得最多.最重要的协议之一,越来越多的 Java 应用程序需要直接通过 HTTP 协议来访问网络资源. 虽然在 JDK 的 jav ...
最新文章
- 决策树(Decision Tree)、决策树的构建、决策树流程、树的生长、熵、信息增益比、基尼系数
- 基因组测序数据分析:测序技术
- 教育安全认证体系建设项目容灾备份体系建设项目
- 如何学习Python数据爬虫?
- hdu 5396 Expression
- 什么是python中子类父类_零基础入门:python中子类继承父类的__init__方法实例
- 用PHP去掉文件头的Unicode签名(BOM)
- matlab 局部特征检测与提取(问题与特征)
- 2009年南京生活小结
- LCD驱动芯片ST7789V
- 用户体验衡量指标分析
- python在冒号处显示语法错误_python中的语法错误
- 转]自己开心一下!!!很轻松的~
- mysql 军规_58到家MySQL军规升级版
- 生成式模型与辨别式模型
- 三极管的经典之作,你知道吗?
- 微信小程序低功耗蓝牙
- 安装linux双系统简书,win10 + ubuntu18.04 双系统的安装
- 【回首2022,展望2023,兔年你好!】
- 全球100家杂志网站(转)