最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

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

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

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ù)不寫在鏈接上,單獨的傳進去,則可以這樣:

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

相關文章

  • spring boot 與kafka集成的示例代碼

    spring boot 與kafka集成的示例代碼

    這篇文章主要介紹了spring boot 與kafka集成的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-04-04
  • jstack+jdb命令查看線程及死鎖堆棧信息的實例

    jstack+jdb命令查看線程及死鎖堆棧信息的實例

    這篇文章主要介紹了jstack+jdb命令查看線程及死鎖堆棧信息的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-02-02
  • Java文件IO操作教程之DirectIO的意義

    Java文件IO操作教程之DirectIO的意義

    這篇文章主要給大家介紹了關于Java文件IO操作教程之DirectIO的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧
    2019-03-03
  • SpringAOP切入點規(guī)范及獲取方法參數(shù)的實現(xiàn)

    SpringAOP切入點規(guī)范及獲取方法參數(shù)的實現(xiàn)

    這篇文章主要介紹了SpringAOP切入點規(guī)范及獲取方法參數(shù),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-06-06
  • java 使用POI合并兩個word文檔

    java 使用POI合并兩個word文檔

    這篇文章主要介紹了java 使用POI合并兩個word文檔的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • SpringBoot+Redis實現(xiàn)后端接口防重復提交校驗的示例

    SpringBoot+Redis實現(xiàn)后端接口防重復提交校驗的示例

    本文將結合實例代碼,介紹SpringBoot+Redis實現(xiàn)后端接口防重復提交校驗的示例,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-06-06
  • java的arrays數(shù)組排序示例分享

    java的arrays數(shù)組排序示例分享

    排序算法,基本的高級語言都有一些提供。C語言有qsort()函數(shù),C++有sort()函數(shù),java語言有Arrays類(不是Array)。用這些排序時,都可以寫自己的排序規(guī)則
    2014-02-02
  • 詳細解讀Java編程中面向字符的輸入流

    詳細解讀Java編程中面向字符的輸入流

    這篇文章主要介紹了Java中面向字符的輸入和輸出流,是Java入門學習中的基礎知識,需要的朋友可以參考下
    2015-10-10
  • Java全面細致講解==和equals的使用

    Java全面細致講解==和equals的使用

    這篇文章主要介紹了Java中==和equals()的區(qū)別,,==可以使用在基本數(shù)據(jù)類型變量和引用數(shù)據(jù)類型變量中,equals()是方法,只能用于引用數(shù)據(jù)類型,需要的朋友可以參考下
    2022-05-05
  • 詳解Java 信號量Semaphore

    詳解Java 信號量Semaphore

    這篇文章主要介紹了Java 信號量Semaphore的相關資料,幫助大家更好的理解和學習Java并發(fā),感興趣的朋友可以了解下
    2020-09-09

最新評論

金平| 黄梅县| 云霄县| 青岛市| 丰台区| 江达县| 新郑市| 石阡县| 松江区| 东宁县| 霍山县| 南昌县| 文昌市| 牟定县| 乐东| 板桥市| 高淳县| 浦城县| 闸北区| 佳木斯市| 玉环县| 德化县| 北票市| 疏勒县| 金昌市| 股票| 涿州市| 通海县| 梅河口市| 宁武县| 金湖县| 富蕴县| 威信县| 灵璧县| 泾源县| 太白县| 华蓥市| 抚顺县| 大方县| 石楼县| 濮阳县|