Java 类org.apache.commons.httpclient.ProxyClient 实例源码

项目:jframe    文件:TlsTunnelBuilder.java   
private Socket AuthenticateProxy(ConnectMethod method, ProxyClient client, 
        String proxyHost, int proxyPort, 
        String proxyUsername, String proxyPassword) throws IOException {   
    if(method.getProxyAuthState().getAuthScheme().getSchemeName().equalsIgnoreCase("ntlm")) {
        // If Auth scheme is NTLM, set NT credentials with blank host and domain name
        client.getState().setProxyCredentials(new AuthScope(proxyHost, proxyPort), 
                        new NTCredentials(proxyUsername, proxyPassword,"",""));
    } else {
        // If Auth scheme is Basic/Digest, set regular Credentials
        client.getState().setProxyCredentials(new AuthScope(proxyHost, proxyPort), 
                new UsernamePasswordCredentials(proxyUsername, proxyPassword));
    }

    ProxyClient.ConnectResponse response = client.connect();
    Socket socket = response.getSocket();

    if (socket == null) {
        method = response.getConnectMethod();
        throw new ProtocolException("Proxy Authentication failed. Socket not created: " 
                + method.getStatusLine());
    }
    return socket;
}
项目:lib-commons-httpclient    文件:ProxyTunnelDemo.java   
public static void main(String[] args) throws Exception {

    ProxyClient proxyclient = new ProxyClient();
    // set the host the proxy should create a connection to
    //
    // Note:  By default port 80 will be used. Some proxies only allow conections
    // to ports 443 and 8443.  This is because the HTTP CONNECT method was intented
    // to be used for tunneling HTTPS.
    proxyclient.getHostConfiguration().setHost("www.yahoo.com");
    // set the proxy host and port
    proxyclient.getHostConfiguration().setProxy("10.0.1.1", 3128);
    // set the proxy credentials, only necessary for authenticating proxies
    proxyclient.getState().setProxyCredentials(
        new AuthScope("10.0.1.1", 3128, null),
        new UsernamePasswordCredentials("proxy", "proxy"));

    // create the socket
    ProxyClient.ConnectResponse response = proxyclient.connect(); 

    if (response.getSocket() != null) {
        Socket socket = response.getSocket();
        try {
            // go ahead and do an HTTP GET using the socket
            Writer out = new OutputStreamWriter(
                socket.getOutputStream(), "ISO-8859-1");
            out.write("GET http://www.yahoo.com/ HTTP/1.1\r\n");  
            out.write("Host: www.yahoo.com\r\n");  
            out.write("Agent: whatever\r\n");  
            out.write("\r\n");  
            out.flush();  
            BufferedReader in = new BufferedReader(
                new InputStreamReader(socket.getInputStream(), "ISO-8859-1"));
            String line = null;
            while ((line = in.readLine()) != null) {
                System.out.println(line);
            }
        } finally {
            // be sure to close the socket when we're done
            socket.close(); 
        }
    } else {
        // the proxy connect was not successful, check connect method for reasons why
        System.out.println("Connect failed: " + response.getConnectMethod().getStatusLine());
        System.out.println(response.getConnectMethod().getResponseBodyAsString());
    }
}
项目:jframe    文件:TlsTunnelBuilder.java   
@SuppressFBWarnings(value = "VA_FORMAT_STRING_USES_NEWLINE",
        justification = "use <CR><LF> as according to RFC, not platform-linefeed")
Socket makeTunnel(String host, int port, String proxyUsername, 
        String proxyPassword, InetSocketAddress proxyAddress) throws IOException {
    if(host == null || port < 0 || host.isEmpty() || proxyAddress == null){
        throw new ProtocolException("Incorrect parameters to build tunnel.");   
    }
    logger.debug("Creating socket for Proxy : " + proxyAddress.getAddress() + ":" + proxyAddress.getPort());
    Socket socket;
    try {
        ProxyClient client = new ProxyClient();
        client.getParams().setParameter("http.useragent", "java-apns");
        client.getHostConfiguration().setHost(host, port);
        String proxyHost = proxyAddress.getAddress().toString().substring(0, proxyAddress.getAddress().toString().indexOf("/"));
        client.getHostConfiguration().setProxy(proxyHost, proxyAddress.getPort());


        ProxyClient.ConnectResponse response = client.connect();
        socket = response.getSocket();
        if (socket == null) {
            ConnectMethod method = response.getConnectMethod();
            // Read the proxy's HTTP response.
            if(method.getStatusLine().toString().matches("HTTP/1\\.\\d 407 Proxy Authentication Required")) {
                // Proxy server returned 407. We will now try to connect with auth Header
                if(proxyUsername != null && proxyPassword != null) {
                    socket = AuthenticateProxy(method, client,proxyHost, proxyAddress.getPort(),
                            proxyUsername, proxyPassword);
                } else {
                    throw new ProtocolException("Socket not created: " + method.getStatusLine()); 
                }
            }             
        }

    } catch (Exception e) {
        throw new ProtocolException("Error occurred while creating proxy socket : " + e.toString());
    }
    if (socket != null) {
        logger.debug("Socket for proxy created successfully : " + socket.getRemoteSocketAddress().toString());
    }
    return socket;
}
项目:httpclient3-ntml    文件:ProxyTunnelDemo.java   
public static void main(String[] args) throws Exception {

    ProxyClient proxyclient = new ProxyClient();
    // set the host the proxy should create a connection to
    //
    // Note:  By default port 80 will be used. Some proxies only allow conections
    // to ports 443 and 8443.  This is because the HTTP CONNECT method was intented
    // to be used for tunneling HTTPS.
    proxyclient.getHostConfiguration().setHost("www.yahoo.com");
    // set the proxy host and port
    proxyclient.getHostConfiguration().setProxy("10.0.1.1", 3128);
    // set the proxy credentials, only necessary for authenticating proxies
    proxyclient.getState().setProxyCredentials(
        new AuthScope("10.0.1.1", 3128, null),
        new UsernamePasswordCredentials("proxy", "proxy"));

    // create the socket
    ProxyClient.ConnectResponse response = proxyclient.connect(); 

    if (response.getSocket() != null) {
        Socket socket = response.getSocket();
        try {
            // go ahead and do an HTTP GET using the socket
            Writer out = new OutputStreamWriter(
                socket.getOutputStream(), "ISO-8859-1");
            out.write("GET http://www.yahoo.com/ HTTP/1.1\r\n");  
            out.write("Host: www.yahoo.com\r\n");  
            out.write("Agent: whatever\r\n");  
            out.write("\r\n");  
            out.flush();  
            BufferedReader in = new BufferedReader(
                new InputStreamReader(socket.getInputStream(), "ISO-8859-1"));
            String line = null;
            while ((line = in.readLine()) != null) {
                System.out.println(line);
            }
        } finally {
            // be sure to close the socket when we're done
            socket.close(); 
        }
    } else {
        // the proxy connect was not successful, check connect method for reasons why
        System.out.println("Connect failed: " + response.getConnectMethod().getStatusLine());
        System.out.println(response.getConnectMethod().getResponseBodyAsString());
    }
}