自动化立体仓库 - WMS系统
13
zhang
2 天以前 82de5a307466894bbb0258f8a63a26a7bb96d80d
src/main/java/com/zy/common/utils/HttpHandler.java
@@ -10,6 +10,7 @@
/**
 * Http协议客户端
 *
 * @author luxiaotao
 * @date 2018-9-27
 */
@@ -27,7 +28,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;
@@ -40,13 +41,14 @@
    /**
     * 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()){
        if (headers != null && headers.size() > 0) {
            for (Map.Entry<String, Object> entry : headers.entrySet()) {
                headerBuilder.addHeader(entry.getKey(), String.valueOf(entry.getValue()));
            }
        }
@@ -57,29 +59,30 @@
    /**
     * 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()){
        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;charset=UTF-8");
            request = builder.post(body).build();
@@ -92,13 +95,14 @@
    /**
     * 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) == '/')){
        if (path.length() > 0 && !(path.charAt(0) == '/')) {
            res.append("/");
        }
        res.append(path);
@@ -114,17 +118,18 @@
                }
        );
        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;
    }
    /**
     * 获取 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)
@@ -155,16 +160,17 @@
        /**
         * 建造器
         *
         * @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 +181,7 @@
        }
        public Builder setPath(String path) {
            if (!path.startsWith("/")){
            if (!path.startsWith("/")) {
                path = "/" + path;
            }
            this.path = path;