izb*_*izb 128 java windows proxy http
如果我这样做......
conn = new URL(urlString).openConnection();
System.out.println("Proxy? " + conn.usingProxy());
Run Code Online (Sandbox Code Playgroud)
它打印
Proxy? false
Run Code Online (Sandbox Code Playgroud)
问题是,我支持代理.JVM从哪里获取Windows上的代理信息?我该如何设置?我的所有其他应用程序似乎对我的代理非常满意.
Nic*_*kDK 322
从java 1.5开始,您还可以将java.net.Proxy实例传递给openConnection(proxy)
方法:
//Proxy instance, proxy ip = 10.0.0.1 with port 8080
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("10.0.0.1", 8080));
conn = new URL(urlString).openConnection(proxy);
Run Code Online (Sandbox Code Playgroud)
如果您的代理需要身份验证,它将为您提供响应407.
在这种情况下,您将需要以下代码:
Authenticator authenticator = new Authenticator() {
public PasswordAuthentication getPasswordAuthentication() {
return (new PasswordAuthentication("user",
"password".toCharArray()));
}
};
Authenticator.setDefault(authenticator);
Run Code Online (Sandbox Code Playgroud)
Sea*_*wen 33
这很容易从互联网上回答.设置系统属性http.proxyHost
和http.proxyPort
.您可以System.setProperty()
使用-D
语法执行此操作,也可以从命令行执行此操作.
Pas*_*ent 19
通过两个系统属性支持代理:http.proxyHost和http.proxyPort.它们必须分别设置为代理服务器和端口.以下基本示例说明了它:
String url = "http://www.google.com/",
proxy = "proxy.mydomain.com",
port = "8080";
URL server = new URL(url);
Properties systemProperties = System.getProperties();
systemProperties.setProperty("http.proxyHost",proxy);
systemProperties.setProperty("http.proxyPort",port);
HttpURLConnection connection = (HttpURLConnection)server.openConnection();
connection.connect();
InputStream in = connection.getInputStream();
readResponse(in);
Run Code Online (Sandbox Code Playgroud)
Dan*_*art 11
你也可以设置
-Djava.net.useSystemProxies=true
Run Code Online (Sandbox Code Playgroud)
在Windows和Linux上,这将使用系统设置,因此您不需要重复自己(DRY)
http://docs.oracle.com/javase/7/docs/api/java/net/doc-files/net-properties.html#Proxies
ZZ *_*der 10
在openConnection之前设置以下内容,
System.setProperty("http.proxyHost", "host");
System.setProperty("http.proxyPort", "port_number");
Run Code Online (Sandbox Code Playgroud)
如果代理需要身份验证
System.setProperty("http.proxyUser", "user");
System.setProperty("http.proxyPassword", "password");
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
245417 次 |
最近记录: |