Use the hutool toolkit to send HTTP requests

Official document http://hutool.cn/docs/#/http/Http request-HttpRequest

maven dependencies

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

use

1. httpUtil uses post and get requests

	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 request header:reference article


2. Http request-HttpRequest
Essentially, the get and post tool methods in HttpUtil are the encapsulation of HttpRequest objects, so if you want to operate Http requests more flexibly, you can use 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();

insert image description here
If there is a requirement to use HTTP authentication, you can use 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();

Other customizations

Similarly, we can easily do the following operations through HttpRequest:

  • Specify request header
  • Custom Cookie (cookie method)
  • Specifies whether to keepAlive (keepAlive method)
  • Specify form content (form method)
  • Specify the content of the request, such as a rest request specifying the JSON request body (body method)
  • Timeout setting (timeout method)
  • Specify proxy (setProxy method)
  • Specifies the SSL protocol (setSSLProtocol)
  • Simple authentication (basicAuth method)

Guess you like

Origin blog.csdn.net/munangs/article/details/124256504