我需要在groovy脚本中并行使用多个http代理。例如
url1 = 'https://boston.myorg.com'
proxy1 = 10.0.0.3:8000
url2 = 'https://newyork.myorg.com'
proxy2 = 10.0.0.5:8001
我发现了下面的示例,但是不是为脚本中的每个连接设置了代理吗?有没有一种方法可以使每个连接使用自己的代理?
System.properties.putAll( ["http.proxyHost":"proxy-host", "http.proxyPort":"proxy-port"] )
def url = 'http://www.google.com/images/logo.gif'
def file = new FileOutputStream(address.tokenize("/")[-1])
def out = new BufferedOutputStream(file)
out << new URL(address).openStream()
out.close()
答案 0 :(得分:1)
https://docs.oracle.com/javase/8/docs/technotes/guides/net/proxies.html
代理类部分
SocketAddress proxy1Addr = new InetSocketAddress("10.0.0.3", 8000)
Proxy proxy1 = new Proxy(Proxy.Type.HTTP, proxy1Addr)
URL url1 = new URL("https://boston.myorg.com/")
URLConnection conn1 = url1.openConnection(proxy1)