简体   繁体   English

每个线程使用自己的代理

[英]Each thread using its own proxy

I'm trying to set up a Java program, where each thread can use its own proxy. 我正在尝试建立一个Java程序,其中每个线程都可以使用其自己的代理。

Right now I only found a way to set a proxy globally. 现在,我只找到一种全局设置代理的方法。 ( http://docs.oracle.com/javase/6/docs/technotes/guides/net/proxies.html ) http://docs.oracle.com/javase/6/docs/technotes/guides/net/proxies.html

As mentioned earlier, these settings affect all http connections during the entire lifetime of the VM invoked with these options. 如前所述,这些设置会影响使用这些选项调用的VM的整个生命周期内的所有http连接。 However it is possible, using the System.setProperty() method, to have a slightly more dynamic behavior. 但是,使用System.setProperty()方法可以使动态行为稍微多一点。

Here is a code excerpt showing how this can be done: 这是一段代码摘录,显示了如何完成此操作:

//Set the http proxy to webcache.mydomain.com:8080 //将http代理设置为webcache.mydomain.com:8080

System.setProperty("http.proxyHost", "webcache.mydomain.com"); System.setProperty(“ http.proxyHost”,“ webcache.mydomain.com”); System.setPropery("http.proxyPort", "8080"); System.setPropery(“ http.proxyPort”,“ 8080”);

Update 更新

I tried using the proxy class, yet can't create a direct connection for when I don't want to use said proxy: 我尝试使用代理类,但是当我不想使用所述代理时无法创建直接连接:

private void setProxy()
{
    if(proxyUrl != null)
    {
        SocketAddress addr = new InetSocketAddress(proxyUrl, proxyPort);
        proxy = new Proxy(Proxy.Type.HTTP, addr);
    }
    else
    {
        proxy = new Proxy(Proxy.Type.DIRECT, null);
    }       
}

Exception in .... java.lang.IllegalArgumentException: type DIRECT is not compatible with address null

How can I get this to work for direct connections? 我如何才能将其用于直接连接? Haven't tried proxies yet. 尚未尝试代理。

Maybe use the Proxy class as explained here in section 3: 也许使用代理类作为解释这里在第3节:

As we have seen, the system properties are powerful, but not flexible. 如我们所见,系统属性是强大的,但不灵活。 The "all or nothing" behavior was justly deemed too severe a limitation by most developers. 大多数开发人员都认为“全有或全无”行为过于严格。 That's why it was decided to introduce a new, more flexible, API in J2SE 5.0 so that it would be possible to have connection based proxy settings. 这就是为什么决定在J2SE 5.0中引入一个新的,更灵活的API,以便有可能具有基于连接的代理设置。

You could use Proxy.NO_PROXY to: 您可以使用Proxy.NO_PROXY来:

... not to use any proxying. ...不使用任何代理。

Do something like this: 做这样的事情:

private void setProxy()
{
    if(proxyUrl != null)
    {
        SocketAddress addr = new InetSocketAddress(proxyUrl, proxyPort);
        proxy = new Proxy(Proxy.Type.HTTP, addr);
    }
    else
    {
        proxy = Proxy.NO_PROXY;
    }       
}

Barry NL's solution semi-worked, since I couldn't figure out how to not use a proxy with that solution. Barry NL的解决方案是行不通的,因为我不知道如何在该解决方案中不使用代理。

What I came up with: 我想到的是:

proxyUrl and proxyPort are in the constructor of my class. proxyUrlproxyPort在我的类的构造函数中。

I wasn't able to call url.openConnection(proxy); 我无法调用url.openConnection(proxy); if I just set proxy = null 如果我只是设置proxy = null

or proxy = new Proxy(Proxy.Type.DIRECT, null); proxy = new Proxy(Proxy.Type.DIRECT, null); . So I wrote my own openConnection 所以我写了我自己的openConnection

private void setProxy()
{
    if(proxyUrl != null)
    {
        SocketAddress addr = new InetSocketAddress(proxyUrl, proxyPort);
        proxy = new Proxy(Proxy.Type.HTTP, addr);
    }
    else
    {
        proxy = null;
    }       
}
private URLConnection openConnection2(URL url)
{
    try {
        if(proxy != null)               
            return url.openConnection(proxy);               
        else 
            return url.openConnection();            
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return null;
    }   
}

setProxy() has to be called before making any connections. 建立任何连接之前必须调用setProxy()

Also I replaced 我也换了

url = new URL(http_url);
con = (HttpURLConnection)url.openConnection(proxy);

with

url = new URL(http_url);
con = (HttpURLConnection)openConnection2(url);  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM