Refine search
try { System.setProperty("java.net.useSystemProxies","true"); List<Proxy> l = ProxySelector.getDefault().select( new URI("http://www.yahoo.com/")); System.out.println("proxy hostname : " + proxy.type()); InetSocketAddress addr = (InetSocketAddress)proxy.address(); System.out.println("proxy hostname : " + addr.getHostName()); System.out.println("proxy port : " + addr.getPort());
/** * Clients should invoke this method when they encounter a connectivity failure on a connection * returned by this route selector. */ public void connectFailed(Route failedRoute, IOException failure) { if (failedRoute.proxy().type() != Proxy.Type.DIRECT && address.proxySelector() != null) { // Tell the proxy selector when we fail to connect on a fresh connection. address.proxySelector().connectFailed( address.url().uri(), failedRoute.proxy().address(), failure); } routeDatabase.failed(failedRoute); }
static void install(String hostToIgnore, int portToIgnore) { ProxySelector defaultProxySelector = ProxySelector.getDefault(); ProxySelector ignoreHostProxySelector = new IgnoreHostProxySelector(defaultProxySelector, hostToIgnore, portToIgnore); ProxySelector.setDefault(ignoreHostProxySelector); }
private List<Proxy> getDefaultProxies() { try { ProxySelector defaultProxySelector = ProxySelector.getDefault(); return defaultProxySelector.select(new URI(getPingUrl())); } catch (URISyntaxException e) { throw new IllegalStateException(e); } }
@Override protected HttpHost determineProxy( final HttpHost target, final HttpRequest request, final HttpContext context) throws HttpException { final URI targetURI; try { targetURI = new URI(target.toURI()); } catch (final URISyntaxException ex) { throw new HttpException("Cannot convert host to URI: " + target, ex); } final List<Proxy> proxies = this.proxySelector.select(targetURI); final Proxy p = chooseProxy(proxies); HttpHost result = null; if (p.type() == Proxy.Type.HTTP) { // convert the socket address to an HttpHost if (!(p.address() instanceof InetSocketAddress)) { throw new HttpException("Unable to handle non-Inet proxy address: " + p.address()); } final InetSocketAddress isa = (InetSocketAddress) p.address(); // assume default scheme (http) result = new HttpHost(getHost(isa), isa.getPort()); } return result; }
@Override public List<Proxy> select(URI uri) { String uristr = uri.toString(); try{ URI u = new URI("socket" + uristr.substring(uristr.indexOf(':'))); // logger.info("detectecting socks proxy for " + u // + " using " + parent.getClass().getName()); return parent.select(u); } catch(URISyntaxException e){ logger.info("return direct connection."); return ProxyUtil.DIRECT; } }
@Test public void shouldGetDirectProxySynthesis() throws URISyntaxException { ProxySelector proxySelector = mock(ProxySelector.class); when(proxySelector.select(any(URI.class))).thenReturn(Arrays.asList(Proxy.NO_PROXY)); assertThat(DefaultHttpDownloader.BaseHttpDownloader.getProxySynthesis(new URI("http://an_url"), proxySelector)).isEqualTo("no proxy"); }
@Override public List<Proxy> select(URI uri) { String url = WWW_PATERN.matcher(uri.toString()).replaceAll(""); if (uri.getScheme().startsWith("http") || (uri.getPort() != 80 && uri.getPort() != 443)) { actionManager.checkBlockingAction("Socket: " + url); } return oldProxySelector == null ? Collections.singletonList(Proxy.NO_PROXY) : oldProxySelector.select(uri); }
public VulasProxySelector() { // Remember current default (fallback solution) this.def = ProxySelector.getDefault(); // Read configuration this.cfg = VulasConfiguration.getGlobal().getConfiguration(); // No proxy for the following hosts noProxyHosts.addAll(this.cfg.getString("http.nonProxyHosts", ""), "\\|", true); //String[] no_proxy_hosts = this.cfg.getString("http.nonProxyHosts", "").split("\\|"); //for(int i=0; i<no_proxy_hosts.length; i++) this.noProxyHosts.add(no_proxy_hosts[i]); // Create HTTP proxy if( this.cfg.getString("http.proxyHost")!=null && !this.cfg.getString("http.proxyHost").equals("") && this.cfg.getString("http.proxyPort")!=null && !this.cfg.getString("http.proxyPort").equals("") ) { this.httpProxy = new Proxy( Proxy.Type.HTTP, new InetSocketAddress( this.cfg.getString("http.proxyHost"), this.cfg.getInt("http.proxyPort") ) ); VulasProxySelector.log.info("Proxy selector configuration: [" + this.cfg.getString("http.proxyHost") + ":" + this.cfg.getInt("http.proxyPort") + ", non proxy hosts: "+this.noProxyHosts.toString()+"]"); } else { VulasProxySelector.log.info("Proxy selector configuration: None"); } }
ProxySearch proxySearch = new ProxySearch(); proxySearch.addStrategy(Strategy.OS_DEFAULT); proxySearch.addStrategy(Strategy.JAVA); proxySearch.addStrategy(Strategy.BROWSER); ProxySelector proxySelector = proxySearch.getProxySelector(); ProxySelector.setDefault(proxySelector); URI home = URI.create("http://www.google.com"); System.out.println("ProxySelector: " + proxySelector); System.out.println("URI: " + home); List<Proxy> proxyList = proxySelector.select(home); if (proxyList != null && !proxyList.isEmpty()) { for (Proxy proxy : proxyList) { System.out.println(proxy); SocketAddress address = proxy.address(); if (address instanceof InetSocketAddress) { String host = ((InetSocketAddress) address).getHostName(); String port = Integer.toString(((InetSocketAddress) address).getPort()); System.setProperty("http.proxyHost", host); System.setProperty("http.proxyPort", port); } } }
public static HostAndPort getSystemSocksProxy() { URI uri = URI.create("socket://0.0.0.0:80"); for (Proxy proxy : ProxySelector.getDefault().select(uri)) { if (proxy.type() == Proxy.Type.SOCKS && proxy.address() instanceof InetSocketAddress) { InetSocketAddress address = (InetSocketAddress) proxy.address(); return HostAndPort.fromParts(address.getHostString(), address.getPort()); } } return null; }
@Override public ProxyData get(InetSocketAddress remoteAddress) { try { List<Proxy> proxies = ProxySelector.getDefault() .select(new URI(Proxy.Type.SOCKS.name(), "//" + remoteAddress.getHostString(), null)); //$NON-NLS-1$ ProxyData data = getData(proxies, Proxy.Type.SOCKS); if (data == null) { proxies = ProxySelector.getDefault() .select(new URI(Proxy.Type.HTTP.name(), "//" + remoteAddress.getHostString(), //$NON-NLS-1$ null)); data = getData(proxies, Proxy.Type.HTTP); } return data; } catch (URISyntaxException e) { return null; } }
/** * Get the first available {@link Proxy} using the {@link ProxySelector} implementation * * @return the first {@link Type#HTTP} proxy available, or {@link Proxy#NO_PROXY} if no * proxy is available */ private Proxy chooseProxy() { try { for (Proxy proxy : ProxySelector.getDefault().select(new URI(server.getHost()))) { if (proxy.type() == Type.HTTP) { return proxy; } } } catch (URISyntaxException e) { // Failed to parse the server host URI // Fallback to the default NO_PROXY log.error("Failed to parse the host URI", e); } return Proxy.NO_PROXY; }
ProxySelector.setDefault(new ProxySelector() { final ProxySelector delegate = ProxySelector.getDefault(); @Override public List<Proxy> select(URI uri) { // Filter the URIs to be proxied if (uri.toString().contains("github") && uri.toString().contains("https")) { return Arrays.asList(new Proxy(Type.HTTP, InetSocketAddress .createUnresolved("localhost", 3128))); } if (uri.toString().contains("github") && uri.toString().contains("http")) { return Arrays.asList(new Proxy(Type.HTTP, InetSocketAddress .createUnresolved("localhost", 3129))); } // revert to the default behaviour return delegate == null ? Arrays.asList(Proxy.NO_PROXY) : delegate.select(uri); } @Override public void connectFailed(URI uri, SocketAddress sa, IOException ioe) { if (uri == null || sa == null || ioe == null) { throw new IllegalArgumentException( "Arguments can't be null."); } } });
Loggers.get(getClass()).debug("Download: " + uri + " (" + getProxySynthesis(uri, ProxySelector.getDefault()) + ")"); HttpURLConnection connection = (HttpURLConnection) uri.toURL().openConnection(); connection.setRequestMethod(requestMethod); HttpsTrust.INSTANCE.trust(connection);
ProxySelector selector = ProxySelector.getDefault(); StringBuilder result = new StringBuilder(); for (Proxy proxy : selector.select(destURI)) { SocketAddress address = proxy.address(); if (address == null) { result.append("Direct connection\n"); InetSocketAddress ina = (InetSocketAddress) address; result.append(' '); result.append(ina.getHostName()); result.append(':'); result.append(ina.getPort()); if (ina.isUnresolved()) { result.append(" [unresolved]"); } else {
public static InputStream inputStreamTryingProxies(final URI source) throws Exception { final URL url = source.toURL(); for (final Proxy proxy : ProxySelector.getDefault().select(source)) { // try to connect try { final URLConnection urlConnection = url.openConnection(proxy); urlConnection.setConnectTimeout(CONNECT_TIMEOUT); return new BufferedInputStream(urlConnection.getInputStream()); } catch (final IOException e) { // ignored } } return null; }
import java.net.Proxy; import java.net.ProxySelector; import java.net.URI; import java.util.List; public class Play { public static void main(String args[]) { System.out.println(System.getProperty("java.home")); System.out.println(System.getProperty("http.proxyHost")); ProxySelector ps = ProxySelector.getDefault(); List<Proxy> proxies = ps.select(URI.create("http://www.yahoo.com")); System.out.println("HTTP Proxies"); for (Proxy p:proxies) { System.out.append(p.toString()).append("\n"); } } }
URI javaUri = uri.toJavaNetURI(); List<Proxy> proxies = proxySelector.select(javaUri); if (proxies != null) { switch (proxy.type()) { case HTTP: if (!(proxy.address() instanceof InetSocketAddress)) { logger.warn("Don't know how to connect to address " + proxy.address()); return null; } else { InetSocketAddress address = (InetSocketAddress) proxy.address(); return proxyServer(address.getHostName(), address.getPort()).build();