1. Get请求
//发送get请求并接收响应数据
String result = HttpUtil.createGet(url).addHeaders(headers).form(map).execute().body();
System.out.println(result);
2. POST请求
2.1 x-www-form-urlencoded形式的参数
//参数
Map<String,Object> paramMap = new HashMap<>();
paramMap.put("token", token);
HttpResponse httpResponse = HttpRequest.post(url).form(paramMap).execute();
int status = httpResponse.getStatus();
2.2 application/json形式的参数
Map<String, String> headers = generateHeader(urlStr, requestMethod, accessKey, secretKey);
// 使用headers来发起HTTP请求
Map<String, Object> parameters = new HashMap<>();
parameters.put("token", "debug_at_******88********5368");
// 发起请求
HttpResponse response = HttpRequest.post(urlStr)
.headerMap(headers, true) // 设置请求头
.body(JSON.toJSONString(parameters), "application/json") // 设置表单参数转成json格式
.execute();
3. PUT方式请求数据(与POST方式的请求相同)
3.1 x-www-form-urlencoded形式的参数
//参数
Map<String,Object> paramMap = new HashMap<>();
paramMap.put("token", token);
HttpResponse httpResponse = HttpRequest.put(url).form(paramMap).execute();
int status = httpResponse.getStatus();
3.2 application/json形式的参数
Map<String, String> headers = generateHeader(urlStr, requestMethod, accessKey, secretKey);
// 使用headers来发起HTTP请求
Map<String, Object> parameters = new HashMap<>();
parameters.put("userid", "**************");
// 发起请求
HttpResponse response = HttpRequest.put(urlStr)
.headerMap(headers, true) // 设置请求头
.body(JSON.toJSONString(parameters), "application/json") // 设置表单参数转成json格式
.execute();