方法一:
全局设置为代理的模式
System.setProperty("http.proxyHost", "127.0.0.1");
System.setProperty("http.proxyPort", "1080");
System.setProperty("https.proxyHost", "127.0.0.1");
System.setProperty("https.proxyPort", "1080");
方法二:
首先理解JVM参数设置中 -D的作用
举例如下:
-Daaa=123
编辑代码即等于
System.setProperty("aaa", "123");
结合方法一。即可以通过JVM参数的方式设置代理
方法三:
代码如下
SocketAddress socketAddress = new InetSocketAddress("127.0.0.1", 1080);
Proxy proxy = new Proxy(Proxy.Type.HTTP, socketAddress);
URL url = new URL("https://www.google.com");
InputStream stream = url.openConnection(proxy).getInputStream();