當前位置: 首頁>>代碼示例>>Java>>正文


Java ProxySelector類代碼示例

本文整理匯總了Java中java.net.ProxySelector的典型用法代碼示例。如果您正苦於以下問題:Java ProxySelector類的具體用法?Java ProxySelector怎麽用?Java ProxySelector使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


ProxySelector類屬於java.net包,在下文中一共展示了ProxySelector類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: Address

import java.net.ProxySelector; //導入依賴的package包/類
public Address(String uriHost, int uriPort, Dns dns, SocketFactory socketFactory,
    SSLSocketFactory sslSocketFactory, HostnameVerifier hostnameVerifier,
    CertificatePinner certificatePinner, Authenticator proxyAuthenticator, Proxy proxy,
    List<Protocol> protocols, List<ConnectionSpec> connectionSpecs, ProxySelector proxySelector) {
  this.url = new HttpUrl.Builder()
      .scheme(sslSocketFactory != null ? "https" : "http")
      .host(uriHost)
      .port(uriPort)
      .build();

  if (dns == null) throw new NullPointerException("dns == null");
  this.dns = dns;

  if (socketFactory == null) throw new NullPointerException("socketFactory == null");
  this.socketFactory = socketFactory;

  if (proxyAuthenticator == null) {
    throw new NullPointerException("proxyAuthenticator == null");
  }
  this.proxyAuthenticator = proxyAuthenticator;

  if (protocols == null) throw new NullPointerException("protocols == null");
  this.protocols = Util.immutableList(protocols);

  if (connectionSpecs == null) throw new NullPointerException("connectionSpecs == null");
  this.connectionSpecs = Util.immutableList(connectionSpecs);

  if (proxySelector == null) throw new NullPointerException("proxySelector == null");
  this.proxySelector = proxySelector;

  this.proxy = proxy;
  this.sslSocketFactory = sslSocketFactory;
  this.hostnameVerifier = hostnameVerifier;
  this.certificatePinner = certificatePinner;
}
 
開發者ID:weiwenqiang,項目名稱:GitHub,代碼行數:36,代碼來源:Address.java

示例2: copyWithDefaults

import java.net.ProxySelector; //導入依賴的package包/類
OkHttpClient copyWithDefaults() {
    OkHttpClient result = new OkHttpClient(this);
    if (result.proxySelector == null) {
        result.proxySelector = ProxySelector.getDefault();
    }
    if (result.cookieHandler == null) {
        result.cookieHandler = CookieHandler.getDefault();
    }
    if (result.socketFactory == null) {
        result.socketFactory = SocketFactory.getDefault();
    }
    if (result.sslSocketFactory == null) {
        result.sslSocketFactory = getDefaultSSLSocketFactory();
    }
    if (result.hostnameVerifier == null) {
        result.hostnameVerifier = OkHostnameVerifier.INSTANCE;
    }
    if (result.certificatePinner == null) {
        result.certificatePinner = CertificatePinner.DEFAULT;
    }
    if (result.authenticator == null) {
        result.authenticator = AuthenticatorAdapter.INSTANCE;
    }
    if (result.connectionPool == null) {
        result.connectionPool = ConnectionPool.getDefault();
    }
    if (result.protocols == null) {
        result.protocols = DEFAULT_PROTOCOLS;
    }
    if (result.connectionSpecs == null) {
        result.connectionSpecs = DEFAULT_CONNECTION_SPECS;
    }
    if (result.dns == null) {
        result.dns = Dns.SYSTEM;
    }
    return result;
}
 
開發者ID:JackChan1999,項目名稱:boohee_v5.6,代碼行數:38,代碼來源:OkHttpClient.java

示例3: Address

import java.net.ProxySelector; //導入依賴的package包/類
public Address(String uriHost, int uriPort, Dns dns, SocketFactory socketFactory,
    @Nullable SSLSocketFactory sslSocketFactory, @Nullable HostnameVerifier hostnameVerifier,
    @Nullable CertificatePinner certificatePinner, Authenticator proxyAuthenticator,
    @Nullable Proxy proxy, List<Protocol> protocols, List<ConnectionSpec> connectionSpecs,
    ProxySelector proxySelector) {
  this.url = new HttpUrl.Builder()
      .scheme(sslSocketFactory != null ? "https" : "http")
      .host(uriHost)
      .port(uriPort)
      .build();

  if (dns == null) throw new NullPointerException("dns == null");
  this.dns = dns;

  if (socketFactory == null) throw new NullPointerException("socketFactory == null");
  this.socketFactory = socketFactory;

  if (proxyAuthenticator == null) {
    throw new NullPointerException("proxyAuthenticator == null");
  }
  this.proxyAuthenticator = proxyAuthenticator;

  if (protocols == null) throw new NullPointerException("protocols == null");
  this.protocols = Util.immutableList(protocols);

  if (connectionSpecs == null) throw new NullPointerException("connectionSpecs == null");
  this.connectionSpecs = Util.immutableList(connectionSpecs);

  if (proxySelector == null) throw new NullPointerException("proxySelector == null");
  this.proxySelector = proxySelector;

  this.proxy = proxy;
  this.sslSocketFactory = sslSocketFactory;
  this.hostnameVerifier = hostnameVerifier;
  this.certificatePinner = certificatePinner;
}
 
