方法一:在java项目启动时添加环境变量参数(http || https),如下:
-Dhttps.proxySet=true
-Dhttp.keepAlive=false
-Dhttps.proxyHost=ip地址
-Dhttps.proxyPort=端口号
方法二:在java代码初始化时设置环境变量,如下:
System.setProperty("http.proxyHost", "代理ip");
System.setProperty("http.proxyPort", "8080");
System.setProperty("https.proxyHost", "代理ip");
System.setProperty("https.proxyPort", "");
方法三:直接用过Url使用代理,如下所示:
URL url = new URL("https://xxxxxxxxxxx");
Proxy proxy = new Proxy(Proxy.Type.DIRECT.HTTP, new InetSocketAddress("代理ip", 8080));
HttpURLConnection conn = (HttpURLConnection) url.openConnection(proxy);
方法四:直接使用操作系统配置的代理,直接使用集合
System.setProperty("java.net.useSystemProxies", "true");
当然也可以在启动时增加-Djava.net.useSystemProxies=true
如果某些网址不需要使用代理,可以单独进行设置,比如:
-Dhttp.nonProxyHosts="www.hongxuejing.com|localhost"