时间:2021-05-19
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 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<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(); } }}二、POST请求的表单提交方式,代码如下:
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(); } }}三、 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(); } }}以上这篇基于HttpClient在HTTP协议接口测试中的使用(详解)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
Android网络编程分为两种:基于http协议的,和基于socket的。基于Http协议:HttpClient、HttpURLConnection、Async
JSP开发中Apache-HTTPClient用户验证的实例详解前言:在微服务框架之外的系统中,我们经常会遇到使用httpClient进行接口调用的问题,除了进
前言前几话主要讲解关于使用golang进行单元测试,在单元测试的上一层就是接口测试,本节主要讲使用golang进行接口测试,其中主要以http协议的接口测试来讲
AsyncHttpHelp是一个android平台下基于httpclient开发的HTTP网络请求工具。优点功能齐全,提供常用的http网络访问接口。轻量级,无
Android中提供的HttpURLConnection和HttpClient接口可以用来开发HTTP程序。以下是学习中的一些经验。1、HttpURLConne