HttpClient在HTTP协议接口测试中的使用

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

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

 1 public void get(String url){
 2     CloseableHttpClient httpClient = null;
 3     HttpGet httpGet = null;
 4     try {
 5         httpClient = HttpClients.createDefault();
 6         RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(20000).setConnectTimeout(20000).build();     
 7         httpGet = new HttpGet(url);
 8         httpGet.setConfig(requestConfig);
 9         CloseableHttpResponse response = httpClient.execute(httpGet);
10         HttpEntity httpEntity = response.getEntity();
11         System.out.println(EntityUtils.toString(httpEntity,"utf-8"));
12     } catch (ClientProtocolException e) {
13         e.printStackTrace();
14     } catch (IOException e) {
15         e.printStackTrace();
16     }finally{
17         try {
18             if(httpGet!=null){
19                 httpGet.releaseConnection();
20             }
21             if(httpClient!=null){
22                 httpClient.close();
23             }
24         } catch (IOException e) {
25             e.printStackTrace();
26         }
27     }
28 }

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

 1 public void post(String url, Map<String, String> params){
 2     CloseableHttpClient httpClient = null;
 3     HttpPost httpPost = null;
 4     try {
 5         httpClient = HttpClients.createDefault();
 6         RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(20000).setConnectTimeout(20000).build();
 7         httpPost = new HttpPost(url);
 8         httpPost.setConfig(requestConfig);
 9         List<NameValuePair> ps = new ArrayList<NameValuePair>();
10         for (String pKey : params.keySet()) {
11             ps.add(new BasicNameValuePair(pKey, params.get(pKey)));
12         }
13         httpPost.setEntity(new UrlEncodedFormEntity(ps));
14         CloseableHttpResponse response = httpClient.execute(httpPost);
15         HttpEntity httpEntity = response.getEntity();
16         System.out.println(EntityUtils.toString(httpEntity,"utf-8"));
17     } catch (ClientProtocolException e) {
18         e.printStackTrace();
19     } catch (IOException e) {
20         e.printStackTrace();
21     }finally{
22         try {
23             if(httpPost!=null){
24                 httpPost.releaseConnection();
25             }
26             if(httpClient!=null){
27                 httpClient.close();
28             }
29         } catch (IOException e) {
30             e.printStackTrace();
31         }
32     }
33 }

三. POST请求的RAW参数传递:

 1 public void post(String url, String body){
 2     CloseableHttpClient httpClient = null;
 3     HttpPost httpPost = null;
 4     try {
 5         httpClient = HttpClients.createDefault();
 6         RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(20000).setConnectTimeout(20000).build();
 7         httpPost = new HttpPost(url);
 8         httpPost.setConfig(requestConfig);
 9         httpPost.setEntity(new StringEntity(body));
10         CloseableHttpResponse response = httpClient.execute(httpPost);
11         HttpEntity httpEntity = response.getEntity();
12         System.out.println(EntityUtils.toString(httpEntity,"utf-8"));
13     } catch (ClientProtocolException e) {
14         e.printStackTrace();
15     } catch (IOException e) {
16         e.printStackTrace();
17     }finally{
18         try {
19             if(httpPost!=null){
20                 httpPost.releaseConnection();
21             }
22             if(httpClient!=null){
23                 httpClient.close();
24             }
25         } catch (IOException e) {
26             e.printStackTrace();
27         }
28     }
29 }

原文:http://www.cnblogs.com/zhangfei

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏哲学驱动设计

090603 T LINQ的一个小问题

string strCityId = condition.CityId.ToString(); string startChar = strCityId + "...

20590
来自专栏.net core新时代

List,DataTable实现行转列的通用方案

  最近在做报表统计方面的需求,涉及到行转列报表。根据以往经验使用SQL可以比较容易完成,这次决定挑战一下直接通过代码方式完成行转列。期间遇到几个问题和用到的新...

25470
来自专栏跟着阿笨一起玩NET

把DataTable 转换成Json格式,适用于EasyUI 绑定DataGrid

本文转载:http://www.cnblogs.com/liang--liang/archive/2013/02/05/2893030.html

17020
来自专栏hbbliyong

C# 给枚举类型增加一个备注特性

/// <summary> /// 备注特性 /// </summary> public class RemarkAttribute...

36470
来自专栏跟着阿笨一起玩NET

C# 4.0命名参数和可选参数

Named And Optional Arguments - 命名参数和可选参数

7810
来自专栏liulun

WEB项目后端跨域请求

using System; using System.Collections.Generic; using System.IO; using System.Li...

212100
来自专栏c#开发者

C# : row-clickable GridView and get and set gridview rows using JavaScript

Complete C# code: ---------------- using System; using System.ComponentModel; ...

36360
来自专栏菩提树下的杨过

DataTable排序结果的纠正

默认情况下,即便db中某一列的值是数字,查询出来的DataSet/DataTable里,Column的类型都是String型,所以当用dataTable.Def...

22390
来自专栏魂祭心

原 数据字典+匿名委托模拟switch/c

30090
来自专栏程序员与猫

代码小目

标签: 代码片段 日常记录 日常记录的代码片段 1.使用Paralle进行并行计算累加求和的不同形式 public static int ParallelSum...

22470

扫码关注云+社区

领取腾讯云代金券