Java Host Name Get getProxy(String host, String proxyType)

Here you can find the source of getProxy(String host, String proxyType)

Description

get Proxy

License

Open Source License

Parameter

Parameter Description
host a parameter
proxyType a parameter

Return

proxy or null if it cannot be resolved

Declaration

public static Proxy getProxy(String host, String proxyType) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2012, 2014 Pivotal Software, Inc.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * and Apache License v2.0 which accompanies this distribution.
 *
 * The Eclipse Public License is available at
 *
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * and the Apache License v2.0 is available at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * You may elect to redistribute this code under either of these licenses.
 *
 *  Contributors:/*  ww w.  j a  v  a  2 s  .  c o m*/
 *     Pivotal Software, Inc. - initial API and implementation
 ********************************************************************************/

import java.net.Proxy;
import java.net.ProxySelector;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;

public class Main {
    /**
     * @param host
     * @param proxyType
     * @return proxy or null if it cannot be resolved
     */
    public static Proxy getProxy(String host, String proxyType) {
        Proxy foundProxy = null;
        try {

            URI uri = new URI(proxyType, "//" + host, null);
            List<Proxy> proxies = ProxySelector.getDefault().select(uri);

            if (proxies != null) {
                for (Proxy proxy : proxies) {
                    if (proxy != Proxy.NO_PROXY) {
                        foundProxy = proxy;
                        break;
                    }
                }
            }

        } catch (URISyntaxException e) {
            // No proxy
        }
        return foundProxy;
    }
}

Related

  1. getLocalCanonicalHostName()
  2. getLocalShortHostName()
  3. getLogHost()
  4. getMachineHostname()
  5. getMyHostname()
  6. getServerHost()
  7. getShortHostName()
  8. getShortHostName()
  9. getShortHostname()