自动化立体仓库 - WCS系统
zhangc
2025-03-11 d5449236ef0b3adafb3e4cc872f50479efa0ce7b
src/main/java/com/zy/common/utils/HttpHandler.java
@@ -27,7 +27,7 @@
    private Integer timeout;
    private TimeUnit timeUnit;
    public HttpHandler(Builder builder){
    public HttpHandler(Builder builder) {
        this.uri = builder.uri;
        this.path = builder.path;
        this.json = builder.json;
@@ -45,8 +45,8 @@
    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()){
        if (headers != null && headers.size() > 0) {
            for (Map.Entry<String, Object> entry : headers.entrySet()) {
                headerBuilder.addHeader(entry.getKey(), String.valueOf(entry.getValue()));
            }
        }
@@ -62,24 +62,24 @@
    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()){
        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)){
        if (json == null || "".equals(json)) {
            FormBody.Builder builder = new FormBody.Builder();
            for (Map.Entry<String, Object> entry : params.entrySet()){
            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)
                    .url((https ? "https://" : "http://") + uri + path)
                    .post(body)
                    .build();
        } else {
            RequestBody body = RequestBody.create(MEDIA_TYPE, json);
            Request.Builder builder = headerBuilder.url((https?"https://":"http://")+uri+path);
            Request.Builder builder = headerBuilder.url((https ? "https://" : "http://") + uri + path);
            builder.header("Content-Type", "application/json");
            request = builder.post(body).build();
@@ -98,7 +98,7 @@
        StringBuilder res = new StringBuilder();
        res.append(isHttps ? "https://" : "http://");
        res.append(uri);
        if (path.length() > 0 && !(path.charAt(0) == '/')){
        if (path.length() > 0 && !(path.charAt(0) == '/')) {
            res.append("/");
        }
        res.append(path);
@@ -114,8 +114,8 @@
                }
        );
        String url = res.toString();
        if ("&".equals(url.substring(url.length()-1, url.length()))){
            url = url.substring(0, url.length()-1);
        if ("&".equals(url.substring(url.length() - 1, url.length()))) {
            url = url.substring(0, url.length() - 1);
        }
        return url;
    }
@@ -124,7 +124,7 @@
     * 获取 okHttpClient
     * @return the HttpHandler instance
     */
    private OkHttpClient getClient(Integer timeout, TimeUnit timeUnit){
    private OkHttpClient getClient(Integer timeout, TimeUnit timeUnit) {
        return new OkHttpClient
                .Builder()
                .connectTimeout(timeout, timeUnit)
@@ -157,14 +157,14 @@
         * 建造器
         * @return the HttpHandler instance
         */
        public HttpHandler build(){
            if (null == this.uri || "".equals(this.uri)){
        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());
            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);
        }
@@ -175,7 +175,7 @@
        }
        public Builder setPath(String path) {
            if (!path.startsWith("/")){
            if (!path.startsWith("/")) {
                path = "/" + path;
            }
            this.path = path;