如何通过 SOCKS 代理使用 URLConnection?

发布于 2024-09-08 00:06:26 字数 148 浏览 1 评论 0原文

如何为 (new URL(url)).openConnection() 给出的 URLConnection 设置 SOCKS 代理?

每个连接都需要不同的代理,因此请不要建议设置系统属性。

How can I set the SOCKS proxy for a URLConnection given by (new URL(url)).openConnection()?

Different proxies are needed on a per-connection basis, so please don't suggest setting system properties.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

这个俗人 2024-09-15 00:06:27

如果要对每个连接使用不同的代理设置,可以使用 Java 6.0 ProxySelector 机制,如 Java 网络和代理。具体来说,请阅读第 3 节和第 4 节。

If you want to use different proxy settings on a per-connection bases, you can use the Java 6.0 ProxySelector mechanism as described in Java Networking and Proxies. Specifically, read sections 3 and 4.

妄断弥空 2024-09-15 00:06:27

您可以使用 ProxyProxySelector 类设置每个连接代理。以下是使用 Java 11 HttpClient 和旧版 HttpURLConnection 进行按连接代理的示例:

public static void java11Http(String url) throws Exception {
    ProxySelector proxySelector = new ProxySelector() {
        @Override
        public List<Proxy> select(URI uri) {
            return List.of(new Proxy(Proxy.Type.SOCKS, new InetSocketAddress("127.0.0.1", 1234)));
        }
        @Override
        public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
            ioe.printStackTrace();
        }
    };

    HttpClient client = HttpClient.newBuilder()
            .proxy(proxySelector)
            .build();
    HttpRequest request = HttpRequest.newBuilder()
            .uri(URI.create(url))
            .build();

    HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
    System.out.println(response.body());
}

private static void legacyJavaHttp(String url) {
    SocketAddress proxyAddr = new InetSocketAddress("127.0.0.1", 1234);
    Proxy pr = new Proxy(Proxy.Type.SOCKS, proxyAddr);

    try {
        HttpURLConnection con = (HttpURLConnection) URI.create(url).toURL().openConnection(pr);
        con.setConnectTimeout(5000);
        con.setReadTimeout(5000);
        con.connect();
        System.out.println(con.getResponseMessage());
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

您可以在此处阅读有关 Java 代理的更多信息:https://docs.oracle.com/javase/8/docs/technotes/guides/网/proxies.html

You can set up per-connection proxies with the Proxy and ProxySelector classes. Here is an example of per-connection proxying with the Java 11 HttpClient and the legacy HttpURLConnection:

public static void java11Http(String url) throws Exception {
    ProxySelector proxySelector = new ProxySelector() {
        @Override
        public List<Proxy> select(URI uri) {
            return List.of(new Proxy(Proxy.Type.SOCKS, new InetSocketAddress("127.0.0.1", 1234)));
        }
        @Override
        public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
            ioe.printStackTrace();
        }
    };

    HttpClient client = HttpClient.newBuilder()
            .proxy(proxySelector)
            .build();
    HttpRequest request = HttpRequest.newBuilder()
            .uri(URI.create(url))
            .build();

    HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
    System.out.println(response.body());
}

private static void legacyJavaHttp(String url) {
    SocketAddress proxyAddr = new InetSocketAddress("127.0.0.1", 1234);
    Proxy pr = new Proxy(Proxy.Type.SOCKS, proxyAddr);

    try {
        HttpURLConnection con = (HttpURLConnection) URI.create(url).toURL().openConnection(pr);
        con.setConnectTimeout(5000);
        con.setReadTimeout(5000);
        con.connect();
        System.out.println(con.getResponseMessage());
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

You can read more on Java proxying here: https://docs.oracle.com/javase/8/docs/technotes/guides/net/proxies.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文