Java使用代理创建HttpURLConnection链接

走着路睡觉
  • java
小于 1 分钟

Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("10.0.0.1", 8080));
conn = new URL(urlString).openConnection(proxy);

如果你的代理需要账号密码认证,代码如下:

Authenticator authenticator = new Authenticator() {

        public PasswordAuthentication getPasswordAuthentication() {
            return (new PasswordAuthentication("user",
                    "password".toCharArray()));
        }
    };
    Authenticator.setDefault(authenticator);
上次编辑于:
贡献者: zhaojingbo
Loading...