欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2017年10月07日 10:43:37   作者:張飛_  
下面小編就為大家?guī)?lái)一篇基于HttpClient在HTTP協(xié)議接口測(cè)試中的使用(詳解)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧

HTTP協(xié)議的接口測(cè)試中,使用到最多的就是GET請(qǐng)求與POST請(qǐng)求,其中POST請(qǐng)求有FORM參數(shù)提交請(qǐng)求與RAW請(qǐng)求,下面我將結(jié)合HttpClient來(lái)實(shí)現(xiàn)一下這三種形式:

一、GET請(qǐng)求: GET請(qǐng)求時(shí),參數(shù)一般是寫(xiě)在鏈接上的,代碼如下:

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();
    }
  }
}

如果想把參數(shù)不寫(xiě)在鏈接上,單獨(dú)的傳進(jìn)去,則可以這樣:

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請(qǐng)求的表單提交方式,代碼如下:

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請(qǐng)求的RAW參數(shù)傳遞:

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

相關(guān)文章

  • Spring Boot攔截器和過(guò)濾器實(shí)例解析

    Spring Boot攔截器和過(guò)濾器實(shí)例解析

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

    Java實(shí)現(xiàn)屏幕截圖工具的代碼分享

    這篇文章主要為大家介紹了如何利用Java語(yǔ)言編寫(xiě)一個(gè)電腦屏幕截圖工具,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)有一定的幫助,需要的可以參考一下
    2022-05-05
  • java如何讀取文件目錄返回樹(shù)形結(jié)構(gòu)

    java如何讀取文件目錄返回樹(shù)形結(jié)構(gòu)

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

    Spring Boot 中使用cache緩存的方法

    Spring Cache是Spring針對(duì)Spring應(yīng)用,給出的一整套應(yīng)用緩存解決方案。下面小編給大家?guī)?lái)了Spring Boot 中使用cache緩存的方法,感興趣的朋友參考下吧
    2018-01-01
  • Java 多用戶(hù)登錄限制的實(shí)現(xiàn)方法

    Java 多用戶(hù)登錄限制的實(shí)現(xiàn)方法

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

    idea連接數(shù)據(jù)庫(kù)的操作方法

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

    Spring Data JPA 建立表的聯(lián)合主鍵

    這篇文章主要介紹了Spring Data JPA 建立表的聯(lián)合主鍵。本文詳細(xì)的介紹了2種方式,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2019-04-04
  • Feign如何實(shí)現(xiàn)第三方的HTTP請(qǐng)求

    Feign如何實(shí)現(xiàn)第三方的HTTP請(qǐng)求

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

    Java異常處理操作實(shí)例小結(jié)

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

    springboot國(guó)際化多語(yǔ)言配置方式

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

最新評(píng)論