记录一个Request父类

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.URL;
import java.net.URLConnection;

/**
 * Requestor tools class
 * 
 * @author HuangSL
 * @since 2018/05/30
 * @version 1.0.0
 * 
 */
public abstract class Requestor {

    private String contentType = "application/x-www-form-urlencoded";
    private String charset = "utf-8";
    private Integer connectTimeout = null;
    private Integer socketTimeout = null;
    private String proxyHost = null;
    private Integer proxyPort = null;
    private String SOAPAction = null;
    private Boolean doInput = true;
    private Boolean doOutpt = false;

    public String getContentType() {
        return contentType;
    }

    public void setContentType(String contentType) {
        this.contentType = contentType;
    }

    public String getCharset() {
        return charset;
    }

    public void setCharset(String charset) {
        this.charset = charset;
    }

    public void setConnectTimeout(Integer connectTimeout) {
        this.connectTimeout = connectTimeout;
    }

    public Integer getConnectTimeout() {
        return connectTimeout;
    }

    public void setSocketTimeout(Integer socketTimeout) {
        this.socketTimeout = socketTimeout;
    }

    public Integer getSocketTimeout() {
        return socketTimeout;
    }

    public String getProxyHost() {
        return proxyHost;
    }

    public void setProxyHost(String proxyHost) {
        this.proxyHost = proxyHost;
    }

    public void setProxyPort(Integer proxyPort) {
        this.proxyPort = proxyPort;
    }

    public Integer getProxyPort() {
        return proxyPort;
    }

    public String getSOAPAction() {
        return SOAPAction;
    }

    public void setSOAPAction(String sOAPAction) {
        SOAPAction = sOAPAction;
        this.setContentType("text/xml; charset=utf-8");
    }

    public Boolean getDoInput() {
        return doInput;
    }

    public void setDoInput(Boolean doInput) {
        this.doInput = doInput;
    }

    public Boolean getDoOutpt() {
        return doOutpt;
    }

    public void setDoOutpt(Boolean doOutpt) {
        this.doOutpt = doOutpt;
    }

    /**
     * Open a connection
     * 
     * @param url
     *            String url path
     * 
     * @return URLConnection
     * @throws IOException
     * 
     * @author HuangSL
     * @since 2018/05/30
     */
    public URLConnection openConnection(String url) throws IOException {
        return this.openConnection(new URL(url));
    }

    /**
     * Open a connection
     * 
     * @param url
     *            URL
     * @return URLConnection
     * @throws IOException
     * 
     * @author HuangSL
     * @since 2018/05/30
     */
    public URLConnection openConnection(URL url) throws IOException {
        URLConnection connection;
        if (this.proxyHost != null && this.proxyPort != null) {
            Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(this.proxyHost, this.proxyPort));
            connection = url.openConnection(proxy);
        } else {
            connection = url.openConnection();
        }

        if (this.connectTimeout != null) {
            connection.setConnectTimeout(this.connectTimeout);
        }

        if (this.socketTimeout != null) {
            connection.setReadTimeout(this.socketTimeout);
        }

        if (this.SOAPAction != null) {
            connection.setRequestProperty("SOAPAction", SOAPAction);
        }

        if (!this.doInput) {
            connection.setDoInput(this.doInput);
        }

        if (this.doOutpt) {
            connection.setDoOutput(this.doOutpt);
        }

        connection.setRequestProperty("Accept-Charset", this.charset);
        connection.setRequestProperty("Content-Type", this.contentType);

        return connection;
    }
}

猜你喜欢

转载自blog.csdn.net/hsl0530hsl/article/details/80525228