2

我有一个需要连接到网络的应用程序。在处理代理连接时我需要一些建议。目前用户设置代理设置,因此我使用输入的信息进行连接。有没有更好的方法来处理这种情况。

我的意思是 chrome 之类的东西,它打开系统的代理设置然后使用它们。如何做到这一点并检索这些值?还有其他理想的方法吗?

其次,目前我正在检查是否有代理集。如果是,我使用的是url.openConnection(proxy); IF not then plainurl.openConnection();有没有更清洁的方法?其中系统自动连接代理集。

4

5 回答 5

3
//Set the http proxy to webcache.mydomain.com:8080

System.setProperty( "http.proxyHost", "webcache.mydomain.com" );
System.setProperty( "http.proxyPort", "8080" );

System.setProperty( "https.proxyHost", "webcache.mydomain.com" );
System.setProperty( "https.proxyPort", "8080" );
于 2012-10-31T07:07:40.853 回答
2

从源代码我们可以使用

System.getProperties().put("http.proxyHost", "ProxyURL");
System.getProperties().put("http.proxyPort", "ProxyPort");
System.getProperties().put("http.proxyUser", "UserName");
System.getProperties().put("http.proxyPassword", "Password");

命令行 :

  $> java -Dhttp.proxyHost=proxyhostURL -Dhttp.proxyPort=proxyPortNumber
-Dhttp.proxyUser=UserName -Dhttp.proxyPassword=Password ProxyClassHere

文档

于 2012-10-31T07:37:08.297 回答
0

我遇到了同样的问题,想使用 SOAP 客户端调用 1 个 WSDL。我能够通过 SOAP UI 调用 WSDL 但是当我尝试通过我的 JAVA 代码打包请求时,它失败了。我发现了问题,我的 java 代码没有选择代理的集合。我通过在我的 Eclipse 中设置这些代理来明确尝试:Eclipse -> Windows -> Preferences -> Geneal -> Network Connection。将本机更改为手动并添加了代理和端口。尽管如此,它还是没有奏效。最后,我在我的代码中只添加了 1 行并且它全部工作: System.setProperty("java.net.useSystemProxies", "true"); 如果您的 JAVA 主页设置正确,这肯定会在 Eclipse 中获取系统设置代理。

谢谢 Saurabh M. Chande

于 2014-08-20T08:30:37.773 回答
0

也看看这个: How do I set the proxy to be used by the JVM

可以通过使用一些标志启动 JVM 来完成: JAVA_FLAGS=-Dhttp.proxyHost=10.0.0.100 -Dhttp.proxyPort=8800 java ${JAVA_FLAGS}

于 2012-10-31T07:28:22.347 回答
0

您所需要的一切:https ://docs.oracle.com/javase/7/docs/api/java/net/doc-files/net-properties.html

忠告:不要使用System.getProperties().put,见http://hg.openjdk.java.net/jdk8/jdk8/jdk/file/687fd7c7986d/src/share/classes/java/util/Properties.java

/**
 * ....
 * Because {@code Properties} inherits from {@code Hashtable}, the
 * {@code put} and {@code putAll} methods can be applied to a
 * {@code Properties} object.  Their use is strongly discouraged as they
 * allow the caller to insert entries whose keys or values are not
 * {@code Strings}.  The {@code setProperty} method should be used
 * instead.
 * ....
 */

(如果你使用Properties::put非字符串值,你会遇到麻烦)

于 2016-02-29T09:57:35.740 回答