使用CloseableHttpClient 模拟发送HttpGet和HttpPost请求
项目中经常会用到模拟Http请求,而jdk 下的 rt.jar核心库中也有 java.net提供了这方面的功能,但是总体而言,功能还是缺少灵活性和全面性,HttpClient的出现就是弥补了其缺失的功能。HttpClient不是浏览器客户端,而是一个客户端和服务端实现通信的Http组件库。本篇文章主要讲解CloseableHttpClient 的使用。
1. 项目中添加依赖的jar包
2.1 模拟HttpGet请求
/** * 模拟HttpGet 请求 * @param url * @return */ public static String HttpGet(String url){//单位毫秒 RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(3000).setConnectTimeout(3000).setSocketTimeout(3000).build();//设置请求的状态参数 CloseableHttpClient httpclient = HttpClients.createDefault();//创建 CloseableHttpClient HttpGet httpGet = new HttpGet(url); httpGet.setConfig(requestConfig); CloseableHttpResponse response = null; try {response = httpclient.execute(httpGet);//返回请求执行结果 int statusCode = response.getStatusLine().getStatusCode();//获取返回的状态值 if (statusCode != HttpStatus.SC_OK) {return null; } else {String result = EntityUtils.toString(response.getEntity(), "UTF-8"); return result; }} catch (Exception e) {logger.error("httpget Exception handle-- > " + e); } finally {if (response != null){try {response.close();//关闭response } catch (IOException e) {logger.error("httpget IOException handle-- > " + e); }}if(httpclient != null){try {httpclient.close();//关闭httpclient } catch (IOException e) {logger.error("httpget IOException handle-- > " + e); }}}return null; }
2.2 模拟HttpPost请求
/** * 模拟HttpPost请求 * @param url * @param jsonString * @return */ public static String HttpPost(String url, String jsonString){ CloseableHttpResponse response = null; CloseableHttpClient httpClient = HttpClientBuilder.create().build();//创建CloseableHttpClient HttpPost httpPost = new HttpPost(url);//实现HttpPost RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(3000).setConnectTimeout(3000).build(); httpPost.setConfig(requestConfig); //设置httpPost的状态参数 httpPost.addHeader("Content-Type", "application/json");//设置httpPost的请求头中的MIME类型为json StringEntity requestEntity = new StringEntity(jsonString, "utf-8"); httpPost.setEntity(requestEntity);//设置请求体 try {response = httpClient.execute(httpPost, new BasicHttpContext());//执行请求返回结果 if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {return null; }HttpEntity entity = response.getEntity(); if (entity != null) {String resultStr = EntityUtils.toString(entity, "utf-8"); return resultStr; } else {return null; }} catch (Exception e) {logger.error("httpPost method exception handle-- > " + e); return null; } finally {if (response != null){try {response.close();//最后关闭response } catch (IOException e) {logger.error("httpPost method IOException handle -- > " + e); }}if(httpClient != null){try {httpClient.close();} catch (IOException e) {logger.error("httpPost method exception handle-- > " + e);}}}}
使用CloseableHttpClient 模拟发送HttpGet和HttpPost请求相关推荐
- 使用firefox插件httperrequest,模拟发送及接收Json请求
目标:使用httpreques\Json-Handle\tcpdump\wireshark工具进行,抓取手机访问网络的包,分析request及response请求,通过httprequester来实现 ...
- 使用HttpClient模拟HTTP发送POST或GET请求
在Java开发中,服务与服务之间进行调用,需要使用HttpClient发送HTTP请求,以下是使用Java实现模拟HTTP发送POST或GET请求的代码 1.pom.xml中导入HTTP依赖 < ...
- Java模拟发送Http请求详细示例
对接第三方接口,肯定是需要我们自己模拟浏览器来发送请求的,有的文档中有demo,有demo改一改参数配置就好了,但有的接口却没有demo,只有一份接口参数介绍文档,这时候就需要我们自己来写发送请求的代 ...
- C#模拟http 发送post或get请求
C#模拟http 发送post或get请求 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 2 ...
- java string 包含http_Java中使用HttpPost上传文件以及HttpGet进行API请求(包含HttpPost上传文件)...
一.HttpPost上传文件 public static String getSuffix(final MultipartFile file){ if(file == null || file.get ...
- 使用谷歌浏览器模拟发送http请求
下载一个chromed的插件postman附上下载地址http://download.csdn.net/detail/zhenghui89/8490331;下载以后解压缩;打开谷歌浏览器以后 依次 ...
- jmeter测试TCP服务器/模拟发送TCP请求
jmeter测试TCP服务器,使用TCP采样器模拟发送TCP请求. TCP采样器:打开一个到指定服务器的TCP / IP连接,然后发送指定文本并等待响应. jmeter模拟发送TCP请求的方法: 1. ...
- 如何使用apiPOST进行模拟发送get、post、delete、put请求(支持文件上传)
现在的模拟发送请求插件很多,但亲测apiPOST更好用一些,因为它不仅可以模拟发送get.post.delete.put请求,还可以导出文档,中文界面更适合国内的程序员. 今天来分享如何使用apiPO ...
- 如何简单的模拟发送http post请求
有天在做项目演示的时候要用到post请求的模拟发送,为此总不至于写一个html页面,当时只记得百度了一下模拟发送http post请求,方法大概都是说用fiddler工具或者使用cmd内置telnet ...
最新文章
- Linux之链接命令
- linux 学习笔记 (五)
- Web.config配置文件的加密,解密及读写操作
- oracle间隔分区
- Windows环境下IOCP和SELECT模型性能比较
- dubbo的学习使用,第一章
- 计算机游戏和传统游戏的区别是什么,电竞显示器与普通显示器有什么区别-电脑自学网...
- linux应用程序文件丢失,详谈Ubuntu软件管理丢失的功能
- dataearth可视域分析_谁不知道前期分析要用ARCGIS?我就是不会用啊
- python清空列表_Python 内存分配时的小秘密
- Centos7安装完成找不到 ifconfig 网路设置命令
- Selenium WebDriver架构
- effective java 枚举_Effective.Java第34-44条(枚举)
- 一个很好的makefile例子(经典)
- Windows环境搭建Web自动化测试框架Watir(基于Ruby)
- 电脑版微信怎么双开、多开
- 云计算时代运维的出路在哪?
- 云社区博客博客详情火瞳智慧通行助力疫情防控及安全出行
- 英文字母间距非常大的问题
- 2023Mac装机系统优化软件CleanMyMacX