All Downloads are FREE. Search and download functionalities are using the official Maven repository.

top.jfunc.common.http.base.AbstractConfigurableHttp Maven / Gradle / Ivy

Go to download

http客户端请求工具类,有多种实现:OkHttp3、ApacheHttpClient、HttpURLConnection、Jodd-Http,可以随意切换http实现

There is a newer version: 1.2.12
Show newest version
package top.jfunc.common.http.base;

import top.jfunc.common.utils.ArrayListMultimap;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.X509TrustManager;
import java.util.Objects;

/**
 * 一些http的公共方法处理
 * @author xiongshiyan at 2018/8/7 , contact me with email [email protected] or phone 15208384257
 */
public abstract class AbstractConfigurableHttp {
    private Config config = Config.defaultConfig();

    public Config getConfig() {
        return config;
    }

    public AbstractConfigurableHttp setConfig(Config config) {
        this.config = Objects.requireNonNull(config);
        return this;
    }


    /////////////////////////////////////以下方法都由config代理,只是为了调用方便//////////////////////////////

    protected String addBaseUrlIfNecessary(String inputUrl){
        return getConfig().addBaseUrlIfNecessary(inputUrl);
    }

    public Integer getConnectionTimeoutWithDefault(Integer connectionTimeout){
        return getConfig().getConnectionTimeoutWithDefault(connectionTimeout);
    }

    public Integer getReadTimeoutWithDefault(Integer readTimeout){
        return getConfig().getReadTimeoutWithDefault(readTimeout);
    }

    public String getBodyCharsetWithDefault(String bodyCharset){
        return getConfig().getBodyCharsetWithDefault(bodyCharset);
    }
    public String getDefaultBodyCharset() {
        return getConfig().getDefaultBodyCharset();
    }
    public String getResultCharsetWithDefault(String resultCharset){
        return getConfig().getResultCharsetWithDefault(resultCharset);
    }
    public String getDefaultResultCharset() {
        return getConfig().getDefaultResultCharset();
    }
    public HostnameVerifier getHostnameVerifier() {
        return getConfig().getHostnameVerifier();
    }

    public SSLContext getSSLContext() {
        return getConfig().getSSLContext();
    }

    public SSLSocketFactory getSSLSocketFactory() {
        return getConfig().getSSLSocketFactory();
    }

    public X509TrustManager getX509TrustManager() {
        return getConfig().getX509TrustManager();
    }

    public ArrayListMultimap getDefaultHeaders(){
        return getConfig().getDefaultHeaders();
    }

    /**
     * 合并默认的header
     */
    protected ArrayListMultimap mergeDefaultHeaders(final ArrayListMultimap headers){
        ArrayListMultimap defaultHeaders = getDefaultHeaders();
        if(null == headers){
            return defaultHeaders;
        }
        if(null == defaultHeaders){
            return headers;
        }

        //合并两个
        for (String key : defaultHeaders.keySet()) {
            defaultHeaders.get(key).forEach((v)-> headers.put(key , v));
        }

        return headers;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy