博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HttpClient接口测试中的使用
阅读量:6629 次
发布时间:2019-06-25

本文共 3460 字,大约阅读时间需要 11 分钟。

作者:嗨刚好遇见你

链接:
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();}}}

转载地址:http://xlqpo.baihongyu.com/

你可能感兴趣的文章
云服务器离线安装MariaDB安装步骤和解决办法
查看>>
js手机短信按钮倒计时
查看>>
UIImage保存为JPG,PNG的方法
查看>>
权限管理
查看>>
QueryRunner 结果处理器
查看>>
Ant是什么
查看>>
Boot-col-sm布局
查看>>
Git彻底删除历史提交记录的方法
查看>>
pandas入门
查看>>
渐进增强 优雅降级
查看>>
angulajs中引用chart.js做报表,修改线条样式
查看>>
mysql之高可靠
查看>>
ubuntu中使用eclipse开发android,logcat显示问题
查看>>
学习英文之社区,博客及源码
查看>>
Virtual Machine Manager 2012 R2创建SQL 配置文件
查看>>
rsync同步工具基础介绍01
查看>>
SCOM 2012 R2监控Microsoft Azure服务(2)配置Azure监控
查看>>
统计客户端连接数
查看>>
CentOS6.5升级内核到3.10.28
查看>>
易宝典文章——用ISA 2006标准版发布Exchange 2010的OWA系列之创建Web侦
查看>>