Sprint boot设置Http代理

网上找到四种方案为applicaiton 设置Http和Https的代理:

方法一:设置java的启动参数

 在启动java时增加环境变量参数,比如:

 -Dhttp.proxyHost=ip -Dhttp.proxyPort=port -Dhttps.proxyHost=ip -Dhttps.proxyPort=port

 

 

方法二:在java代码初始化时设置环境变量:

  

System.setProperty("http.proxyHost", "代理ip");

System.setProperty("http.proxyPort", "3128");

System.setProperty("https.proxyHost", "代理ip");

System.setProperty("https.proxyPort", "3128");

 

 

方法三:在java 代码中设置使用代理:

URL url = new URL("https://某网址");

Proxy proxy = new Proxy(Proxy.Type.DIRECT.HTTP, new InetSocketAddress("代理ip",port));  

HttpURLConnection conn = (HttpURLConnection) url.openConnection(proxy);

 

 

方法四:如果操作系统已经配置好代理,可以直接使用

System.setProperty("java.net.useSystemProxies", "true");

当然也可以在启动时增加
-Djava.net.useSystemProxies=true

如果某些网址不需要使用代理,可以单独进行设置,比如:
-Dhttp.nonProxyHosts="localhost"

 

 亲身试验只有第二种最有效!

posted @ 2017-01-13 09:02  Crazy_Yang  阅读(3065)  评论(0)    收藏  举报