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

基于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 與kafka集成的示例代碼

    spring boot 與kafka集成的示例代碼

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

    jstack+jdb命令查看線(xiàn)程及死鎖堆棧信息的實(shí)例

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

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

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

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

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

    java 使用POI合并兩個(gè)word文檔

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

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

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

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

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

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

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

    Java全面細(xì)致講解==和equals的使用

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

    詳解Java 信號(hào)量Semaphore

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

最新評(píng)論

蚌埠市| 雅安市| 大埔区| 从化市| 阳西县| 江川县| 从化市| 乌海市| 余姚市| 黔南| 平泉县| 绍兴县| 陇川县| 隆昌县| 旬阳县| 彭阳县| 淮滨县| 镶黄旗| 保山市| 山丹县| 南投县| 延津县| 南乐县| 丹东市| 左权县| 阿坝县| 萨迦县| 鹤峰县| 扬中市| 呼图壁县| 濮阳市| 河间市| 甘孜县| 资兴市| 汽车| 武川县| 通江县| 老河口市| 天镇县| 项城市| 阿坝|