1.添加依赖
cn.hutool hutool-all4.1.2
2.发送get或者post 并携带指定参数与请求头headers。
String url = "https://xxx/xx";//指定URL Mapmap = 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(map).execute().body(); //发送post请求并接收响应数据 String result= HttpUtil.createPost(url).addHeaders(headers).form(map).execute().body();
3.HttpRequest多个headers
@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);
}
同样,我们通过HttpRequest可以很方便的做以下操作:
指定请求头
自定义cookie(cookie方法)
指定是否keepAlive(keepAlive方法)
指定表单内容(form方法)
指定请求内容,比如rest请求指定JSON请求体(body方法)
超时设置(timeout方法)
指定代理(setProxy方法)
指定SSL协议(setSSLProtocol)
简单验证(basicAuth方法)
4.向指定URL发送DELETE请求,并携带请求头headers。
String url = "https://xxx/delete/"+id;//指定URL携带ID HashMapheaders = new HashMap<>();//存放请求头,可以存放多个请求头 headers.put("xxx", xxx); //发送delete请求并接收响应数据 String result= HttpUtil.createRequest(Method.DELETE, url).addHeaders(headers).execute().body();
5.官网 Hutool参考文档Hutool,Java工具集https://www.hutool.cn/docs/#/
更多内容请关注:
欢迎您欢迎您https://qicaitun.com/



