博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java分享第十一天(接口测试)
阅读量:6657 次
发布时间:2019-06-25

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

 HTTP协议的接口测试中,使用到最多的就是GET请求与POST请求,其中POST请求有FORM参数提交请求与RAW请求

post请求时有一个选项是form-data,或者raw,使用raw可以请求成功,from-data不知道怎么组装key和value所以一直失败。非常不明白raw是什么意思,google百度都没有相关的解释。后来研究发现,其实raw方式使用的是纯字符串的数据上传方式,所以在POST之前,可能需要手工的把一些json/text/xml格式的数据转换成字符串,是一种post原始请求,区别于form-data这种常用的key-value方式

),下面我将结合HttpClient来实现一下这三种形式:
一.GET请求: GET请求时,参数一般是写在链接上的,代码如下:
public void get(String url){    CloseableHttpClient httpClient = null;    HttpGet httpGet = null;    try {        httpClient = HttpClients.createDefault();        RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(20000).setConnectTimeout(20000).build();             httpGet = new HttpGet(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();        }    }}
 如果想把参数不写在链接上,单独的传进去,则可以这样: 
public void get(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 = new HttpGet(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请求的表单提交方式,代码如下: 
public void post(String url, Map
params){ CloseableHttpClient httpClient = null; HttpPost httpPost = null; try { httpClient = HttpClients.createDefault(); RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(20000).setConnectTimeout(20000).build(); httpPost = new HttpPost(url); httpPost.setConfig(requestConfig); List
ps = new ArrayList
(); for (String pKey : params.keySet()) { ps.add(new BasicNameValuePair(pKey, params.get(pKey))); } httpPost.setEntity(new UrlEncodedFormEntity(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参数传递 
public void post(String url, String body){    CloseableHttpClient httpClient = null;    HttpPost httpPost = null;    try {        httpClient = HttpClients.createDefault();        RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(20000).setConnectTimeout(20000).build();        httpPost = new HttpPost(url);        httpPost.setConfig(requestConfig);        httpPost.setEntity(new StringEntity(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();        }    }}

 

转载于:https://www.cnblogs.com/tiancy/p/6041922.html

你可能感兴趣的文章
Spring 通过工厂方法(Factory Method)来配置bean
查看>>
Android 资源保护问题——探索
查看>>
修改!important定义的样式(2)
查看>>
mac下PHP安装mongo扩展
查看>>
腾讯前端面试
查看>>
C++STL之algorithm(一)
查看>>
bzoj千题计划211:bzoj1996: [Hnoi2010]chorus 合唱队
查看>>
bzoj千题计划321:bzoj5251: [2018多省省队联测]劈配(网络流 + 二分)
查看>>
PHP通过串口发短信
查看>>
Objective-C数组和字典
查看>>
mysql登录基本语句
查看>>
废掉一个人最隐蔽的方式,是让他忙到没时间成长(转)
查看>>
二维数组的遍历
查看>>
页面布局(5)——三栏自适应布局(左右定宽中间自适应)
查看>>
【集成学习】sklearn中xgboost模块的XGBClassifier函数
查看>>
装系统遇到的那些问题
查看>>
修改文件
查看>>
成为七牛云 Contributor -如何贡献 logkit 代码
查看>>
apt-get upgarde 和dist-upgrade的差别
查看>>
Difference Between Arraylist And Vector : Core Java Interview Collection Question
查看>>