gpt4 book ai didi

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

转载 作者:qq735679552 更新时间:2022-09-28 22:32:09 24 4
gpt4 key购买 nike

CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.

这篇CFSDN的博客文章基于HttpClient在HTTP协议接口测试中的使用(详解)由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.

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

1、GET请求: 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
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();
     }
   }
}

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

?
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
29
30
31
32
33
34
35
36
37
38
public void get(String url, Map< String , String> 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();
     }
   }
}

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

?
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
29
30
31
32
33
public void post(String url, Map< String , String> 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< NameValuePair > ps = new ArrayList< NameValuePair >();
     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();
     }
   }
}

3、 POST请求的RAW参数传递:

?
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
29
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();
     }
   }
}

以上这篇基于HttpClient在HTTP协议接口测试中的使用(详解)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持我.

原文链接:http://www.cnblogs.com/zhangfei/p/5099036.html 。

最后此篇关于基于HttpClient在HTTP协议接口测试中的使用(详解)的文章就讲到这里了,如果你想了解更多关于基于HttpClient在HTTP协议接口测试中的使用(详解)的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com