com.alibaba.arthas.nat.agent.common.utils.OkHttpUtil Maven / Gradle / Ivy
The newest version!
package com.alibaba.arthas.nat.agent.common.utils;
import okhttp3.*;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
/**
* @description: OkHttpUtil
* @author:flzjkl
* @date: 2024-10-20 21:35
*/
public class OkHttpUtil {
private static final OkHttpClient client = new OkHttpClient.Builder()
.connectTimeout(10, TimeUnit.SECONDS)
.writeTimeout(10, TimeUnit.SECONDS)
.readTimeout(30, TimeUnit.SECONDS)
.build();
private static final MediaType JSON = MediaType.get("application/json; charset=utf-8");
public static String get(String url) throws IOException {
Request request = new Request.Builder()
.url(url)
.build();
try (Response response = client.newCall(request).execute()) {
return response.body().string();
}
}
public static String post(String url, String json) throws IOException {
RequestBody body = RequestBody.create(json, JSON);
Request request = new Request.Builder()
.url(url)
.post(body)
.header("Content-Type", "application/json")
.build();
try (Response response = client.newCall(request).execute()) {
return response.body().string();
}
}
}