Hutool工具類之HttpUtil使用Https

關於Hutool工具類之HttpUtil如何使用能夠參考官方文檔Hutool之HttpUtil編程

其實使用Http和Https使用的方式是同樣的。json

建議你們能夠看看HttpUtil的源碼,感受設計的挺不錯的。api

導入Maven依賴

<dependency>
   <groupId>cn.hutool</groupId>
   <artifactId>hutool-all</artifactId>
   <version>4.1.0</version>
</dependency>

編寫測試類(使用Junit單元測試)

@Test public void testHttps() throws Exception { JSONObject json = new JSONObject(); json.put("username", "1332788xxxxxx"); json.put("password", "123456."); String result = HttpRequest.post("https://api2.bmob.cn/1/users") .header("Content-Type", "application/json") .header("X-Bmob-Application-Id","2f0419a31f9casdfdsf431f6cd297fdd3e28fds4af") .header("X-Bmob-REST-API-Key","1e03efdas82178723afdsafsda4be0f305def6708cc6") .body(json) .execute().body(); System.out.println(result); }

方法解釋(上面採用的是一種叫鏈式編程的方式):
header對應的是請求頭。
body對應的是請求體(包含參數和參數值)。
HttpRequest裏面包含Post、GET、Delete、Put等經常使用的RestFul方式。session

打印以下:app

{"createdAt":"2019-04-30 10:42:07","objectId":"6cfdb77081","sessionToken":"269e433440c9e65b8058d016df703ccb"}
相關文章
相關標籤/搜索