| | |
| | | import java.io.IOException; |
| | | import java.security.SecureRandom; |
| | | import java.security.cert.X509Certificate; |
| | | import java.util.Map; |
| | | import java.util.concurrent.TimeUnit; |
| | | |
| | | public class OkHttpExample { |
| | | // 定义 JSON 媒体类型 |
| | | private static final MediaType JSON = MediaType.get("application/json; charset=utf-8"); |
| | | // 创建 OkHttpClient 实例(全局单例即可,避免重复创建) |
| | | private static final OkHttpClient client = new OkHttpClient.Builder() |
| | | .connectTimeout(5, TimeUnit.SECONDS) |
| | | .readTimeout(5, TimeUnit.SECONDS) |
| | | .build(); |
| | | |
| | | public static OkHttpClient createUnsafeOkHttpClient() { |
| | | |
| | |
| | | } |
| | | } |
| | | |
| | | public static String sendGetRequestWithBearerToken(String url, String token) throws IOException { |
| | | // 构建请求:添加 Authorization 请求头(格式:Bearer + 空格 + token) |
| | | Request request = new Request.Builder() |
| | | .url(url) |
| | | .get() // GET 请求(可省略,默认就是 GET) |
| | | .addHeader("Authorization", "Bearer " + token) // 核心:Bearer Token 头 |
| | | .addHeader("Content-Type", "application/json") // 按需添加其他头 |
| | | .build(); |
| | | |
| | | // 执行请求并返回响应 |
| | | try (Response response = client.newCall(request).execute()) { |
| | | if (!response.isSuccessful()) { |
| | | throw new IOException("请求失败,响应码:" + response.code() + ",消息:" + response.message()); |
| | | } |
| | | // 读取响应体(string() 会自动关闭流,无需手动关) |
| | | return response.body().string(); |
| | | } |
| | | } |
| | | |
| | | // GET 请求方法 |
| | | public static String get(String url) throws IOException { |
| | | Request request = new Request.Builder() |
| | | .url(url) |
| | | .get() // 默认为 GET,可省略 |
| | | .build(); |
| | | |
| | | // 执行请求并获取响应 |
| | | try (Response response = client.newCall(request).execute()) { |
| | | if (!response.isSuccessful()) { |
| | | throw new IOException("GET 请求失败:" + response); |
| | | } |
| | | return response.body().string(); // 读取响应体 |
| | | } |
| | | } |
| | | |
| | | // POST 请求方法(携带 JSON 参数) |
| | | 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) |
| | | .build(); |
| | | |
| | | try (Response response = client.newCall(request).execute()) { |
| | | if (!response.isSuccessful()) { |
| | | throw new IOException("POST 请求失败:" + response); |
| | | } |
| | | return response.body().string(); |
| | | } |
| | | } |
| | | |
| | | // POST 请求方法(form-urlencoded + 忽略SSL证书,适用于OAuth2 token端点) |
| | | public static String postFormUnsafe(String url, Map<String, String> formParams) throws IOException { |
| | | OkHttpClient unsafeClient = createUnsafeOkHttpClient(); |
| | | |
| | | FormBody.Builder formBuilder = new FormBody.Builder(); |
| | | for (Map.Entry<String, String> entry : formParams.entrySet()) { |
| | | formBuilder.add(entry.getKey(), entry.getValue()); |
| | | } |
| | | |
| | | Request request = new Request.Builder() |
| | | .url(url) |
| | | .post(formBuilder.build()) |
| | | .build(); |
| | | |
| | | try (Response response = unsafeClient.newCall(request).execute()) { |
| | | if (!response.isSuccessful()) { |
| | | throw new IOException("POST form 请求失败:" + response.code() + " " + response.message()); |
| | | } |
| | | return response.body().string(); |
| | | } |
| | | } |
| | | |
| | | // GET 请求方法(参数拼接到URL + 忽略SSL证书,适用于OAuth2 token端点) |
| | | public static String getUnsafe(String url, String token) throws IOException { |
| | | OkHttpClient unsafeClient = createUnsafeOkHttpClient(); |
| | | |
| | | Request request = new Request.Builder() |
| | | .url(url) |
| | | .addHeader("Authorization", "Bearer " + token) // 核心:Bearer Token 头 |
| | | .get() |
| | | .build(); |
| | | |
| | | try (Response response = unsafeClient.newCall(request).execute()) { |
| | | if (!response.isSuccessful()) { |
| | | throw new IOException("GET form 请求失败:" + response.code() + " " + response.message()); |
| | | } |
| | | return response.body().string(); |
| | | } |
| | | } |
| | | |
| | | } |