HttpClient接口测试中的使用

简介: 作者:嗨刚好遇见你链接:http://www.jianshu.com/p/3f15f1602513TTP协议的接口测试中,使用到最多的就是GET请求与POST请求,其中POST请求有FORM参数提交请求与RAW请求,下面我将结合HttpClient来实现一下这三种形式:一.

作者:嗨刚好遇见你
链接:http://www.jianshu.com/p/3f15f1602513
TTP协议的接口测试中,使用到最多的就是GET请求与POST请求,其中POST请求有FORM参数提交请求与RAW请求,下面我将结合HttpClient来实现一下这三种形式:

一.GET请求: GET请求时,参数一般是写在链接上的,代码如下:

publicvoidget(String url){

CloseableHttpClient httpClient =null;

HttpGet httpGet =null;

try{

httpClient = HttpClients.createDefault();

RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(20000).setConnectTimeout(20000).build();

httpGet =newHttpGet(url);

httpGet.setConfig(requestConfig);

CloseableHttpResponse response = httpClient.execute(httpGet);

HttpEntity httpEntity = response.getEntity();

System.out.println(EntityUtils.toString(httpEntity,"utf-8"));

}catch(ClientProtocolException e) {

e.printStackTrace();

}catch(IOException e) {

e.printStackTrace();

}finally{

try{

if(httpGet!=null){

httpGet.releaseConnection();

}

if(httpClient!=null){

httpClient.close();

}

}catch(IOException e) {

e.printStackTrace();

}

}

}

如果想把参数不写在链接上,单独的传进去,则可以这样:

publicvoidget(String url, Map params){

CloseableHttpClient httpClient =null;

HttpGet httpGet =null;

try{

httpClient = HttpClients.createDefault();

RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(20000).setConnectTimeout(20000).build();

String ps ="";

for(String pKey : params.keySet()) {

if(!"".equals(ps)){

ps = ps +"&";

}

ps = pKey+"="+params.get(pKey);

}

if(!"".equals(ps)){

url = url +"?"+ ps;

}

httpGet =newHttpGet(url);

httpGet.setConfig(requestConfig);

CloseableHttpResponse response = httpClient.execute(httpGet);

HttpEntity httpEntity = response.getEntity();

System.out.println(EntityUtils.toString(httpEntity,"utf-8"));

}catch(ClientProtocolException e) {

e.printStackTrace();

}catch(IOException e) {

e.printStackTrace();

}finally{

try{

if(httpGet!=null){

httpGet.releaseConnection();

}

if(httpClient!=null){

httpClient.close();

}

}catch(IOException e) {

e.printStackTrace();

}

}

}

二. POST请求的表单提交方式,代码如下:

publicvoidpost(String url, Map params){

CloseableHttpClient httpClient =null;

HttpPost httpPost =null;

try{

httpClient = HttpClients.createDefault();

RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(20000).setConnectTimeout(20000).build();

httpPost =newHttpPost(url);

httpPost.setConfig(requestConfig);

List ps =newArrayList();

for(String pKey : params.keySet()) {

ps.add(newBasicNameValuePair(pKey, params.get(pKey)));

}

httpPost.setEntity(newUrlEncodedFormEntity(ps));

CloseableHttpResponse response = httpClient.execute(httpPost);

HttpEntity httpEntity = response.getEntity();

System.out.println(EntityUtils.toString(httpEntity,"utf-8"));

}catch(ClientProtocolException e) {

e.printStackTrace();

}catch(IOException e) {

e.printStackTrace();

}finally{

try{

if(httpPost!=null){

httpPost.releaseConnection();

}

if(httpClient!=null){

httpClient.close();

}

}catch(IOException e) {

e.printStackTrace();

}

}

}

三. POST请求的RAW参数传递

publicvoidpost(String url, String body){

CloseableHttpClient httpClient =null;

HttpPost httpPost =null;

try{

httpClient = HttpClients.createDefault();

RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(20000).setConnectTimeout(20000).build();

httpPost =newHttpPost(url);

httpPost.setConfig(requestConfig);

httpPost.setEntity(newStringEntity(body));

CloseableHttpResponse response = httpClient.execute(httpPost);

HttpEntity httpEntity = response.getEntity();

System.out.println(EntityUtils.toString(httpEntity,"utf-8"));

}catch(ClientProtocolException e) {

e.printStackTrace();

}catch(IOException e) {

e.printStackTrace();

}finally{

try{

if(httpPost!=null){

httpPost.releaseConnection();

}

if(httpClient!=null){

httpClient.close();

}

}catch(IOException e) {

e.printStackTrace();

}

}

}
目录
相关文章
|
3天前
|
存储 关系型数据库 分布式数据库
PostgreSQL 18 发布,快来 PolarDB 尝鲜!
PostgreSQL 18 发布,PolarDB for PostgreSQL 全面兼容。新版本支持异步I/O、UUIDv7、虚拟生成列、逻辑复制增强及OAuth认证,显著提升性能与安全。PolarDB-PG 18 支持存算分离架构,融合海量弹性存储与极致计算性能,搭配丰富插件生态,为企业提供高效、稳定、灵活的云数据库解决方案,助力企业数字化转型如虎添翼!
|
14天前
|
弹性计算 关系型数据库 微服务
基于 Docker 与 Kubernetes(K3s)的微服务:阿里云生产环境扩容实践
在微服务架构中,如何实现“稳定扩容”与“成本可控”是企业面临的核心挑战。本文结合 Python FastAPI 微服务实战,详解如何基于阿里云基础设施,利用 Docker 封装服务、K3s 实现容器编排,构建生产级微服务架构。内容涵盖容器构建、集群部署、自动扩缩容、可观测性等关键环节,适配阿里云资源特性与服务生态,助力企业打造低成本、高可靠、易扩展的微服务解决方案。
1303 5
|
13天前
|
机器学习/深度学习 人工智能 前端开发
通义DeepResearch全面开源!同步分享可落地的高阶Agent构建方法论
通义研究团队开源发布通义 DeepResearch —— 首个在性能上可与 OpenAI DeepResearch 相媲美、并在多项权威基准测试中取得领先表现的全开源 Web Agent。
1330 87
|
2天前
|
弹性计算 安全 数据安全/隐私保护
2025年阿里云域名备案流程(新手图文详细流程)
本文图文详解阿里云账号注册、服务器租赁、域名购买及备案全流程,涵盖企业实名认证、信息模板创建、域名备案提交与管局审核等关键步骤,助您快速完成网站上线前的准备工作。
184 82
2025年阿里云域名备案流程(新手图文详细流程)
|
7天前
|
前端开发
Promise的then方法返回的新Promise对象的状态为“失败(Rejected)”时,链式调用会如何执行?
Promise的then方法返回的新Promise对象的状态为“失败(Rejected)”时,链式调用会如何执行?
242 127