hc-httpclient-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jeroen ter Voorde <j.tervoo...@home.nl>
Subject Re: Automatic Proxy Configuration
Date Tue, 12 Jul 2005 08:52:51 GMT
I'm using this, seems to work on windows using plugin 1.4 and 1.5:

    /**
     * Detects the proxy to use (if configured in the browser). The 
return string
     * is either "DIRECT" (no proxy configured) or "PROXY host:port 
[;DIRECT | PROXY...]"
     *
     * @param url the url to find the proxy for.
     * @return the proxy host or the string "DIRECT"
     */   
    public String detectProxyForUrl(String hostUrl) {
        try {
            com.sun.java.browser.net.ProxyInfo[] pi = 
com.sun.java.browser.net.ProxyService.getProxyInfo(new URL(hostUrl));
            if (pi == null || pi.length == 0) {
                log.info("ProxyService reported NULL proxy (no proxy 
assumed)");
                return "DIRECT";
            }
            else {
                log.info("ProxyService returned proxy "+pi[0].getHost()+ 
":"+pi[0].getPort()+".");
               
                String host = pi[0].getHost();
                int port = pi[0].getPort();
                return "PROXY " + host + ":" + port;
            }
        }
        catch (Exception ee) {
            log.error("Could not detect proxy info.", ee);
        }
        return "DIRECT";
    }


Gábor SEBESTYÉN wrote:

>
> On 2005.07.12., at 6:27, grlea@dev.java.net wrote:
>
>> Just wondering if HttpClient is able to do automatic proxy  
>> configuration,
>> i.e. steal the proxy settings from the default browser?
>>
>> If not, is there any plan to implement such a feature?
>>
> Here is my own proxy detection method. It reads all info from system  
> properties. It works on Java 1.4.2 but on Java 1.5 doesn't because  
> Java 1.5 doesn't store proxy information in system properties AFAIK.  
> But correct me if I'm wrong.
>
> Here's my code:
>
>
>      public void detectProxy(Properties p) {
>         ArrayList proxies = new ArrayList();
>         if (p.containsKey("http.proxyHost")) {
>             HashMap pm = new HashMap();
>
>             /*
>              * http.agent = Mozilla/4.0 (Mac OS X 10.4.1)
>              * http.auth.serializeRequests = true
>              * http.nonProxyHosts = www|*.www|monarakh.hu| 
> *.monarakh.hu|core0|*.core0|core1|*.core1|10.0.0.2|127.0.0.1
>              * http.proxyHost = core0
>              * http.proxyPort = 3128
>              * https.protocols = SSLv3,SSLv2Hello
>              * https.proxyHost = core0
>              * https.proxyPort = 3128
>              */
>
>             try {
>                 HashMap m = new HashMap();
>                 m.put("host", p.getProperty("http.proxyHost"));
>                 m.put("port", p.getProperty("http.proxyPort"));
>                 pm.put("http", m);
>             } catch (Exception exc) {
>                 // debug
>                 System.out.println(getClass() + ": failed to set  http 
> proxy setting, exc="+ exc);
>             }
>
>             try {
>                 HashMap m = new HashMap();
>                 m.put("host", p.getProperty("https.proxyHost"));
>                 m.put("port", p.getProperty("https.proxyPort"));
>                 pm.put("https", m);
>             } catch (Exception exc) {
>                 // debug
>                 System.out.println(getClass() + ": failed to set  
> https proxy setting, exc="+ exc);
>             }
>
>             // put("proxy.autodetected", pm);
>             proxies.add(pm);
>         }
>
>         if (p.containsKey("javaplugin.proxy.config.list")) {
>             HashMap pm = new HashMap();
>             /*
>              * javaplugin.proxy.config.bypass =  
> www,*.www,monarakh.hu,*.monarakh.hu,core0,*.core0,core1,*.core1,10.0.0.2 
> ,127.0.0.1
>              * javaplugin.proxy.config.list =  
> http=core0:3128,https=core0:3128
>              * javaplugin.proxy.config.type = manual
>              * javaplugin.proxy.usebrowsersettings = true
>              */
>             String str = p.getProperty("javaplugin.proxy.config.list");
>             if (str != null) {
>                 if (str.length() > 0) {
>                     String[] list = str.split(",");
>                     for (int i=0; i<list.length; i++) {
>                         String sel = null;
>                         String key = null;
>                         if (list[i].startsWith("http=")) {
>                             sel = list[i].substring("http=".length());
>                             key = "http";
>                         } else if (list[i].startsWith("https=")) {
>                             sel = list[i].substring("https=".length());
>                             key = "https";
>                         }
>
>                         if (sel != null) {
>                             // TODO: no we have host:port pairs.  take 
> it apart and drop into a map
>                             String[] ops = sel.split(":");
>                             try {
>                                 HashMap m = new HashMap();
>                                 m.put("host", ops[0]);
>                                 m.put("port", ops[1]);
>                                 pm.put(key, m);
>                             } catch (Exception exc) {
>                                 // debug
>                                 System.out.println(getClass() + ":  
> failed to set http proxy setting, exc="+ exc);
>                             }
>                         } else {
>                             // debug
>                             System.out.println(getClass() + ":  
> unknown proxy type: "+ list[i]);
>                         }
>                     }
>                 } else {
>                     // debug
>                     System.out.println(getClass() + ":  
> <javaplugin.proxy.config.list> is empty!");
>                 }
>
>             }
>             proxies.add(pm);
>         }
>
>         put("proxies", proxies);
>     }
>
> Hope it helps,
>
>     Gábor
>
>
>
> "Always forgive your enemies. Nothing annoys them so much. (Oscar  
> Wilde, 1854-1900)"
>
>


---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: httpclient-user-help@jakarta.apache.org


Mime
View raw message