kevinsawicki發送HTTP(S)請求


kevinsawicki是一個小巧又便捷的HTTP(S)請求利器。有時,如果只是向做快捷簡易的HTTP(S)請求,那么kevinsawicki將會是一種不錯的選擇。

聲明:本人測試時軟硬件環境為:Windows10、idea、SpringBoot-2.0.5.RELEASE

准備工作:除了SpringBoot基本依賴外,還需引入kevinsawicki依賴
<!-- https://mvnrepository.com/artifact/com.github.kevinsawicki/http-request -->
<dependency>
<groupId>com.github.kevinsawicki</groupId>
<artifactId>http-request</artifactId>
<version>6.0</version>
</dependency>
 

說明:本人發送請求的代碼寫在單元測試里,請求的URL是寫在同一個項目的controller包下的。

GET請求示例:
/**
* GET請求測試
*
* @date 2018/10/31 14:37
*/
@Test
public void getRequestTest() throws UnsupportedEncodingException {
// 如果URL中含有特殊的字符值,那么需要編碼一下
String symbolValue = URLEncoder.encode("&", "UTF-8");
String url = "http://127.0.0.1:8080/get?name=lisi&age=18&symbol=" + symbolValue;
HttpRequest httpRequest = HttpRequest.get(url);
// 執行請求,並返回請求體數據
String responseBody = httpRequest.body();
System.out.println(responseBody);
}
該請求對應的Controlelr層中的方法是:

/**
* 測試 -> GET參請求
*
* @date 2018/10/31 14:04
*/
@RequestMapping(value = "/get", method = RequestMethod.GET)
public String getController(@RequestParam("name") String name, Integer age, String symbol) {
String str = "age:" + name + "\nage:" + age + "\nsymbol:" + symbol;
return "歡迎來到getController! -> \n" + str;
}
執行該請求,輸出結果為:

 

 

 

 

POST請求(數據放在請求體中)示例:
/**
* POST請求體 --- 測試
*
* @date 2018/10/31 14:37
*/
@Test
public void postRequestTest() {
HttpRequest httpRequest = new HttpRequest("http://127.0.0.1:8080/post/requestBody", "POST");
httpRequest.contentType("application/json", "UTF-8");
// 將請求體信息放入send中
httpRequest.send("i am data 我是json數據!");
System.out.println(httpRequest.body());
}
該請求對應的Controlelr層中的方法是:

/**
* 測試 -> POST請求體
*
* @date 2018/10/31 14:04
*/
@RequestMapping(value = "/post/requestBody", method = RequestMethod.POST)
public String requestBodyController(@RequestBody String str) {
return "歡迎來到requestBodyController! -> \n" + str;
}
執行該請求,輸出結果為:

 

 

 

 

 

發送表單信息(以POST)示例:
/**
* 表單數據 --- 測試
*
* @date 2018/10/31 14:37
*/
@Test
public void formTest() {
Map<String, Object> data = new HashMap<>(8);
data.put("name", "JustryDeng");
data.put("age", 24);
HttpRequest httpRequest = new HttpRequest("http://127.0.0.1:8080/post/formInfo", "POST");
httpRequest.form(data);
System.out.println(httpRequest.body());
}
該請求對應的Controlelr層中的方法是:

/**
* 測試 -> 表單信息
*
* @date 2018/10/31 14:04
*/
@RequestMapping(value = "/post/formInfo", method = RequestMethod.POST)
public String formInfoController(@RequestParam("name") String name, Integer age) {
String str = "age:" + name + "\nage:" + age;
return "歡迎來到formInfoController! -> \n" + str;
}
執行該請求,輸出結果為:

 

 

 

 

 


請求頭域信息設置示例:
/**
* 請求頭域測試
* 注:頭域里面的key,是不區分大小寫的;
* 值還是會區分大小寫的
*
* @date 2018/10/31 14:37
*/
@Test
public void headerTest() {
HttpRequest httpRequest = new HttpRequest("http://127.0.0.1:8080/requestHeadInfo", "GET");
// 往請求頭域中添加信息的同通用方式
httpRequest.header("my-name", "鄧沙利文");
httpRequest.header("my-gender", "男");
httpRequest.header("my-age", "24");
httpRequest.header("my-motto", "我是一只小小小小鳥~");
// 請求頭域中經常會用到的的信息,HttpRequest又專門給我們提供了一些方法,如:
httpRequest.authorization("AuThoriZatIon");
httpRequest.contentType("text/xml", "UTF-8");
System.out.println(httpRequest.body());
}
該請求對應的Controlelr層中的方法是:

