Recommend a useful third-party package for java http-request

github address: https://github.com/kevinsawicki/http-request
is similar to the python requests package, which is simple to call and does not need to deal with complex data streams.

The following is a reference to the Chinese usage instructions of the peers. If there is no maven, you can copy the source code. Just one file, refer to the hub usage instructions.
Original link: www.jianshu.com
In our daily work, we often need to communicate with third-party interfaces. Many times we use the http protocol to interact, and java natively supports http (java.net.* ), but it is not very convenient to use. In addition, the most used is the apache httpclient toolkit. But in terms of personal use for so long, I feel that whether it is native or httpclient, it is not easy to use, and it is slightly complicated

to introduce maven .
<dependency>
  <groupId>com.github.kevinsawicki</groupId>
  <artifactId>http-request</artifactId>
  <version>5.6</version>
</dependency>

Example
Send a Get request to get the response message
String response = HttpRequest.get("http://www.baidu.com").body();
System.out.println("Response was: "+response);

Send a Get request with query parameters to get the response message
Writing method 1:

HttpRequest request = HttpRequest.get("http://www.baidu.com",true,'q',"baseball gloves","size",100);
System.out.println(request.toString());// GET http://www.baidu.com?q=baseball%20gloves&size=100


Writing 2:

Map data = new HashMap();
data.put("q", "baseball gloves");
data.put("size", "100");
String resp =HttpRequest.get("http://www.baidu.com")form(data).body();
System.out.println("---------------response parameter:" + resp);


Similarly, to send a Post request, just replace the get method with Post.
Send a request to upload an attachment
HttpRequest request=HttpRequest.post("http://google.com");
request.part("status[body]","Making a multipart request");
request.part("status[image]",newFile("/home/kevin/Pictures/ide.png"));
if(request.ok()){
    System.out.println("Status was updated");
}

Common http request configuration
HttpRequest request = HttpRequest.get("https://google.com");
//trust all certificates
request.trustAllCerts();
// trust all addresses
request.trustAllHosts();
//Set the request timeout
request.connectTimeout(60000);
//Set the read timeout
request.readTimeout(60000);


The above settings support Builder mode

String resp = HttpRequest.post("http://www.baidu.com").trustAllCerts().trustAllHosts()
.form(data)
.connectTimeout(60000)
.readTimeout(60000)
.body();

Configure http proxy
HttpRequest request = HttpRequest.get("https://google.com");
//Configure proxy
request.useProxy("localhost", 8080);
//Optional proxy basic authentication
request.proxyBasic("username", "p4ssw0rd");

Summary
This tool class is convenient and easy to use, without any dependent libraries, and the source code has only one class HttpRequest, which is concise and clear, and supports chained calls in Builder mode. Interested students can try to use it, and it is good to use it. For more usage, please check the official website for examples.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326097494&siteId=291194637