官网
https://square.github.io/okhttp/recipes/
快速入门
添加 jar 到 pom.xml
<!-- https://mvnrepository.com/artifact/com.squareup.okhttp3/okhttp --><dependency><groupId>com.squareup.okhttp3</groupId><artifactId>okhttp</artifactId><version>4.9.3</version></dependency>
测试代码
package basic_okhttp;import lombok.SneakyThrows;import okhttp3.*;import okhttp3.Request.Builder;import org.junit.jupiter.api.Test;import java.io.File;import java.io.IOException;/*** The type Okhttp study.*/public class OkhttpStudy {private final OkHttpClient client = new OkHttpClient();private final MediaType MEDIA_TYPE_MARKDOWN = MediaType.parse("text/x-markdown; charset=utf-8");/*** Test get.*/@SneakyThrows@Testvoid testGet() {Request request = new Builder().url("https://publicobject.com/helloworld.txt").build();try (Response response = client.newCall(request).execute()) {if (!response.isSuccessful()) {throw new IOException("Unexpected code " + response);}Headers responseHeaders = response.headers();for (int i = 0; i < responseHeaders.size(); i++) {System.out.println(responseHeaders.name(i) + ": " + responseHeaders.value(i));}System.out.println(response.body().string());}}/*** 测试添加请求头*/@SneakyThrows@Testvoid testHeaders() {Request request = new Request.Builder().url("https://api.github.com/repos/square/okhttp/issues").header("User-Agent", "OkHttp Headers.java").addHeader("Accept", "application/json; q=0.5").addHeader("Accept", "application/vnd.github.v3+json").build();try (Response response = client.newCall(request).execute()) {if (!response.isSuccessful()) {throw new IOException("Unexpected code " + response);}System.out.println("Server: " + response.header("Server"));System.out.println("Date: " + response.header("Date"));System.out.println("Vary: " + response.headers("Vary"));}}/*** 测试 Post 请求*/@SneakyThrows@Testvoid testPost() {String postBody = """Releases--------*_ 1.0_ May 6, 2013* _1.1_ June 15, 2013* 1.2_ August 11, 2013""";Request request = new Request.Builder().url("https://api.github.com/markdown/raw").post(RequestBody.create(postBody, MEDIA_TYPE_MARKDOWN)).build();try (Response response = client.newCall(request).execute()) {if (!response.isSuccessful()) {throw new IOException("Unexpected code " + response);}System.out.println(response.body().string());}}/*** 测试提交文件请求*/@SneakyThrows@Testvoid testFile() {File file = new File("README.md");Request request = new Request.Builder().url("https://api.github.com/markdown/raw").post(RequestBody.create(file, MEDIA_TYPE_MARKDOWN)).build();try (Response response = client.newCall(request).execute()) {if (!response.isSuccessful()) {throw new IOException("Unexpected code " + response);}System.out.println(response.body().string());}}/*** 测试 form 表单*/@SneakyThrows@Testvoid testForm() {RequestBody formBody = new FormBody.Builder().add("search", "Jurassic Park").build();Request request = new Request.Builder().url("https://en.wikipedia.org/w/index.php").post(formBody).build();try (Response response = client.newCall(request).execute()) {if (!response.isSuccessful()) {throw new IOException("Unexpected code " + response);}System.out.println(response.body().string());}}}
工具类封装
枚举类
主要封装不同的 Content-type,方便管理
package com.polo.testplatform.enums;
import lombok.AllArgsConstructor;
import lombok.Getter;
import okhttp3.MediaType;
/**
* The enum Okhttp mediatype.
*/
@Getter
@AllArgsConstructor
public enum OkHttpMediaType {
/**
* 声明 Content-type
* application 开头-常用
*/
MEDIA_TYPE_JSON(MediaType.parse("application/json; charset=utf-8")),
MEDIA_TYPE_XML(MediaType.parse("application/xml; charset=utf-8")),
MEDIA_TYPE_FORM_URLENCODED(MediaType.parse("application/x-www-form-urlencoded; charset=utf-8")),
/**
* 声明 Content-type
* text 开头-常用
*/
MEDIA_TYPE_MARKDOWN(MediaType.parse("text/x-markdown; charset=utf-8")),
MEDIA_TYPE_HTML(MediaType.parse("text/html; charset=utf-8")),
MEDIA_TYPE_PLAINTEXT(MediaType.parse("text/plain; charset=utf-8")),
/**
* 声明 Content-type
* multipart 开头
* 需要在表单中进行文件上传时,就需要使用该格式
*/
MEDIA_TYPE_MULTIPART(MediaType.parse("multipart/form-data; charset=utf-8"));
private final MediaType type;
}
工具类
package com.polo.testplatform.utils;
import com.alibaba.fastjson.JSON;
import com.polo.testplatform.enums.OkHttpMediaType;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import okhttp3.*;
import org.apache.commons.collections4.MapUtils;
import org.springframework.stereotype.Component;
import java.io.File;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
/**
* 关于 OkHttp 的工具类
*/
@Slf4j
@Component
public class OkHttpUtils {
private final OkHttpClient client = new OkHttpClient();
private Map<String, String> headersMap;
private Map<String, String> paramsMap;
private String url;
private Request.Builder request;
/**
* 设置 url
*
* @param url the url
* @return 工具类本身
*/
public OkHttpUtils url(String url) {
this.url = url;
return this;
}
/**
* 添加请求参数
*
* @param name 请求参数 key
* @param value 请求参数 value
* @return 工具类本身
*/
public OkHttpUtils addParams(String name, String value) {
if (MapUtils.isEmpty(paramsMap)) {
paramsMap = new HashMap<>(16);
}
paramsMap.put(name, value);
return this;
}
/**
* 重载,添加请求参数
*
* @param map 请求参数组成的 map
* @return 工具类本身
*/
public OkHttpUtils addParams(Map<String, String> map) {
if (MapUtils.isEmpty(paramsMap)) {
paramsMap = new HashMap<>(16);
}
paramsMap.putAll(map);
return this;
}
/**
* 添加请求头
*
* @param name 请求头 key
* @param value 请求头 value
* @return 工具类本身
*/
public OkHttpUtils addHeader(String name, String value) {
if (MapUtils.isEmpty(headersMap)) {
headersMap = new HashMap<>(16);
}
headersMap.put(name, value);
return this;
}
/**
* 重载,添加请求头
*
* @param map 请求头组成的 map
* @return 工具类本身
*/
public OkHttpUtils addHeader(Map<String, String> map) {
if (MapUtils.isEmpty(headersMap)) {
headersMap = new HashMap<>(16);
}
headersMap.putAll(map);
return this;
}
/**
* 处理 get 请求
*
* @return 工具类本身
*/
public OkHttpUtils get() {
request = new Request.Builder().get();
StringBuilder urlBuilder = new StringBuilder(url);
if (MapUtils.isNotEmpty(paramsMap)) {
for (Map.Entry<String, String> entry : paramsMap.entrySet()) {
urlBuilder.append(URLEncoder.encode(entry.getKey(), StandardCharsets.UTF_8))
.append("=")
.append(URLEncoder.encode(entry.getValue(), StandardCharsets.UTF_8))
.append("&");
}
urlBuilder.deleteCharAt(urlBuilder.length() - 1);
}
request.url(urlBuilder.toString());
return this;
}
/**
* 最终处理 post 请求
*
* @param requestBody the request body
* @return 工具类本身
*/
public OkHttpUtils post(RequestBody requestBody) {
request = new Request.Builder()
.post(requestBody)
.url(url);
return this;
}
/**
* 处理 post 请求
*
* @param data the data
* @param mediaType Content-type
* @return 工具类本身
*/
public OkHttpUtils post(String data, MediaType mediaType) {
return post(RequestBody.create(data, mediaType));
}
/**
* 处理上传文件的 post 请求
*
* @param file the file
* @param mediaType Content-type
* @return 工具类本身
*/
public OkHttpUtils post(File file, MediaType mediaType) {
return post(RequestBody.create(file, mediaType));
}
/**
* json post 请求
*
* @return 工具类本身
*/
public OkHttpUtils postJson() {
String json = JSON.toJSONString(paramsMap);
return post(json, OkHttpMediaType.MEDIA_TYPE_JSON.getType());
}
/**
* json post 请求
*
* @param map the map
* @return 工具类本身
*/
public OkHttpUtils postJson(Map<String, String> map) {
String json = JSON.toJSONString(map);
return post(json, OkHttpMediaType.MEDIA_TYPE_JSON.getType());
}
/**
* form 表单 post 请求
*
* @return 工具类本身
*/
public OkHttpUtils postForm() {
FormBody.Builder formBody = new FormBody.Builder();
paramsMap.forEach(formBody::add);
return post(formBody.build());
}
/**
* form 表单 post 请求
*
* @param map the map
* @return 工具类本身
*/
public OkHttpUtils postForm(Map<String, String> map) {
FormBody.Builder formBody = new FormBody.Builder();
map.forEach(formBody::add);
return post(formBody.build());
}
/**
* 同步请求
*
* @return the string
*/
@SneakyThrows
public String sync() {
if (MapUtils.isNotEmpty(headersMap)) {
for (Map.Entry<String, String> entry : headersMap.entrySet()) {
request.addHeader(entry.getKey(), entry.getValue());
}
}
String result = null;
try (Response response = client.newCall(request.build()).execute()) {
if (!response.isSuccessful()) {
log.error("Unexpected code " + response);
}
result = response.body().string();
log.info("response body " + result);
}
return result;
}
}
重写测试类
package com.polo.testplatform.utils;
import com.polo.testplatform.enums.OkHttpMediaType;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
@Slf4j
@SpringBootTest
class TestOkHttpUtils {
@Autowired
private OkHttpUtils okHttpUtils;
@Test
void testGet() {
String response = okHttpUtils.url("https://publicobject.com/helloworld.txt").get().sync();
log.info(response);
}
//
@SneakyThrows
@Test
void testHeaders() {
Map<String, String> headers = new HashMap<>();
headers.put("Accept", "application/vnd.github.v3+json");
String response = okHttpUtils.url("https://api.github.com/repos/square/okhttp/issues")
.addHeader("Accept", "application/json; q=0.5")
.addHeader(headers)
.get()
.sync();
log.info(response);
}
@SneakyThrows
@Test
void testPost() {
String postBody = """
Releases
--------
*_ 1.0_ May 6, 2013
* _1.1_ June 15, 2013
* 1.2_ August 11, 2013
""";
String response = okHttpUtils.url("https://api.github.com/markdown/raw")
.post(postBody, OkHttpMediaType.MEDIA_TYPE_MARKDOWN.getType())
.sync();
log.info(response);
}
@SneakyThrows
@Test
void testFile() {
File file = new File("README.md");
String response = okHttpUtils.url("https://api.github.com/markdown/raw")
.post(file, OkHttpMediaType.MEDIA_TYPE_MARKDOWN.getType())
.sync();
log.info(response);
}
//
@SneakyThrows
@Test
void testForm() {
String response = okHttpUtils.url("https://en.wikipedia.org/w/index.php")
.addParams("search", "Jurassic Park")
.postForm()
.sync();
log.info(response);
}
}
可以看到代码量省了不少
