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方式的请求雷同)