|
|
|
@ -0,0 +1,181 @@
|
|
|
|
|
package com.dsideal.resource.Util;
|
|
|
|
|
|
|
|
|
|
import com.dsideal.resource.Config.GatewayConfig;
|
|
|
|
|
import okhttp3.*;
|
|
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
|
|
|
|
public class HttpClient {
|
|
|
|
|
private static final OkHttpClient OK_HTTP_CLIENT = new OkHttpClient.Builder()
|
|
|
|
|
.connectTimeout(GatewayConfig.CONNECT_TIMEOUT, TimeUnit.MILLISECONDS)
|
|
|
|
|
.readTimeout(GatewayConfig.READ_TIMEOUT, TimeUnit.MILLISECONDS)
|
|
|
|
|
.writeTimeout(GatewayConfig.WRITE_TIMEOUT, TimeUnit.MILLISECONDS)
|
|
|
|
|
.connectionPool(new ConnectionPool(GatewayConfig.MAX_CONNECTIONS,
|
|
|
|
|
GatewayConfig.KEEP_ALIVE_DURATION,
|
|
|
|
|
TimeUnit.SECONDS))
|
|
|
|
|
.build();
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 发送GET请求
|
|
|
|
|
*
|
|
|
|
|
* @param url 请求URL
|
|
|
|
|
* @param headers 请求头
|
|
|
|
|
* @return 响应字符串
|
|
|
|
|
*/
|
|
|
|
|
public static String get(String url, Map<String, String> headers) {
|
|
|
|
|
Request.Builder builder = new Request.Builder().url(url);
|
|
|
|
|
|
|
|
|
|
// 添加请求头
|
|
|
|
|
if (headers != null) {
|
|
|
|
|
headers.forEach(builder::addHeader);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try (Response response = OK_HTTP_CLIENT.newCall(builder.build()).execute()) {
|
|
|
|
|
if (!response.isSuccessful()) {
|
|
|
|
|
throw new IOException("请求失败: " + response);
|
|
|
|
|
}
|
|
|
|
|
ResponseBody body = response.body();
|
|
|
|
|
return body != null ? body.string() : null;
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
throw new RuntimeException("GET请求异常: " + url, e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 发送POST请求(JSON格式)
|
|
|
|
|
*
|
|
|
|
|
* @param url 请求URL
|
|
|
|
|
* @param jsonBody JSON请求体
|
|
|
|
|
* @param headers 请求头
|
|
|
|
|
* @return 响应字符串
|
|
|
|
|
*/
|
|
|
|
|
public static String postJson(String url, String jsonBody, Map<String, String> headers) {
|
|
|
|
|
MediaType JSON = MediaType.parse("application/json; charset=utf-8");
|
|
|
|
|
RequestBody body = RequestBody.create(jsonBody, JSON);
|
|
|
|
|
|
|
|
|
|
Request.Builder builder = new Request.Builder()
|
|
|
|
|
.url(url)
|
|
|
|
|
.post(body);
|
|
|
|
|
|
|
|
|
|
// 添加请求头
|
|
|
|
|
if (headers != null) {
|
|
|
|
|
headers.forEach(builder::addHeader);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try (Response response = OK_HTTP_CLIENT.newCall(builder.build()).execute()) {
|
|
|
|
|
if (!response.isSuccessful()) {
|
|
|
|
|
throw new IOException("请求失败: " + response);
|
|
|
|
|
}
|
|
|
|
|
ResponseBody responseBody = response.body();
|
|
|
|
|
return responseBody != null ? responseBody.string() : null;
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
throw new RuntimeException("POST请求异常: " + url, e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 发送POST请求(表单格式)
|
|
|
|
|
*
|
|
|
|
|
* @param url 请求URL
|
|
|
|
|
* @param formData 表单数据
|
|
|
|
|
* @param headers 请求头
|
|
|
|
|
* @return 响应字符串
|
|
|
|
|
*/
|
|
|
|
|
public static String postForm(String url, Map<String, String> formData, Map<String, String> headers) {
|
|
|
|
|
FormBody.Builder formBuilder = new FormBody.Builder();
|
|
|
|
|
|
|
|
|
|
// 添加表单数据
|
|
|
|
|
if (formData != null) {
|
|
|
|
|
formData.forEach(formBuilder::add);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Request.Builder builder = new Request.Builder()
|
|
|
|
|
.url(url)
|
|
|
|
|
.post(formBuilder.build());
|
|
|
|
|
|
|
|
|
|
// 添加请求头
|
|
|
|
|
if (headers != null) {
|
|
|
|
|
headers.forEach(builder::addHeader);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try (Response response = OK_HTTP_CLIENT.newCall(builder.build()).execute()) {
|
|
|
|
|
if (!response.isSuccessful()) {
|
|
|
|
|
throw new IOException("请求失败: " + response);
|
|
|
|
|
}
|
|
|
|
|
ResponseBody responseBody = response.body();
|
|
|
|
|
return responseBody != null ? responseBody.string() : null;
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
throw new RuntimeException("POST请求异常: " + url, e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 发送POST请求(文件上传)
|
|
|
|
|
*
|
|
|
|
|
* @param url 请求URL
|
|
|
|
|
* @param file 文件
|
|
|
|
|
* @param fileName 文件名
|
|
|
|
|
* @param headers 请求头
|
|
|
|
|
* @return 响应字符串
|
|
|
|
|
*/
|
|
|
|
|
public static String postFile(String url, File file, String fileName, Map<String, String> headers) {
|
|
|
|
|
RequestBody requestBody = new MultipartBody.Builder()
|
|
|
|
|
.setType(MultipartBody.FORM)
|
|
|
|
|
.addFormDataPart("file", fileName,
|
|
|
|
|
RequestBody.create(file, MediaType.parse("application/octet-stream")))
|
|
|
|
|
.build();
|
|
|
|
|
|
|
|
|
|
Request.Builder builder = new Request.Builder()
|
|
|
|
|
.url(url)
|
|
|
|
|
.post(requestBody);
|
|
|
|
|
|
|
|
|
|
// 添加请求头
|
|
|
|
|
if (headers != null) {
|
|
|
|
|
headers.forEach(builder::addHeader);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try (Response response = OK_HTTP_CLIENT.newCall(builder.build()).execute()) {
|
|
|
|
|
if (!response.isSuccessful()) {
|
|
|
|
|
throw new IOException("请求失败: " + response);
|
|
|
|
|
}
|
|
|
|
|
ResponseBody responseBody = response.body();
|
|
|
|
|
return responseBody != null ? responseBody.string() : null;
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
throw new RuntimeException("文件上传异常: " + url, e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
|
// 设置请求头
|
|
|
|
|
Map<String, String> headers = new HashMap<>();
|
|
|
|
|
headers.put("Authorization", "Bearer token123");
|
|
|
|
|
headers.put("Content-Type", "application/json");
|
|
|
|
|
|
|
|
|
|
// GET请求示例
|
|
|
|
|
String getUrl = "https://api.example.com/users";
|
|
|
|
|
String getResponse = HttpClient.get(getUrl, headers);
|
|
|
|
|
System.out.println("GET Response: " + getResponse);
|
|
|
|
|
|
|
|
|
|
// POST JSON请求示例
|
|
|
|
|
String postUrl = "https://api.example.com/users";
|
|
|
|
|
String jsonBody = "{\"name\":\"John\",\"age\":30}";
|
|
|
|
|
String postResponse = HttpClient.postJson(postUrl, jsonBody, headers);
|
|
|
|
|
System.out.println("POST Response: " + postResponse);
|
|
|
|
|
|
|
|
|
|
// POST表单请求示例
|
|
|
|
|
Map<String, String> formData = new HashMap<>();
|
|
|
|
|
formData.put("username", "john");
|
|
|
|
|
formData.put("password", "123456");
|
|
|
|
|
String formResponse = HttpClient.postForm(postUrl, formData, headers);
|
|
|
|
|
System.out.println("Form Response: " + formResponse);
|
|
|
|
|
|
|
|
|
|
// 文件上传示例
|
|
|
|
|
File file = new File("test.txt");
|
|
|
|
|
String fileResponse = HttpClient.postFile(postUrl, file, "test.txt", headers);
|
|
|
|
|
System.out.println("File Upload Response: " + fileResponse);
|
|
|
|
|
}
|
|
|
|
|
}
|