使用 hutool 工具包发送 HTTP 请求

官方文档 http://hutool.cn/docs/#/http/Http请求-HttpRequest

maven依赖

<!--Hutool-->
<dependency>
	<groupId>cn.hutool</groupId>
	<artifactId>hutool-all</artifactId>
	<version>5.7.16</version>
</dependency>

使用

1.httpUtil使用post和get请求

	String url = "https://xxx/xx";//指定URL
	Map<String, Object> map = new HashMap<>();//存放参数
	map.put("A", 100);
	map.put("B", 200);
	HashMap<String, String> headers = new HashMap<>();//存放请求头,可以存放多个请求头
	headers.put("xxx", xxx);
	//发送get请求并接收响应数据
	String result= HttpUtil.createGet(url).addHeaders(headers).form(map).execute().body();
	//等价于HttpUtil.get(url).addHeaders(headers).form(map).execute().body();
	//发送post请求并接收响应数据
	String result= HttpUtil.createPost(url).addHeaders(headers).form(map).execute().body();
	//等价于HttpUtil.post(url).addHeaders(headers).form(map).execute().body();
	//无论是 HttpUtil.createGet/get 最后使用到的都是 HttpRequest.get
	//请求头添加方式有两种一种是上述这种。还有一种情况是下面 2. 一个一个添加这种情况。
	
	//向指定URL发送DELETE请求,并携带请求头headers。
	String url = "https://xxx/delete/"+id;//指定URL携带ID
	HashMap<String, String> headers = new HashMap<>();//存放请求头,可以存放多个请求头
	headers.put("xxx", xxx);
	//发送delete请求并接收响应数据
	String result= HttpUtil.createRequest(Method.DELETE, url).addHeaders(headers).execute().body();

HTTP请求头:参考文章


2.Http请求-HttpRequest
本质上,HttpUtil中的get和post工具方法都是HttpRequest对象的封装,因此如果想更加灵活操作Http请求,可以使用HttpRequest。

	/**
	* 通过链式构建请求,我们可以很方便的指定Http头信息和表单信息,
	* 最后调用execute方法即可执行请求,返回HttpResponse对象。HttpResponse包含了服务器响应的一些信息,包括响应的内容和响应的头信息。
	* 通过调用body方法即可获取响应内容。
	*/
	//链式构建请求
	String result2 = HttpRequest.post(url)
	    .header(Header.USER_AGENT, "Hutool http")//头信息,多个头信息多次调用此方法即可
	    .form(paramMap)//表单内容
	    .timeout(20000)//超时,毫秒
	    .execute().body();
	
	
    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)//等价于使用.form() 但是两者之间传递的参数类型不一样
            .execute().body();
       System.out.println(result);   


	//Restful请求
	String json = ...;
	String result2 = HttpRequest.post(url)
	    .body(json)
	    .execute().body();

在这里插入图片描述
如果有地方要求使用HTTP验证,可以使用basicAuth

String JPUSH_APP_KEY = "";
String JPUSH_MASTER_SECRET = "";
String result = HttpRequest.post("https://xxxx")
                .basicAuth(JPUSH_APP_KEY, JPUSH_MASTER_SECRET)
                .body(param.toString())
                .timeout(20000)
                .execute().body();

其它自定义项

同样,我们通过HttpRequest可以很方便的做以下操作:

  • 指定请求头
  • 自定义Cookie(cookie方法)
  • 指定是否keepAlive(keepAlive方法)
  • 指定表单内容(form方法)
  • 指定请求内容,比如rest请求指定JSON请求体(body方法)
  • 超时设置(timeout方法)
  • 指定代理(setProxy方法)
  • 指定SSL协议(setSSLProtocol)
  • 简单验证(basicAuth方法)

猜你喜欢

转载自blog.csdn.net/munangs/article/details/124256504