Search in sources :

Example 86 with Proxy

use of java.net.Proxy in project nutz by nutzam.

the class Http method setHttpProxy.

public static void setHttpProxy(String host, int port) {
    final Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(host, port));
    proxySwitcher = new ProxySwitcher() {

        public Proxy getProxy(URL url) {
            return proxy;
        }

        public Proxy getProxy(Request req) {
            if ("close".equals(req.getHeader().get("NoProxy")))
                return null;
            String url = req.getUrl().toString();
            if (url.startsWith("http") && url.contains("://") && url.length() > "https://".length()) {
                url = url.substring(url.indexOf("://") + "://".length());
                if (url.startsWith("127.0.0") || url.startsWith("localhost"))
                    return null;
            }
            req.getHeader().set("Connection", "close");
            return getProxy(req.getUrl());
        }
    };
}
Also used : Proxy(java.net.Proxy) InetSocketAddress(java.net.InetSocketAddress) URL(java.net.URL)

Example 87 with Proxy

use of java.net.Proxy in project nutz by nutzam.

the class Sender method openConnection.

protected void openConnection() throws IOException {
    if (this.interceptor != null)
        this.interceptor.beforeConnect(request);
    ProxySwitcher proxySwitcher = Http.proxySwitcher;
    if (proxySwitcher != null) {
        try {
            Proxy proxy = proxySwitcher.getProxy(request);
            if (proxy != null) {
                if (Http.autoSwitch) {
                    Socket socket = null;
                    try {
                        socket = new Socket();
                        socket.connect(proxy.address(), 5 * 1000);
                        OutputStream out = socket.getOutputStream();
                        out.write('\n');
                        out.flush();
                    } finally {
                        if (socket != null)
                            socket.close();
                    }
                }
                log.debug("connect via proxy : " + proxy + " for " + request.getUrl());
                conn = (HttpURLConnection) request.getUrl().openConnection(proxy);
                conn.setConnectTimeout(Default_Conn_Timeout);
                conn.setInstanceFollowRedirects(followRedirects);
                if (timeout > 0)
                    conn.setReadTimeout(timeout);
                else
                    conn.setReadTimeout(Default_Read_Timeout);
                return;
            }
        } catch (IOException e) {
            if (!Http.autoSwitch) {
                throw e;
            }
            log.info("Test proxy FAIl, fallback to direct connection", e);
        }
    }
    URL url = request.getUrl();
    String host = url.getHost();
    conn = (HttpURLConnection) url.openConnection();
    if (conn instanceof HttpsURLConnection) {
        if (sslSocketFactory != null)
            ((HttpsURLConnection) conn).setSSLSocketFactory(sslSocketFactory);
        else if (Http.sslSocketFactory != null)
            ((HttpsURLConnection) conn).setSSLSocketFactory(Http.sslSocketFactory);
    }
    if (!Lang.isIPv4Address(host)) {
        if (url.getPort() > 0 && url.getPort() != 80)
            host += ":" + url.getPort();
        conn.addRequestProperty("Host", host);
    }
    conn.setConnectTimeout(Default_Conn_Timeout);
    conn.setRequestMethod(request.getMethod().name());
    if (timeout > 0)
        conn.setReadTimeout(timeout);
    else
        conn.setReadTimeout(Default_Read_Timeout);
    conn.setInstanceFollowRedirects(followRedirects);
    if (interceptor != null)
        this.interceptor.afterConnect(request, conn);
}
Also used : Proxy(java.net.Proxy) OutputStream(java.io.OutputStream) FilterOutputStream(java.io.FilterOutputStream) IOException(java.io.IOException) Socket(java.net.Socket) URL(java.net.URL) HttpsURLConnection(javax.net.ssl.HttpsURLConnection)

Example 88 with Proxy

use of java.net.Proxy in project nutz by nutzam.

the class Http method setSocketProxy.

public static void setSocketProxy(String host, int port) {
    final Proxy proxy = new Proxy(Proxy.Type.SOCKS, new InetSocketAddress(host, port));
    proxySwitcher = new ProxySwitcher() {

        public Proxy getProxy(URL url) {
            return proxy;
        }

        public Proxy getProxy(Request req) {
            if ("close".equals(req.getHeader().get("NoProxy")))
                return null;
            String url = req.getUrl().toString();
            if (url.startsWith("http") && url.contains("://") && url.length() > "https://".length()) {
                url = url.substring(url.indexOf("://"));
                if (url.startsWith("127.0.0") || url.startsWith("localhost"))
                    return null;
            }
            req.getHeader().set("Connection", "close");
            return getProxy(req.getUrl());
        }
    };
}
Also used : Proxy(java.net.Proxy) InetSocketAddress(java.net.InetSocketAddress) URL(java.net.URL)

Example 89 with Proxy

use of java.net.Proxy in project hadoop by apache.

the class TestSocketFactory method testProxy.

/**
   * test proxy methods
   */
@Test(timeout = 5000)
public void testProxy() throws Exception {
    SocksSocketFactory templateWithoutProxy = new SocksSocketFactory();
    Proxy proxy = new Proxy(Type.SOCKS, InetSocketAddress.createUnresolved("localhost", 0));
    SocksSocketFactory templateWithProxy = new SocksSocketFactory(proxy);
    assertFalse(templateWithoutProxy.equals(templateWithProxy));
    Configuration configuration = new Configuration();
    configuration.set("hadoop.socks.server", "localhost:0");
    templateWithoutProxy.setConf(configuration);
    assertTrue(templateWithoutProxy.equals(templateWithProxy));
}
Also used : Proxy(java.net.Proxy) Configuration(org.apache.hadoop.conf.Configuration) SocksSocketFactory(org.apache.hadoop.net.SocksSocketFactory) Test(org.junit.Test)

Example 90 with Proxy

use of java.net.Proxy in project platformlayer by platformlayer.

the class ITAptCacheService method testProxy.

private String testProxy(InetSocketAddress proxySocketAddress, String fetchUrl) throws IOException {
    Proxy proxy = new Proxy(Proxy.Type.HTTP, proxySocketAddress);
    URL url = new URL(fetchUrl);
    HttpURLConnection uc = (HttpURLConnection) url.openConnection(proxy);
    uc.connect();
    InputStream is = uc.getInputStream();
    String html = IoUtils.readAll(is);
    is.close();
    System.out.println(html);
    return html;
}
Also used : Proxy(java.net.Proxy) HttpURLConnection(java.net.HttpURLConnection) InputStream(java.io.InputStream) URL(java.net.URL)

Aggregations

Proxy (java.net.Proxy)132 InetSocketAddress (java.net.InetSocketAddress)71 URL (java.net.URL)44 IOException (java.io.IOException)35 Test (org.junit.Test)22 URI (java.net.URI)21 ProxySelector (java.net.ProxySelector)20 HttpURLConnection (java.net.HttpURLConnection)19 SocketAddress (java.net.SocketAddress)19 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)13 URISyntaxException (java.net.URISyntaxException)11 ServerSocket (java.net.ServerSocket)10 Socket (java.net.Socket)9 PasswordAuthentication (java.net.PasswordAuthentication)8 InterruptedIOException (java.io.InterruptedIOException)6 URLConnection (java.net.URLConnection)6 SSLServerSocket (javax.net.ssl.SSLServerSocket)6 List (java.util.List)5 SSLSocket (javax.net.ssl.SSLSocket)5 OkHttpClient (okhttp3.OkHttpClient)5