/**
* 測試 -> 請求頭
* 注:頭域中的key(name)不區分大小寫
* value區分大小寫
*
* @date 2018/10/31 14:04
*/
@RequestMapping(value = "/requestHeadInfo", method = {RequestMethod.GET, RequestMethod.POST})
public String requestHeadInfoController(HttpServletRequest request) throws UnsupportedEncodingException {
StringBuilder sb = new StringBuilder(32);
Enumeration<String> headerNames = request.getHeaderNames();
while (headerNames.hasMoreElements()) {
// 獲取到請求頭中的key、value
String key = preventGarbledHandler(headerNames.nextElement());
String value = preventGarbledHandler(request.getHeader(key));
sb.append("key(name)為:").append(key);
sb.append("\tvalue為:").append(value);
sb.append("\n");
}
// 請求域中的key,是不區分大小寫的;所以下面這四個都能取出來
System.out.println("第一次取值:" + request.getHeader("authorization"));
System.out.println("第二次取值:" + request.getHeader("Authorization"));
System.out.println("第三次取值:" + request.getHeader("AUTHORization"));
System.out.println("第四次取值:" + request.getHeader("authoriZAtion"));
return "歡迎來到requestHeadInfoController! -> \n" + sb;
}

/**
* 防亂碼處理
*/
private String preventGarbledHandler(String originStr) throws UnsupportedEncodingException {
return new String(originStr.getBytes("ISO-8859-1"), "UTF-8");
}
執行該請求,輸出結果為:

 

 

 

 

 

對比請求時設置的authorization的值以及該controller方法的四個輸出,可以驗證【頭域中的key(name)不區分大小寫,value區分大小寫】:

 

 

 

 

 

HTTPS:
發送HTTPS請求准備工作:提供需要HTTPS訪問的API。
注:因為我是用HTTPS訪問自己的Controller,所以需要提供HTTPS訪問的API,如果是用HTTPS訪問別人,那
     么無需此准備工作。
注:如果配置了HTTPS后,如果沒有再配置HTTP,那么會發現前面的HTTP請求沒法訪問了。本人為了快速
     測試說明,就不再詳細說明如何提供HTTP/HTTPS都可訪問的API了。
      可詳見筆者這篇博客https://blog.csdn.net/justry_deng/article/details/82684505。

HTTPS請求示例:
/**
* HTTPS請求測試
*
* @date 2018/10/31 14:37
*/
@Test
public void httpsTest() throws UnsupportedEncodingException {
// 如果URL中含有特殊的字符值,那么需要編碼一下
String name = URLEncoder.encode("鄧沙利文", "UTF-8");
HttpRequest httpsRequest = HttpRequest.post("https://127.0.0.1:8080/https?name=" + name);
//Accept all certificates
httpsRequest.trustAllCerts();
//Accept all hostnames
httpsRequest.trustAllHosts();
System.out.println(httpsRequest.body());
}
該請求對應的Controlelr層中的方法是:

/**
* 測試 -> HTTPS
*
* @date 2018/10/31 21:09
*/
@RequestMapping(value = "/https", method = {RequestMethod.GET, RequestMethod.POST})
public String httpsController(@RequestParam("name") String name) {
return name + "進入HTTPS了!HTTPS調用成功!";
}
執行該請求,輸出結果為:

 

 

 

 

 

 

注:kevinsawicki源碼清晰明了,建議有時間閱讀。

注:本節是對https://github.com/kevinsawicki/http-request/tree/master中列舉出來的kevinsawicki各個功
     能中的部分功能的示例。更多細節、更多功能建議自行參看該鏈接。

 
————————————————
版權聲明:本文為CSDN博主「justry_deng」的原創文章,遵循CC 4.0 BY-SA版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/justry_deng/article/details/83591505


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM