FileDocCategorySizeDatePackage
Proxy.javaAPI DocAndroid 5.1 API11676Thu Mar 12 22:22:10 GMT 2015android.net

Proxy

public final class Proxy extends Object
A convenience class for accessing the user and default proxy settings.

Fields Summary
private static final boolean
DEBUG
private static final String
TAG
private static final ProxySelector
sDefaultProxySelector
public static final String
PROXY_CHANGE_ACTION
Used to notify an app that's caching the default connection proxy that either the default connection or its proxy has changed. The intent will have the following extra value:

  • EXTRA_PROXY_INFO - The ProxyProperties for the proxy. Non-null, though if the proxy is undefined the host string will be empty.

This is a protected intent that can only be sent by the system

public static final String
EXTRA_PROXY_INFO
Intent extra included with {@link #PROXY_CHANGE_ACTION} intents. It describes the new proxy being used (as a {@link ProxyInfo} object).
public static final int
PROXY_VALID
public static final int
PROXY_HOSTNAME_EMPTY
public static final int
PROXY_HOSTNAME_INVALID
public static final int
PROXY_PORT_EMPTY
public static final int
PROXY_PORT_INVALID
public static final int
PROXY_EXCLLIST_INVALID
private static ConnectivityManager
sConnectivityManager
private static final String
NAME_IP_REGEX
private static final String
HOSTNAME_REGEXP
private static final Pattern
HOSTNAME_PATTERN
private static final String
EXCL_REGEX
private static final String
EXCLLIST_REGEXP
private static final Pattern
EXCLLIST_PATTERN
Constructors Summary
Methods Summary
public static final java.lang.StringgetDefaultHost()
Return the default proxy host specified by the carrier.

return
String containing the host name or null if there is no proxy for this carrier.
deprecated
Use standard java vm proxy values to find the host, port and exclusion list. This call ignores the exclusion list and no longer reports only mobile-data apn-based proxy values.

        String host = System.getProperty("http.proxyHost");
        if (TextUtils.isEmpty(host)) return null;
        return host;
    
public static final intgetDefaultPort()
Return the default proxy port specified by the carrier.

return
The port number to be used with the proxy host or -1 if there is no proxy for this carrier.
deprecated
Use standard java vm proxy values to find the host, port and exclusion list. This call ignores the exclusion list and no longer reports only mobile-data apn-based proxy values.

        if (getDefaultHost() == null) return -1;
        try {
            return Integer.parseInt(System.getProperty("http.proxyPort"));
        } catch (NumberFormatException e) {
            return -1;
        }
    
public static final java.lang.StringgetHost(android.content.Context ctx)
Return the proxy host set by the user.

param
ctx A Context used to get the settings for the proxy host.
return
String containing the host name. If the user did not set a host name it returns the default host. A null value means that no host is to be used.
deprecated
Use standard java vm proxy values to find the host, port and exclusion list. This call ignores the exclusion list.

        java.net.Proxy proxy = getProxy(ctx, null);
        if (proxy == java.net.Proxy.NO_PROXY) return null;
        try {
            return ((InetSocketAddress)(proxy.address())).getHostName();
        } catch (Exception e) {
            return null;
        }
    
public static final intgetPort(android.content.Context ctx)
Return the proxy port set by the user.

param
ctx A Context used to get the settings for the proxy port.
return
The port number to use or -1 if no proxy is to be used.
deprecated
Use standard java vm proxy values to find the host, port and exclusion list. This call ignores the exclusion list.

        java.net.Proxy proxy = getProxy(ctx, null);
        if (proxy == java.net.Proxy.NO_PROXY) return -1;
        try {
            return ((InetSocketAddress)(proxy.address())).getPort();
        } catch (Exception e) {
            return -1;
        }
    
public static final org.apache.http.HttpHostgetPreferredHttpHost(android.content.Context context, java.lang.String url)
Returns the preferred proxy to be used by clients. This is a wrapper around {@link android.net.Proxy#getHost()}.

param
context the context which will be passed to {@link android.net.Proxy#getHost()}
param
url the target URL for the request
note
Calling this method requires permission android.permission.ACCESS_NETWORK_STATE
return
The preferred proxy to be used by clients, or null if there is no proxy. {@hide}

        java.net.Proxy prefProxy = getProxy(context, url);
        if (prefProxy.equals(java.net.Proxy.NO_PROXY)) {
            return null;
        } else {
            InetSocketAddress sa = (InetSocketAddress)prefProxy.address();
            return new HttpHost(sa.getHostName(), sa.getPort(), "http");
        }
    
public static final java.net.ProxygetProxy(android.content.Context ctx, java.lang.String url)
Return the proxy object to be used for the URL given as parameter.

param
ctx A Context used to get the settings for the proxy host.
param
url A URL to be accessed. Used to evaluate exclusion list.
return
Proxy (java.net) object containing the host name. If the user did not set a hostname it returns the default host. A null value means that no host is to be used. {@hide}


     
        HOSTNAME_PATTERN = Pattern.compile(HOSTNAME_REGEXP);
        EXCLLIST_PATTERN = Pattern.compile(EXCLLIST_REGEXP);
        sDefaultProxySelector = ProxySelector.getDefault();
    
        String host = "";
        if ((url != null) && !isLocalHost(host)) {
            URI uri = URI.create(url);
            ProxySelector proxySelector = ProxySelector.getDefault();

            List<java.net.Proxy> proxyList = proxySelector.select(uri);

            if (proxyList.size() > 0) {
                return proxyList.get(0);
            }
        }
        return java.net.Proxy.NO_PROXY;
    
private static final booleanisLocalHost(java.lang.String host)

        if (host == null) {
            return false;
        }
        try {
            if (host != null) {
                if (host.equalsIgnoreCase("localhost")) {
                    return true;
                }
                if (NetworkUtils.numericToInetAddress(host).isLoopbackAddress()) {
                    return true;
                }
            }
        } catch (IllegalArgumentException iex) {
        }
        return false;
    
public static final voidsetHttpProxySystemProperty(java.lang.String host, java.lang.String port, java.lang.String exclList, Uri pacFileUrl)

hide

        if (exclList != null) exclList = exclList.replace(",", "|");
        if (false) Log.d(TAG, "setHttpProxySystemProperty :"+host+":"+port+" - "+exclList);
        if (host != null) {
            System.setProperty("http.proxyHost", host);
            System.setProperty("https.proxyHost", host);
        } else {
            System.clearProperty("http.proxyHost");
            System.clearProperty("https.proxyHost");
        }
        if (port != null) {
            System.setProperty("http.proxyPort", port);
            System.setProperty("https.proxyPort", port);
        } else {
            System.clearProperty("http.proxyPort");
            System.clearProperty("https.proxyPort");
        }
        if (exclList != null) {
            System.setProperty("http.nonProxyHosts", exclList);
            System.setProperty("https.nonProxyHosts", exclList);
        } else {
            System.clearProperty("http.nonProxyHosts");
            System.clearProperty("https.nonProxyHosts");
        }
        if (!Uri.EMPTY.equals(pacFileUrl)) {
            ProxySelector.setDefault(new PacProxySelector());
        } else {
            ProxySelector.setDefault(sDefaultProxySelector);
        }
    
public static final voidsetHttpProxySystemProperty(ProxyInfo p)

hide

        String host = null;
        String port = null;
        String exclList = null;
        Uri pacFileUrl = Uri.EMPTY;
        if (p != null) {
            host = p.getHost();
            port = Integer.toString(p.getPort());
            exclList = p.getExclusionListAsString();
            pacFileUrl = p.getPacFileUrl();
        }
        setHttpProxySystemProperty(host, port, exclList, pacFileUrl);
    
public static intvalidate(java.lang.String hostname, java.lang.String port, java.lang.String exclList)
Validate syntax of hostname, port and exclusion list entries {@hide}

        Matcher match = HOSTNAME_PATTERN.matcher(hostname);
        Matcher listMatch = EXCLLIST_PATTERN.matcher(exclList);

        if (!match.matches()) return PROXY_HOSTNAME_INVALID;

        if (!listMatch.matches()) return PROXY_EXCLLIST_INVALID;

        if (hostname.length() > 0 && port.length() == 0) return PROXY_PORT_EMPTY;

        if (port.length() > 0) {
            if (hostname.length() == 0) return PROXY_HOSTNAME_EMPTY;
            int portVal = -1;
            try {
                portVal = Integer.parseInt(port);
            } catch (NumberFormatException ex) {
                return PROXY_PORT_INVALID;
            }
            if (portVal <= 0 || portVal > 0xFFFF) return PROXY_PORT_INVALID;
        }
        return PROXY_VALID;