Java HttpURLConnection 使用 SOCKS 代理而不是 HTTP [英] Java HttpURLConnection uses SOCKS proxy instead of HTTP

查看:45
本文介绍了Java HttpURLConnection 使用 SOCKS 代理而不是 HTTP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常简单的代码,它使用 HttpURLConnection 通过代理访问某个网站

I have a very simple code that uses HttpURLConnection to access some web site via proxy

    System.setProperty("java.net.useSystemProxies", "true");
    System.out.println("Proxy: " + ProxySelector.getDefault().select(new URI(urlS)));
    URL url = new URL(urlS);
    HttpURLConnection ic = (HttpURLConnection)url.openConnection();

    ic.connect();

出于某种原因,Java 认为我需要 SOCKS 代理,而不是 http,抛出以下异常:

For some reason, Java thinks that I need SOCKS proxy, not http, throwing the following exception:

ERROR: Can't connect to SOCKS proxy:Connection timed out: connect

推荐答案

如果您在 Windows 上遇到此问题,您可能会遇到 Java 错误.

If you are having this issues on Windows, you may run into a Java bug.

Java 将任何系统代理设置视为 SOCKS.您必须在 Windows 中禁用 useSystemProxies 或不使用代理.

Java treats any system proxy setting as SOCKS. You have to either disable useSystemProxies or don't use proxy in Windows.

如果需要代理,请尝试取消选中对所有协议使用相同的代理服务器",确保 SOCKS 代理的字段为空.这解决了我们的问题.

If proxy is needed, try to uncheck "Use the same proxy server for all protocols", making sure the field for the SOCKS proxy is blank. That fixed our problem.

这篇关于Java HttpURLConnection 使用 SOCKS 代理而不是 HTTP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