開發者ID:weiwenqiang,項目名稱:GitHub,代碼行數:37,代碼來源:Address.java

示例4: copyWithDefaults

import java.net.ProxySelector; //導入依賴的package包/類
/**
 * Returns a shallow copy of this OkHttpClient that uses the system-wide default for
 * each field that hasn't been explicitly configured.
 */
private OkHttpClient copyWithDefaults() {
  OkHttpClient result = new OkHttpClient(this);
  result.proxy = proxy;
  result.proxySelector = proxySelector != null ? proxySelector : ProxySelector.getDefault();
  result.cookieHandler = cookieHandler != null ? cookieHandler : CookieHandler.getDefault();
  result.responseCache = responseCache != null ? responseCache : ResponseCache.getDefault();
  result.sslSocketFactory = sslSocketFactory != null
      ? sslSocketFactory
      : HttpsURLConnection.getDefaultSSLSocketFactory();
  result.hostnameVerifier = hostnameVerifier != null
      ? hostnameVerifier
      : OkHostnameVerifier.INSTANCE;
  result.authenticator = authenticator != null
      ? authenticator
      : HttpAuthenticator.SYSTEM_DEFAULT;
  result.connectionPool = connectionPool != null ? connectionPool : ConnectionPool.getDefault();
  result.followProtocolRedirects = followProtocolRedirects;
  result.transports = transports != null ? transports : DEFAULT_TRANSPORTS;
  result.connectTimeout = connectTimeout;
  result.readTimeout = readTimeout;
  return result;
}
 
開發者ID:aabognah,項目名稱:LoRaWAN-Smart-Parking,代碼行數:27,代碼來源:OkHttpClient.java

示例5: retrieveDocument

import java.net.ProxySelector; //導入依賴的package包/類
public HashMap<String, InputStream> retrieveDocument(String baseAddress,
        String documentAddress) throws IOException,URISyntaxException{
    
    String effAddr = getEffectiveAddress(baseAddress, documentAddress);
    if(effAddr == null)
        return null;
    URI currURI = new URI(effAddr);
    HashMap<String, InputStream> result = null;
    
    InputStream is = getInputStreamOfURL(currURI.toURL(), ProxySelector.
            getDefault().select(currURI).get(0));
    result = new HashMap<String, InputStream>();
    result.put(effectiveURL.toString(), is);
    return result;
    
}
 
開發者ID:apache,項目名稱:incubator-netbeans,代碼行數:17,代碼來源:URLResourceRetriever.java

示例6: createEndpoint

import java.net.ProxySelector; //導入依賴的package包/類
private Endpoint createEndpoint() throws IOException {
    URL realUrl = getUrl();
    try {
        if ("https".equals(realUrl.getProtocol())) { // NOI18N
            SSLContext context = ContextProvider.getInstance().getSSLContext(instance);
            return Endpoint.forSocket(context.getSocketFactory().createSocket(realUrl.getHost(), realUrl.getPort()));
        } else if ("http".equals(realUrl.getProtocol())) { // NOI18N
            Socket s = new Socket(ProxySelector.getDefault().select(realUrl.toURI()).get(0));
            int port = realUrl.getPort();
            if (port < 0) {
                port = realUrl.getDefaultPort();
            }
            s.connect(new InetSocketAddress(realUrl.getHost(), port));
            return Endpoint.forSocket(s);
        } else {
            throw new IOException("Unknown protocol: " + realUrl.getProtocol());
        }
    } catch (URISyntaxException ex) {
        throw new IOException(ex);
    }
}
 
開發者ID:apache,項目名稱:incubator-netbeans,代碼行數:22,代碼來源:DockerAction.java

示例7: setUp

