OKHttpUtil Features
- Automatically determines whether a URL is HTTP or HTTPS without requiring additional code.
- Cookies are recorded by default, enabling features like simulated login where subsequent requests remain authenticated after the initial login.
- Automatically handles 304 redirects and subsequent requests.
- Supports proxy configuration.
- Allows referer configuraton.
- Supports User-Agent configuration.
- Automatically identifies and decompresses Gzip-encoded responses.
- Compatible with Spring Boot configuration files.
- Provides a minimalist, encapsulated API for calls.
Using OKHttpUtil
1. Maven Dependency
<dependency>
<groupId>io.github.admin4j</groupId>
<artifactId>http</artifactId>
<version>0.4.0</version>
</dependency>
2. GET Request
The simplest usage envolves using the HttpUtil utility class to quickly request an endpoint.
Response response = HttpUtil.get("https://github.com/search", Pair.of("q", "okhttp"));
System.out.println("Response: " + response);
3. POST Request
Sending a POST request is straightforward.
// Example with a JSON body
Response postResult = HttpUtil.post(
"https://oapi.dingtalk.com/robot/send?access_token=27f595...",
"{\"msgtype\": \"text\", \"text\": {\"content\":\"Test Message\"}}"
);
System.out.println("POST Result: " + postResult);
// Example with form data
Map<String, Object> formData = new HashMap<>();
formData.put("username", "admin");
formData.put("password", "admin123");
Response formResponse = HttpUtil.postForm("http://192.168.1.13:9100/auth/login", formData);
System.out.println("Form Response: " + formResponse);
For JSON responses, use HttpJsonUtil to automatically parse the result into a JSONObject.
JSONObject result = HttpJsonUtil.get(
"https://github.com/search",
Pair.of("q", "http"),
Pair.of("username", "agonie201218")
);
System.out.println("JSON Result: " + result);
4. File Upload
Uploading a file can be done by including it in the form parameters.
File uploadFile = new File("C:\\Users\\andanyang\\Downloads\\Sql.txt");
Map<String, Object> uploadParams = new HashMap<>();
uploadParams.put("key", "test");
uploadParams.put("file", uploadFile);
uploadParams.put("token", "WXyUseb-D4sCum-EvTIDYL-mEehwDtrSBg-Zca7t...");
Response uploadResponse = HttpUtil.upload("https://upload.qiniup.com/", uploadParams);
System.out.println(uploadResponse);
5. File Download
Download a file to a specified directory.
HttpUtil.download("https://gitee.com/admin4j/common-http", "local/path/");
6. Chained HttpRequest
The library supports a fluent, chained API for building requests.
// GET request with query parameters and headers
Response chainedGet = HttpRequest.get("https://search.gitee.com/?skin=rec&type=repository")
.queryParam("q", "admin4j")
.header(HttpHeaderKey.USER_AGENT, "admin4j")
.execute();
System.out.println("Chained GET: " + chainedGet);
// POST request with form data
Response chainedPost = HttpRequest.post("http://192.168.1.13:9100/auth/login")
.queryParam("q", "admin4j")
.header(HttpHeaderKey.USER_AGENT, "admin4j")
.formField("username", "admin")
.formField("password", "admin123")
.execute();
System.out.println("Chained POST: " + chainedPost);