⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 httpclient.java

📁 这是linux下ssl vpn的实现程序
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package com.maverick.http;

import java.io.*;
import java.net.*;
import java.util.*;

import java.util.Enumeration;
import org.apache.commons.logging.*;

public class HttpClient {

    public static final int PROXY_NONE = 0;
    public static final int PROXY_HTTP = 1;
    public static final int PROXY_HTTPS = 2;
    
    public static String USER_AGENT = "Maverick-HttpClient/1.0";

    /**
     * The target server
     */
    String hostname;
    int port;
    boolean isSecure;
    PasswordCredentials credentials;
    AuthenticationPrompt prompt;
    String preferedAuthentication = HttpAuthenticatorFactory.BASIC;
    boolean preemptiveAuthentication = false;
    HttpConnectionManager connections = new HttpConnectionManager(this);
    boolean includeCookies = true;
    Vector cookies = new Vector();
    int maxAuthenticationAttempts = 5;
    int proxyMaxAuthenticationAttempts = 5;
    boolean proxyPreemptiveAuthentication = false;
    HttpClient proxyClient;
    boolean credentialsFailed = false;

    /**
     * Proxy information
     */
    String proxyHost;
    int proxyPort = -1;
    int proxyType = PROXY_NONE;

    PasswordCredentials proxyCredentials;
    AuthenticationPrompt proxyAuthenticationPrompt;
    String proxyPreferedAuthentication = HttpAuthenticatorFactory.BASIC;
    boolean isProxyClient = false;

    /* DEBUG */static Log log = LogFactory.getLog(HttpClient.class);

    public HttpClient(String hostname, int port, boolean isSecure) {
        this.hostname = hostname;
        this.port = port;
        this.isSecure = isSecure;

    }
    
    /**
     * Set the default user agent.
     * 
     * @param userAgent default user agent
     */
    public static void setUserAgent(String userAgent) {
        USER_AGENT = userAgent;
    }

    HttpClient(HttpClient client) {
        this(client.proxyHost, client.proxyPort, client.proxyType == HttpClient.PROXY_HTTPS);
        setAuthenticationPrompt(client.proxyAuthenticationPrompt);
        setCredentials(client.proxyCredentials);
        setMaxAuthenticationAttempts(client.proxyMaxAuthenticationAttempts);
        setPreemtiveAuthentication(client.proxyPreemptiveAuthentication);
        setPreferredAuthentication(client.proxyPreferedAuthentication);
        this.isProxyClient = true;
    }

    boolean configureProxy() {
        /**
         * Attempt to automatically configure proxy from Maverick SSL proxy
         * settings
         */
        if (!isProxyClient && !isNonProxiedHost(hostname)) {

            if (System.getProperty("com.maverick.ssl.https.HTTPProxyHostname") != null) {

                setProxyHost(System.getProperty("com.maverick.ssl.https.HTTPProxyHostname"));

                /* DEBUG */log.debug("Set Maverick HTTP client proxy host to " + proxyHost);

                if (System.getProperty("com.maverick.ssl.https.HTTPProxyPort") != null) {
                    setProxyPort(Integer.parseInt(System.getProperty("com.maverick.ssl.https.HTTPProxyPort")));

                    /* DEBUG */log.debug("Set Maverick HTTP client proxy port to " + proxyPort);
                } else
                    setProxyPort(80);

                if (System.getProperty("com.maverick.ssl.https.HTTPProxySecure") != null) {
                    setProxyType(System.getProperty("com.maverick.ssl.https.HTTPProxySecure").equalsIgnoreCase("true") ? PROXY_HTTPS
                                    : PROXY_HTTP);
                } else
                    setProxyType(PROXY_HTTP);

                return true;
            }
        }

        return false;
    }

    public boolean isProxyConfigured() {
        if (proxyType == PROXY_NONE)
            return configureProxy();
        else
            return true;
    }

    public static boolean isNonProxiedHost(String host) {
        String nonProxiedHosts = System.getProperty("com.maverick.ssl.https.HTTPProxyNonProxyHosts");
        if (nonProxiedHosts == null || nonProxiedHosts.equals("")) {
            return false;
        }
        StringTokenizer t = new StringTokenizer(nonProxiedHosts, "|");
        while (t.hasMoreTokens()) {
            String token = t.nextToken();
            int idx = token.indexOf('*');
            if (idx != -1) {
                if (token.length() == 1) {
                    return true;
                }
                String before = token.substring(0, idx);
                String after = token.substring(idx + 1);
                if (((before.length() == 0) || host.startsWith(before)) && ((after.length() == 0) || host.endsWith(after))) {
                    return true;
                }
            } else {
                if (host.equalsIgnoreCase(token)) {
                    return true;
                }
            }
        }
        return false;
    }

    public HttpConnectionManager getConnectionManager() {
        return connections;
    }

    public String getHost() {
        return hostname;
    }

    public int getPort() {
        return port;
    }

    public boolean isSecure() {
        return isSecure;
    }

    public void setMaxAuthenticationAttempts(int maxAuthenticationAttempts) {
        this.maxAuthenticationAttempts = maxAuthenticationAttempts;
    }

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

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

    public void setProxyMaxAuthenticationAttempts(int proxyMaxAuthenticationAttempts) {
        this.proxyMaxAuthenticationAttempts = proxyMaxAuthenticationAttempts;
    }

    public void setProxyType(int proxyType) {
        if (proxyType > PROXY_HTTPS || proxyType < PROXY_NONE) {
            throw new IllegalArgumentException(proxyType + " is not a valid proxy type!");
        }
        this.proxyType = proxyType;
    }

    public void setProxyCredentials(PasswordCredentials proxyCredentials) {
        this.proxyCredentials = proxyCredentials;
    }

    public void setProxyPreemptiveAuthentication(boolean proxyPreemptiveAuthentication) {
        this.proxyPreemptiveAuthentication = proxyPreemptiveAuthentication;
    }

    public void setProxyAuthenticationPrompt(AuthenticationPrompt proxyAuthenticationPrompt) {
        this.proxyAuthenticationPrompt = proxyAuthenticationPrompt;
    }

    public void setProxyPreferedAuthentication(String scheme) {
        this.proxyPreferedAuthentication = scheme;
    }

    public void setAuthenticationPrompt(AuthenticationPrompt prompt) {
        this.prompt = prompt;
    }

    public void setPreferredAuthentication(String scheme) {
        this.preferedAuthentication = scheme;
    }

    public void setCredentials(PasswordCredentials credentials) {
        this.credentials = credentials;
        credentialsFailed = false;
    }

    public void close() {
        connections.closeConnections();
    }

    synchronized void prepareRequest(HttpRequest request, HttpMethod method, HttpConnection con) throws IOException, HttpException,
                    UnsupportedAuthenticationException, AuthenticationCancelledException {

        if (con.isMonitoring()) {
            con.monitor(method.getURI());
        }

        request.reset();

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -