| | |
| | | package com.zy.common.utils; |
| | | |
| | | |
| | | import okhttp3.*; |
| | | |
| | | import java.io.IOException; |
| | | import java.util.Map; |
| | | import java.util.Optional; |
| | |
| | | public class HttpHandler { |
| | | |
| | | private static final Integer DEFAULT_TIMEOUT_SECONDS = 5; |
| | | private static final MediaType MEDIA_TYPE = MediaType.parse("application/json;charset=utf-8"); |
| | | private static final MediaType MEDIA_TYPE_JSON = MediaType.parse("application/json;charset=utf-8"); |
| | | private static final MediaType MEDIA_TYPE_FORM = MediaType.parse("application/x-www-form-urlencoded;charset=utf-8"); |
| | | |
| | | private String uri; |
| | | private String path; |
| | |
| | | private boolean https; |
| | | private Integer timeout; |
| | | private TimeUnit timeUnit; |
| | | private boolean useFormUrlEncoded; // 新增:标识是否使用x-www-form-urlencoded格式 |
| | | |
| | | public HttpHandler(Builder builder){ |
| | | this.uri = builder.uri; |
| | |
| | | this.https = builder.https; |
| | | this.timeout = builder.timeout; |
| | | this.timeUnit = builder.timeUnit; |
| | | this.useFormUrlEncoded = builder.useFormUrlEncoded; // 新增 |
| | | } |
| | | |
| | | /** |
| | |
| | | public String doPost() throws IOException { |
| | | Request request; |
| | | Request.Builder headerBuilder = new Request.Builder(); |
| | | |
| | | // 设置请求头 |
| | | if (headers != null && headers.size()>0){ |
| | | for (Map.Entry<String, Object> entry : headers.entrySet()){ |
| | | headerBuilder.addHeader(entry.getKey(), String.valueOf(entry.getValue())); |
| | | } |
| | | } |
| | | if (json == null || "".equals(json)){ |
| | | FormBody.Builder builder = new FormBody.Builder(); |
| | | for (Map.Entry<String, Object> entry : params.entrySet()){ |
| | | builder.add(entry.getKey(), String.valueOf(entry.getValue())); |
| | | |
| | | // 根据useFormUrlEncoded标志选择请求体格式 |
| | | if (useFormUrlEncoded) { |
| | | // 使用x-www-form-urlencoded格式[1,2,6](@ref) |
| | | FormBody.Builder formBuilder = new FormBody.Builder(); |
| | | |
| | | if (params != null && !params.isEmpty()) { |
| | | // 添加表单参数[2,6,7](@ref) |
| | | for (Map.Entry<String, Object> entry : params.entrySet()) { |
| | | formBuilder.add(entry.getKey(), String.valueOf(entry.getValue())); |
| | | } |
| | | } else if (json != null && !json.isEmpty()) { |
| | | // 如果提供了json字符串,尝试解析为键值对(简单实现) |
| | | // 注意:这里需要根据实际情况调整json解析逻辑 |
| | | String formattedParams = parseJsonToFormData(json); |
| | | if (formattedParams != null) { |
| | | String[] pairs = formattedParams.split("&"); |
| | | for (String pair : pairs) { |
| | | String[] keyValue = pair.split("=", 2); |
| | | if (keyValue.length == 2) { |
| | | formBuilder.add(keyValue[0], keyValue[1]); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | FormBody body = builder.build(); |
| | | |
| | | FormBody formBody = formBuilder.build(); |
| | | request = headerBuilder |
| | | .url((https?"https://":"http://")+uri+path) |
| | | .post(body) |
| | | .post(formBody) |
| | | .build(); |
| | | } else { |
| | | RequestBody body = RequestBody.create(MEDIA_TYPE, json); |
| | | Request.Builder builder = headerBuilder.url((https?"https://":"http://")+uri+path); |
| | | builder.header("Content-Type", "application/json;charset=UTF-8"); |
| | | request = builder.post(body).build(); |
| | | |
| | | // 原有的JSON格式处理 |
| | | if (json == null || "".equals(json)){ |
| | | FormBody.Builder builder = new FormBody.Builder(); |
| | | for (Map.Entry<String, Object> entry : params.entrySet()){ |
| | | builder.add(entry.getKey(), String.valueOf(entry.getValue())); |
| | | } |
| | | FormBody body = builder.build(); |
| | | request = headerBuilder |
| | | .url((https?"https://":"http://")+uri+path) |
| | | .post(body) |
| | | .build(); |
| | | } else { |
| | | RequestBody body = RequestBody.create(MEDIA_TYPE_JSON, json); |
| | | Request.Builder builder = headerBuilder.url((https?"https://":"http://")+uri+path); |
| | | builder.header("Content-Type", "application/json;charset=UTF-8"); |
| | | request = builder.post(body).build(); |
| | | } |
| | | } |
| | | |
| | | Call call = getClient(timeout, timeUnit).newCall(request); |
| | | Response response = call.execute(); |
| | | return response.body().string(); |
| | | } |
| | | |
| | | /** |
| | | * 将JSON字符串转换为表单数据格式(简易实现) |
| | | * @param json JSON字符串 |
| | | * @return 表单数据字符串 |
| | | */ |
| | | private String parseJsonToFormData(String json) { |
| | | // 简易实现:移除JSON的大括号和引号,替换冒号为等号,逗号为& |
| | | // 注意:对于复杂JSON需要更完善的解析逻辑 |
| | | return json.replaceAll("[{}\"]", "") |
| | | .replaceAll(":", "=") |
| | | .replaceAll(",", "&"); |
| | | } |
| | | |
| | | /** |
| | |
| | | private boolean https; |
| | | private Integer timeout; |
| | | private TimeUnit timeUnit; |
| | | private boolean useFormUrlEncoded; // 新增:标识是否使用x-www-form-urlencoded格式 |
| | | |
| | | { |
| | | // 默认5s超时 |
| | | timeout = DEFAULT_TIMEOUT_SECONDS; |
| | | timeUnit = TimeUnit.SECONDS; |
| | | path = ""; |
| | | useFormUrlEncoded = false; // 默认使用JSON格式 |
| | | } |
| | | |
| | | /** |
| | |
| | | return this; |
| | | } |
| | | |
| | | // 新增方法:设置使用x-www-form-urlencoded格式 |
| | | public Builder setUseFormUrlEncoded(boolean useFormUrlEncoded) { |
| | | this.useFormUrlEncoded = useFormUrlEncoded; |
| | | return this; |
| | | } |
| | | } |
| | | |
| | | } |
| | | } |