@Slf4j
public class OkHttpUtils {
public static final MediaType JSON = MediaType.parse("application/json;charset=utf-8");
public static final MediaType TEXT = MediaType.parse("text/plain; charset=utf-8");
public static final MediaType X_WWW_FORM = MediaType.parse("application/x-www-form-urlencoded");
public static final MediaType MIXED = MediaType.parse("multipart/mixed");
public static final MediaType ALTERNATIVE = MediaType.parse("multipart/alternative");
public static final MediaType DIGEST = MediaType.parse("multipart/digest");
public static final MediaType PARALLEL = MediaType.parse("multipart/parallel");
public static final MediaType FORM = MediaType.parse("multipart/form-data");
private static OkHttpClient client = null;
private static volatile OkHttpUtils okHttpRequest = null;
private OkHttpUtils() {
}
public static OkHttpUtils getInstance() {
if (okHttpRequest == null) {
synchronized (OkHttpUtils.class) {
if (okHttpRequest == null) {
client = new OkHttpClient.Builder()
.connectTimeout(50, TimeUnit.SECONDS)
.readTimeout(100, TimeUnit.SECONDS)
.writeTimeout(50, TimeUnit.SECONDS)
.build();
okHttpRequest = new OkHttpUtils();
}
}
}
return okHttpRequest;
}
public String getHeadParam(String url, Map<String, String> headerMap) throws IOException {
Builder builder = new Builder();
headerParam(builder, headerMap);
Request request = builder.url(url).build();
Response response = client.newCall(request).execute();
return response.body().string();
}
public String getHeadParamObject(String url, Map<String, Object> headerMap) throws IOException {
Builder builder = new Builder();
headerParamObject(builder, headerMap);
Request request = builder.url(url).build();
Response response = client.newCall(request).execute();
return response.body().string();
}
public String getHeadForm(String url, Map<String, String> headerMap, Map<String, String> fromParam) throws IOException {
Builder builder = new Builder();
headerParam(builder, headerMap);
FormBody.Builder fromBodyBuilder = new FormBody.Builder();
if (fromParam != null && !fromParam.isEmpty()) {
fromParam.forEach((k, v) -> {
fromBodyBuilder.add(k, v);
});
}
Request request = builder.url(url).build();
Response response = client.newCall(request).execute();
return response.body().string();
}
public String post(String url, String json) throws IOException {
RequestBody body = RequestBody.create(JSON, json);
Request request = new Builder().url(url).post(body).build();
Response response = client.newCall(request).execute();
return response.body().string();
}
public String postHeaderParamObject(String url, String bodyJson, Map<String, Object> headerMap) throws IOException {
RequestBody body = RequestBody.create(JSON, bodyJson);
Builder builder = new Builder();
headerParamObject(builder, headerMap);
Request request = builder.url(url).post(body).build();
Response response = client.newCall(request).execute();
return response.body().string();
}
public String postHeaderTextParamObject(String url, String text, Map<String, Object> headerMap) throws IOException {
RequestBody body = RequestBody.create(TEXT, text);
Builder builder = new Builder();
headerParamObject(builder, headerMap);
Request request = builder.url(url).post(body).build();
Response response = client.newCall(request).execute();
return response.body().string();
}
public String postHeaderParam(String url, String bodyJson, Map<String, String> headerMap) throws IOException {
RequestBody body = RequestBody.create(JSON, bodyJson);
Builder builder = new Builder();
headerParam(builder, headerMap);
Request request = builder.url(url).post(body).build();
Response response = client.newCall(request).execute();
log.info("response:{}", response);
return response.body().string();
}
public String postForm(String url, Map<String, String> fromParam) throws IOException {
FormBody.Builder fromBodyBuilder = new FormBody.Builder();
if (fromParam != null && !fromParam.isEmpty()) {
fromParam.forEach((k, v) -> {
fromBodyBuilder.add(k, v);
});
}
RequestBody body = fromBodyBuilder.build();
Request request = new Builder().url(url).post(body).build();
Response response = client.newCall(request).execute();
return response.body().string();
}
public String postHeaderForm(String url, Map<String, String> headerMap, Map<String, String> fromParam) throws IOException {
FormBody.Builder fromBodyBuilder = new FormBody.Builder();
formParam(fromBodyBuilder, fromParam);
RequestBody body = fromBodyBuilder.build();
Builder builder = new Builder();
headerParam(builder, headerMap);
Request request = builder.url(url).post(body).build();
Response response = client.newCall(request).execute();
return response.body().string();
}
public String postHeaderObjectForm(String url, Map<String, Object> headerMap, Map<String, String> fromParam) throws IOException {
FormBody.Builder fromBodyBuilder = new FormBody.Builder();
formParam(fromBodyBuilder, fromParam);
RequestBody body = fromBodyBuilder.build();
Builder builder = new Builder();
headerParamObject(builder, headerMap);
Request request = builder.url(url).post(body).build();
Response response = client.newCall(request).execute();
return response.body().string();
}
private void headerParam(Builder builder, Map<String, String> headerMap) {
if (headerMap != null && !headerMap.isEmpty()) {
headerMap.forEach(builder::addHeader);
}
}
private void headerParamObject(Builder builder, Map<String, Object> headerMap) {
if (headerMap != null && !headerMap.isEmpty()) {
headerMap.forEach((k, v) -> {
builder.addHeader(k, v.toString());
});
}
}
private void formParam(FormBody.Builder fromBodyBuilder, Map<String, String> fromParam) {
if (fromParam != null && !fromParam.isEmpty()) {
fromParam.forEach((k, v) -> {
if (StrUtil.isNotEmpty(v)) {
fromBodyBuilder.add(k, v);
}
});
}
}
public String deleteHeaderParamObject(String url, String bodyJson, Map<String, Object> headerMap) throws IOException {
RequestBody body = RequestBody.create(JSON, bodyJson);
Builder builder = new Builder();
headerParamObject(builder, headerMap);
Request request = builder.url(url).delete(body).build();
Response response = client.newCall(request).execute();
return response.body().string();
}
public String deleteHeaderParam(String url, Map<String, Object> headerMap) throws IOException {
Builder builder = new Builder();
headerParamObject(builder, headerMap);
Request request = builder.url(url).delete().build();
Response response = client.newCall(request).execute();
return response.body().string();
}
public static void main(String[] args) {
try {
OkHttpUtils instance = OkHttpUtils.getInstance();
Map<String, String> headerMap = new HashMap<>(16);
Map<String, Object> bodyMap = new HashMap<>(16);
String response = instance.postHeaderParam("http://localhost:8080", JSONObject.toJSONString(bodyMap), headerMap);
} catch (IOException e) {
e.printStackTrace();
}
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理