如何让HttpURLConnection使用代理?如果我这样做......conn = new URL(urlString).openConnection();System.out.println("Proxy? " + conn.usingProxy());它打印Proxy? false问题是,我支持代理。JVM从哪里获取Windows上的代理信息?我该如何设置?我的所有其他应用程序似乎对我的代理非常满意。
3 回答

郎朗坤
TA贡献1921条经验 获得超9个赞
从java 1.5开始,您还可以将java.net.Proxy实例传递给openConnection(proxy)
方法:
//Proxy instance, proxy ip = 10.0.0.1 with port 8080Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("10.0.0.1", 8080));conn = new URL(urlString).openConnection(proxy);
如果您的代理需要身份验证,它将为您提供响应407。
在这种情况下,您将需要以下代码:
Authenticator authenticator = new Authenticator() { public PasswordAuthentication getPasswordAuthentication() { return (new PasswordAuthentication("user", "password".toCharArray())); } }; Authenticator.setDefault(authenticator);

缥缈止盈
TA贡献2041条经验 获得超4个赞
这很容易从互联网上回答。设置系统属性http.proxyHost
和http.proxyPort
。您可以System.setProperty()
使用-D
语法执行此操作,也可以从命令行执行此操作。
添加回答
举报
0/150
提交
取消