自动化立体仓库 - WMS系统
*
lsh
6 小时以前 38291612d417fefa096eecc7c795f5e0279ed11d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
package com.zy.common.utils;
 
import okhttp3.*;
import java.io.IOException;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
 
/**
 * Http协议客户端
 * @author luxiaotao
 * @date 2018-9-27
 */
public class HttpHandler {
 
    private static final Integer DEFAULT_TIMEOUT_SECONDS = 5;
    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 String json;
    private Map<String, Object> params;
    private Map<String, Object> headers;
    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.path = builder.path;
        this.json = builder.json;
        this.params = builder.params;
        this.headers = builder.headers;
        this.https = builder.https;
        this.timeout = builder.timeout;
        this.timeUnit = builder.timeUnit;
        this.useFormUrlEncoded = builder.useFormUrlEncoded; // 新增
    }
 
    /**
     * GET请求执行
     * @return the HttpHandler response
     */
    public String doGet() throws IOException {
        String url = paramsToUrl(uri, path, params, https);
        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()));
            }
        }
        Request request = headerBuilder.url(url).build();
        Response response = getClient(timeout, timeUnit).newCall(request).execute();
        return response.isSuccessful() ? response.body().string() : null;
    }
 
    /**
     * POST请求执行
     * @return the HttpHandler response
     */
    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()));
            }
        }
 
        // 根据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 formBody = formBuilder.build();
            request = headerBuilder
                    .url((https?"https://":"http://")+uri+path)
                    .post(formBody)
                    .build();
        } else {
            // 原有的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(",", "&");
    }
 
    /**
     * get请求参数拼接方法
     * @return 请求行
     */
    private String paramsToUrl(String uri, String path, Map<String, Object> params, boolean isHttps) {
        StringBuilder res = new StringBuilder();
        res.append(isHttps ? "https://" : "http://");
        res.append(uri);
        if (path.length() > 0 && !(path.charAt(0) == '/')){
            res.append("/");
        }
        res.append(path);
        Optional.ofNullable(params).ifPresent(
                args -> {
                    res.append("?");
                    args.forEach((key, value) -> {
                        res.append(key);
                        res.append("=");
                        res.append(value);
                        res.append("&");
                    });
                }
        );
        String url = res.toString();
        if ("&".equals(url.substring(url.length()-1, url.length()))){
            url = url.substring(0, url.length()-1);
        }
        return url;
    }
 
    /**
     * 获取 okHttpClient
     * @return the HttpHandler instance
     */
    private OkHttpClient getClient(Integer timeout, TimeUnit timeUnit){
        return new OkHttpClient
                .Builder()
                .connectTimeout(timeout, timeUnit)
                .readTimeout(timeout, timeUnit)
                .build();
    }
 
    /**
     * Http协议报文建造者
     */
    public static class Builder {
 
        private String uri;
        private String path;
        private String json;
        private Map<String, Object> params;
        private Map<String, Object> headers;
        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 the HttpHandler instance
         */
        public HttpHandler build(){
            if (null == this.uri || "".equals(this.uri)){
                throw new RuntimeException("uri is null");
            }
            if (this.uri.startsWith("http://")){
                this.uri = this.uri.substring(6,uri.length());
            } else if (this.uri.startsWith("https://")){
                this.uri = this.uri.substring(7,uri.length());
            }
            return new HttpHandler(this);
        }
 
        public Builder setUri(String uri) {
            this.uri = uri;
            return this;
        }
 
        public Builder setPath(String path) {
            if (!path.startsWith("/")){
                path = "/" + path;
            }
            this.path = path;
            return this;
        }
 
        public Builder setTimeout(Integer timeout, TimeUnit timeUnit) {
            this.timeout = timeout;
            this.timeUnit = timeUnit;
            return this;
        }
 
        public Builder setParams(Map<String, Object> params) {
            this.params = params;
            return this;
        }
 
        public Builder setHeaders(Map<String, Object> headers) {
            this.headers = headers;
            return this;
        }
 
        public Builder setHttps(boolean https) {
            this.https = https;
            return this;
        }
 
        public Builder setJson(String json) {
            this.json = json;
            return this;
        }
 
        // 新增方法:设置使用x-www-form-urlencoded格式
        public Builder setUseFormUrlEncoded(boolean useFormUrlEncoded) {
            this.useFormUrlEncoded = useFormUrlEncoded;
            return this;
        }
    }
}