您好,登錄后才能下訂單哦!
本篇文章為大家展示了如何在HTTP協議接口測試中使用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(); } } }
上述內容就是如何在HTTP協議接口測試中使用HttpClient,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。