SDK 放入pom中:
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>4.1.2</version>
</dependency>
httpUtil使用post和get请求
//指定URL
String url = "https://xxx";
//存放参数
Map mapParam = new HashMap<>();
map.put("A", 100);
map.put("B", 200);
//存放请求头,可以存放多个请求头
HashMap headers = new HashMap<>();
headers.put("xxx", xxx);
//发送get请求并接收响应数据
String result= HttpUtil.createGet(url).addHeaders(headers)
.form(mapParam).execute().body();
//发送post请求并接收响应数据
String result= HttpUtil.createPost(url).body(mapParam)
.addHeaders(headers).execute().body();
test
@Test
public void testHttps() throws Exception {
JSONObject json = new JSONObject();
json.put("username", "xxxx");
json.put("password", "123456.");
String result = HttpRequest.post("https://xxx/users")
.header("Content-Type", "application/json")
.body(json)
.execute().body();
System.out.println(result);
}
post demo
public Response downloadFile(String fileId, String userName) {
String url ="www.baidu.com"
String jsonParams = JSON.toJSONString(new xxxx(fileId, userName));
Map<String, String> header = new HashMap<>();
String body = cn.hutool.http.HttpUtil.createPost(url)
.body(jsonParams).addHeaders(akSkHeader)
.execute().body();
//返回的结果转为json
return JSON.parseObject(body,Response.class); }
get demo
public String getDemo(){
URI uri ="www.baidu.com";
//存放请求头,可以存放多个请求头
HashMap<String, String> headers = new HashMap<>();
addConsoleHeadersMap(headers);
//请求头加上headers.put("Accept", "application/json")
headers.put("Accept", "application/json");
HashMap<String, Object> params = new HashMap<>();
params.put("systemId", systemId);
//发送get请求并接收响应数据
String result = HttpUtil.createGet(uri.toString())
.addHeaders(headers).form(params).execute().body();
return result;
}