且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

HttpClient接口测试中的使用

更新时间:2022-08-13 20:20:28

作者:嗨刚好遇见你
链接:http://www.jianshu.com/p/3f15f1602513
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();

}

}

}