【问题标题】:Using multiple http proxies in groovy在 groovy 中使用多个 http 代理
【发布时间】:2019-06-26 20:04:02
【问题描述】:

我需要在 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() 

【问题讨论】:

    标签: http groovy


    【解决方案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)
    
    

    【讨论】:

      猜你喜欢
      • 2021-04-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-03
      相关资源
      最近更新 更多