import java.net.ProxySelector; //導入依賴的package包/類
@Override
protected void setUp() throws Exception {
    super.setUp();
    System.setProperty("netbeans.user", getWorkDir().getAbsolutePath());
    
    // reset
    Method m = MylynSupport.class.getDeclaredMethod("reset", new Class[0]);
    m.setAccessible(true);
    m.invoke(MylynSupport.class);
            
    Field f = Bugzilla.class.getDeclaredField("instance");
    f.setAccessible(true);
    f.set(Bugzilla.class, null);
    
    brc = Bugzilla.getInstance().getRepositoryConnector();
    
    WebUtil.init();
    
    if (defaultPS == null) {
        defaultPS = ProxySelector.getDefault();
    }
}
 
開發者ID:apache,項目名稱:incubator-netbeans,代碼行數:23,代碼來源:ExceptionHandlerTest.java

示例8: testIgnoring

import java.net.ProxySelector; //導入依賴的package包/類
@Test // https://github.com/danikula/AndroidVideoCache/issues/28
public void testIgnoring() throws Exception {
    InetSocketAddress proxyAddress = new InetSocketAddress("proxy.com", 80);
    Proxy systemProxy = new Proxy(Proxy.Type.HTTP, proxyAddress);
    ProxySelector mockedProxySelector = Mockito.mock(ProxySelector.class);
    when(mockedProxySelector.select(Mockito.<URI>any())).thenReturn(Lists.newArrayList(systemProxy));
    ProxySelector.setDefault(mockedProxySelector);

    IgnoreHostProxySelector.install("localhost", 42);

    ProxySelector proxySelector = ProxySelector.getDefault();
    List<Proxy> githubProxies = proxySelector.select(new URI("http://github.com"));
    assertThat(githubProxies).hasSize(1);
    assertThat(githubProxies.get(0).address()).isEqualTo(proxyAddress);

    List<Proxy> localhostProxies = proxySelector.select(new URI("http://localhost:42"));
    assertThat(localhostProxies).hasSize(1);
    assertThat(localhostProxies.get(0)).isEqualTo(Proxy.NO_PROXY);

    List<Proxy> localhostPort69Proxies = proxySelector.select(new URI("http://localhost:69"));
    assertThat(localhostPort69Proxies).hasSize(1);
    assertThat(localhostPort69Proxies.get(0).address()).isEqualTo(proxyAddress);
}
 
開發者ID:Achenglove,項目名稱:AndroidVideoCache,代碼行數:24,代碼來源:ProxySelectorTest.java

示例9: chooseProxy

import java.net.ProxySelector; //導入依賴的package包/類
private Proxy chooseProxy() {
    ProxySelector sel =
        java.security.AccessController.doPrivileged(
            new java.security.PrivilegedAction<ProxySelector>() {
                @Override
                public ProxySelector run() {
                    return ProxySelector.getDefault();
                }
            });

    if(sel==null)
        return Proxy.NO_PROXY;


    if(!sel.getClass().getName().equals("sun.net.spi.DefaultProxySelector"))
        // user-defined proxy. may return a different proxy for each invocation
        return null;

    Iterator<Proxy> it = sel.select(uri).iterator();
    if(it.hasNext())
        return it.next();

    return Proxy.NO_PROXY;
}
 
開發者ID:SunburstApps,項目名稱:OpenJSharp,代碼行數:25,代碼來源:EndpointAddress.java

示例10: main

import java.net.ProxySelector; //導入依賴的package包/類
public static void main(String[] args) throws Exception {
    System.setProperty("java.net.useSystemProxies", "true");
    final ProxySelector ps = ProxySelector.getDefault();
    final URI uri = new URI("http://ubuntu.com");
    Thread[] threads = new Thread[NUM_THREADS];

    for (int i = 0; i < NUM_THREADS; i++) {
        threads[i] = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    ps.select(uri);
                } catch (Exception x) {
                    throw new RuntimeException(x);
                }
            }
        });
    }
    for (int i = 0; i < NUM_THREADS; i++) {
        threads[i].start();
    }
    for (int i = 0; i < NUM_THREADS; i++) {
        threads[i].join();
    }
}
 
開發者ID:lambdalab-mirror,項目名稱:jdk8u-jdk,代碼行數:26,代碼來源:MultiThreadedSystemProxies.java

示例11: getProxy

import java.net.ProxySelector; //導入依賴的package包/類
private static Proxy getProxy() {
	List<Proxy> l = null;
	try {
		final ProxySelector def = ProxySelector.getDefault();
		l = def.select(new URI("http://foo/bar"));
		ProxySelector.setDefault(null);
	} catch (final Exception e) {

	}
	if (l != null) {
		for (final Iterator<Proxy> iter = l.iterator(); iter.hasNext();) {
			final java.net.Proxy proxy = iter.next();
			return proxy;
		}
	}
	return null;
}
 
