Hutools請求網絡資源使用的工具類:HttpRequest和HttpResponse
Get方式請求數據
Get方式請求數據
Map<String,Object> paramMap = new HashMap<>(); //map中帶不帶參數都可以這樣用 HttpResponse httpResponse = HttpRequest.get(url).form(paramMap).timeout(time * 1000).execute(); int status = httpResponse.getStatus(); System.out.println(httpResponse.body());
POST方式請求數據
(1)x-www-form-urlencoded形式的參數
Map<String,Object> paramMap = new HashMap<>();
HttpResponse httpResponse = HttpRequest.post(url).form(paramMap).timeout(time * 1000).execute();
int status = httpResponse.getStatus();
(2)application/json形式的參數
Map<String,Object> paramMap = new HashMap<>();
paramMap.put("username,"jerry");
Gson gson = new Gson();
String param = hson.toJson(paramMap); //轉換json數據
HttpResponse httpResponse = HttpRequest.post(url).body(param , "application/json").timeout(time * 1000).execute();
int status = httpResponse.getStatus
PUT方式請求數據(與POST方式的請求雷同)
