基于HttpClient在HTTP协议接口测试中的使用(详解)

网友投稿 364 2023-03-28


基于HttpClient在HTTP协议接口测试中的使用(详解)

HTTP协议的接口测试中,使用到最多的就是GET请求与POST请求,其中POST请求有FORM参数提交请求与RAW请求,下面我将结合HttpClient来实现一下这三种形式:

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

public void get(String url){

CloseableHttpClient httpClient = null;

HttpGet httpGet = null;

try {

httpClient = HttpClients.createDefault();

RequestConfig requhttp://estConfig = 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();

}

}

}


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:微信小程序富文本渲染引擎的详解
下一篇:大数据平台对外接口管理(大数据api接口)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~