開發者ID:leolewis,項目名稱:openvisualtraceroute,代碼行數:18,代碼來源:Env.java

示例12: initDefaultBuilder

import java.net.ProxySelector; //導入依賴的package包/類
/**
 * Initializes default http client builder instance
 * 
 * @return
 */
protected HttpClientBuilder initDefaultBuilder() {
	HttpClientBuilder builder = HttpClientBuilder.create();

	if (null != credentials) {
		CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
		credentialsProvider.setCredentials(AuthScope.ANY, credentials);
		builder.setDefaultCredentialsProvider(credentialsProvider);
	}

	builder.setMaxConnPerRoute(5);
	builder.setMaxConnTotal(20);
	// dirty hack to avoid npe in soapui client, soapui client sets default
	// proxy selector to null
	ProxySelector proxySelector = ProxySelector.getDefault();
	if (proxySelector != null)
		builder.setRoutePlanner(new SystemDefaultRoutePlanner(proxySelector));

	if (null != interceptors && !interceptors.isEmpty()) {
		for (HttpRequestInterceptor interceptor : interceptors) {
			builder.addInterceptorFirst(interceptor);
		}
	}

	return builder;
}
 
開發者ID:reportportal,項目名稱:client-java-rest-core,代碼行數:31,代碼來源:AuthClientFactory.java

示例13: getProxySelector

import java.net.ProxySelector; //導入依賴的package包/類
/*************************************************************************
 * Loads the proxy settings from environment variables.
 * 
 * @return a configured ProxySelector, null if none is found.
 ************************************************************************/

@Override
public ProxySelector getProxySelector() {
  ProtocolDispatchSelector ps = new ProtocolDispatchSelector();

  Logger.log(getClass(), LogLevel.TRACE, "Using settings from Java System Properties");

  setupProxyForProtocol(ps, "http", 80);
  setupProxyForProtocol(ps, "https", 443);
  setupProxyForProtocol(ps, "ftp", 80);
  setupProxyForProtocol(ps, "ftps", 80);
  boolean socksAvailable = setupSocktProxy(ps);

  if (ps.size() == 0 && !socksAvailable) {
    return null;
  }

  return ps;
}
 
開發者ID:MarkusBernhardt,項目名稱:proxy-vole,代碼行數:25,代碼來源:JavaProxySearchStrategy.java

示例14: setupProxyForProtocol

import java.net.ProxySelector; //導入依賴的package包/類
/*************************************************************************
 * Parse properties for the given protocol.
 * 
 * @param ps
 * @param protocol
 * @throws NumberFormatException
 ************************************************************************/

private void setupProxyForProtocol(ProtocolDispatchSelector ps, String protocol, int defaultPort) {
  String host = System.getProperty(protocol + ".proxyHost");
  if (host == null || host.trim().length() == 0) {
    return;
  }

  String port = System.getProperty(protocol + ".proxyPort", Integer.toString(defaultPort));
  String whiteList = System.getProperty(protocol + ".nonProxyHosts", "").replace('|', ',');

  if ("https".equalsIgnoreCase(protocol)) { // This is dirty but https has
                                            // no own property for it.
    whiteList = System.getProperty("http.nonProxyHosts", "").replace('|', ',');
  }

  Logger.log(getClass(), LogLevel.TRACE, protocol.toUpperCase() + " proxy {0}:{1} found using whitelist: {2}", host,
      port, whiteList);

  ProxySelector protocolSelector = new FixedProxySelector(host, Integer.parseInt(port));
  if (whiteList.trim().length() > 0) {
    protocolSelector = new ProxyBypassListSelector(whiteList, protocolSelector);
  }

  ps.setSelector(protocol, protocolSelector);
}
 
開發者ID:MarkusBernhardt,項目名稱:proxy-vole,代碼行數:33,代碼來源:JavaProxySearchStrategy.java

示例15: getFirstProxy

import java.net.ProxySelector; //導入依賴的package包/類
static Proxy getFirstProxy(URL url) throws URISyntaxException {
	System.setProperty("java.net.useSystemProxies", "true");

	List<Proxy> proxylist = ProxySelector.getDefault().select(url.toURI());


	if (proxylist != null) {
		for (Proxy proxy: proxylist) {
			SocketAddress addr = proxy.address();

			if (addr != null) {
				return proxy;
			}
		}

	}
	return null;
}
 
開發者ID:MaxSmile,項目名稱:EasyVPN-Free,代碼行數:19,代碼來源:ProxyDetection.java


注:本文中的java.net.ProxySelector類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。