Search in sources :

Example 41 with Proxy

use of java.net.Proxy in project jdk8u_jdk by JetBrains.

the class SecurityPolicy method test6578647.

static void test6578647() throws Exception {
    BufferedReader reader;
    java.net.Authenticator.setDefault(new KnowAllAuthenticator());
    reader = new BufferedReader(new InputStreamReader(webUrl.openConnection().getInputStream()));
    if (!reader.readLine().equals(CONTENT)) {
        throw new RuntimeException("Bad content");
    }
    reader = new BufferedReader(new InputStreamReader(proxyUrl.openConnection(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(PROXY_HOST, proxyPort))).getInputStream()));
    if (!reader.readLine().equals(CONTENT)) {
        throw new RuntimeException("Bad content");
    }
}
Also used : Proxy(java.net.Proxy) InputStreamReader(java.io.InputStreamReader) InetSocketAddress(java.net.InetSocketAddress) BufferedReader(java.io.BufferedReader)

Example 42 with Proxy

use of java.net.Proxy in project jdk8u_jdk by JetBrains.

the class B6737819 method main.

public static void main(String[] args) throws Exception {
    System.setProperty("http.proxyHost", "myproxy");
    System.setProperty("http.proxyPort", "8080");
    ProxySelector sel = ProxySelector.getDefault();
    java.util.List<Proxy> l;
    // from going through the HTTP proxy
    for (String s : uris) {
        l = sel.select(new URI(s));
        if (l.size() == 1 && l.get(0).type() != Proxy.Type.DIRECT) {
            throw new RuntimeException("ProxySelector returned the wrong proxy for " + s);
        }
    }
    // Let's override the default nonProxyHosts and make sure we now get a
    // HTTP proxy
    System.setProperty("http.nonProxyHosts", "");
    for (String s : uris) {
        l = sel.select(new URI(s));
        if (l.size() == 1 && l.get(0).type() != Proxy.Type.HTTP) {
            throw new RuntimeException("ProxySelector returned the wrong proxy for " + s);
        }
    }
}
Also used : ProxySelector(java.net.ProxySelector) Proxy(java.net.Proxy) URI(java.net.URI)

Example 43 with Proxy

use of java.net.Proxy in project android_frameworks_base by AOSPA.

the class PacProxySelector method parseResponse.

private static List<Proxy> parseResponse(String response) {
    String[] split = response.split(";");
    List<Proxy> ret = Lists.newArrayList();
    for (String s : split) {
        String trimmed = s.trim();
        if (trimmed.equals("DIRECT")) {
            ret.add(java.net.Proxy.NO_PROXY);
        } else if (trimmed.startsWith(PROXY)) {
            Proxy proxy = proxyFromHostPort(Type.HTTP, trimmed.substring(PROXY.length()));
            if (proxy != null) {
                ret.add(proxy);
            }
        } else if (trimmed.startsWith(SOCKS)) {
            Proxy proxy = proxyFromHostPort(Type.SOCKS, trimmed.substring(SOCKS.length()));
            if (proxy != null) {
                ret.add(proxy);
            }
        }
    }
    if (ret.size() == 0) {
        ret.add(java.net.Proxy.NO_PROXY);
    }
    return ret;
}
Also used : Proxy(java.net.Proxy)

Example 44 with Proxy

use of java.net.Proxy in project yyl_example by Relucent.

the class HttpProxy method main.

public static void main(String[] args) {
    try {
        URL url = new URL("http://www.baidu.com");
        // 创建代理服务器
        InetSocketAddress addr = new InetSocketAddress("10.14.38.235", 9090);
        // Proxy proxy = new Proxy(Proxy.Type.SOCKS, addr); // Socket 代理
        // http 代理
        Proxy proxy = new Proxy(Proxy.Type.HTTP, addr);
        // 如果我们知道代理server的名字, 可以直接使用
        // 结束
        URLConnection conn = url.openConnection(proxy);
        InputStream in = conn.getInputStream();
        // InputStream in = url.openStream();
        String s = toString(in);
        System.out.println(s);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : Proxy(java.net.Proxy) InetSocketAddress(java.net.InetSocketAddress) InputStream(java.io.InputStream) URL(java.net.URL) URLConnection(java.net.URLConnection) IOException(java.io.IOException)

Example 45 with Proxy

use of java.net.Proxy in project android_frameworks_base by ResurrectionRemix.

the class PacProxySelector method parseResponse.

private static List<Proxy> parseResponse(String response) {
    String[] split = response.split(";");
    List<Proxy> ret = Lists.newArrayList();
    for (String s : split) {
        String trimmed = s.trim();
        if (trimmed.equals("DIRECT")) {
            ret.add(java.net.Proxy.NO_PROXY);
        } else if (trimmed.startsWith(PROXY)) {
            Proxy proxy = proxyFromHostPort(Type.HTTP, trimmed.substring(PROXY.length()));
            if (proxy != null) {
                ret.add(proxy);
            }
        } else if (trimmed.startsWith(SOCKS)) {
            Proxy proxy = proxyFromHostPort(Type.SOCKS, trimmed.substring(SOCKS.length()));
            if (proxy != null) {
                ret.add(proxy);
            }
        }
    }
    if (ret.size() == 0) {
        ret.add(java.net.Proxy.NO_PROXY);
    }
    return ret;
}
Also used : Proxy(java.net.Proxy)

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