亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

基于HttpClient在HTTP協議接口測試中的使用(詳解)

 更新時間:2017年10月07日 10:43:37   作者:張飛_  
下面小編就為大家?guī)硪黄贖ttpClient在HTTP協議接口測試中的使用(詳解)。小編覺得挺不錯的,現在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

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協議接口測試中的使用(詳解)就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關文章

  • Spring Boot攔截器和過濾器實例解析

    Spring Boot攔截器和過濾器實例解析

    這篇文章主要介紹了Spring Boot攔截器和過濾器實例解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-01-01
  • Java實現屏幕截圖工具的代碼分享

    Java實現屏幕截圖工具的代碼分享

    這篇文章主要為大家介紹了如何利用Java語言編寫一個電腦屏幕截圖工具,文中的示例代碼講解詳細,對我們學習有一定的幫助,需要的可以參考一下
    2022-05-05
  • java如何讀取文件目錄返回樹形結構

    java如何讀取文件目錄返回樹形結構

    這篇文章主要介紹了java如何讀取文件目錄返回樹形結構問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-01-01
  • Spring Boot 中使用cache緩存的方法

    Spring Boot 中使用cache緩存的方法

    Spring Cache是Spring針對Spring應用,給出的一整套應用緩存解決方案。下面小編給大家?guī)砹薙pring Boot 中使用cache緩存的方法,感興趣的朋友參考下吧
    2018-01-01
  • Java 多用戶登錄限制的實現方法

    Java 多用戶登錄限制的實現方法

    最近沒有事情做,閑的發(fā)呆,于是寫個東東練練手。這篇文章主要介紹了Java 多用戶登錄限制的實現方法的相關資料,需要的朋友可以參考下
    2016-11-11
  • idea連接數據庫的操作方法

    idea連接數據庫的操作方法

    這篇文章主要介紹了idea如何連接數據庫,本文通過圖文并茂的形式給大家介紹的非常詳細,需要的朋友可以參考下
    2024-02-02
  • Spring Data JPA 建立表的聯合主鍵

    Spring Data JPA 建立表的聯合主鍵

    這篇文章主要介紹了Spring Data JPA 建立表的聯合主鍵。本文詳細的介紹了2種方式,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-04-04
  • Feign如何實現第三方的HTTP請求

    Feign如何實現第三方的HTTP請求

    這篇文章主要介紹了Feign如何實現第三方的HTTP請求,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-10-10
  • Java異常處理操作實例小結

    Java異常處理操作實例小結

    這篇文章主要介紹了Java異常處理操作,結合實例形式總結分析了java異常處理常見操作情況與相關處理技巧,需要的朋友可以參考下
    2019-07-07
  • springboot國際化多語言配置方式

    springboot國際化多語言配置方式

    這篇文章主要介紹了springboot國際化多語言配置方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-04-04

最新評論