//Proxy instance, proxy ip = 10.0.0.1 with port 8080 Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("10.0.0.1", 8080)); conn = new URL(urlString).openConnection(proxy);
System.setProperty("java.net.useSystemProxies", "true"); System.out.println("detecting proxies"); List l = null; try { l = ProxySelector.getDefault().select(new URI("http://foo/bar")); } catch (URISyntaxException e) { e.printStackTrace(); } if (l != null) { for (Iterator iter = l.iterator(); iter.hasNext();) { java.net.Proxy proxy = (java.net.Proxy) iter.next(); System.out.println("proxy type: " + proxy.type()); InetSocketAddress addr = (InetSocketAddress) proxy.address(); if (addr == null) { System.out.println("No Proxy"); } else { System.out.println("proxy hostname: " + addr.getHostName()); System.setProperty("http.proxyHost", addr.getHostName()); System.out.println("proxy port: " + addr.getPort()); System.setProperty("http.proxyPort", Integer.toString(addr.getPort())); } } }
private InetAddress getConnectToInetAddress(Proxy proxy, HttpUrl url) throws IOException { return proxy.type() != Proxy.Type.DIRECT ? ((InetSocketAddress) proxy.address()).getAddress() : InetAddress.getByName(url.host()); } }
@VisibleForTesting static String getProxySynthesis(URI uri, ProxySelector proxySelector) { List<Proxy> proxies = proxySelector.select(uri); if (proxies.size() == 1 && proxies.get(0).type().equals(Proxy.Type.DIRECT)) { return "no proxy"; } List<String> descriptions = Lists.newArrayList(); for (Proxy proxy : proxies) { if (proxy.type() != Proxy.Type.DIRECT) { descriptions.add(proxy.type() + " proxy: " + proxy.address()); } } return Joiner.on(", ").join(descriptions); }
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(); logger.warn("ProxySelector returned proxy type that we don't know how to use: " + proxy.type()); break;
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("10.10.100.100", 80)); HttpURLConnection connection =(HttpURLConnection)new URL("http://abc.abcd.com").openConnection(proxy); connection.setDoOutput(true); connection.setDoInput(true); connection.setRequestProperty("Content-type", "text/xml"); connection.setRequestProperty("Accept", "text/xml, application/xml"); connection.setRequestMethod("POST");
PreferencesDialog.saveValue(KEY_CONNECTION_URL_SERVER_LIST, serverUrl); URL serverListURL = new URL(serverUrl); if (p == null || !p.equals(Proxy.NO_PROXY)) { try { String address = PreferencesDialog.getCachedValue(PreferencesDialog.KEY_PROXY_ADDRESS, ""); Integer port = Integer.parseInt(PreferencesDialog.getCachedValue(PreferencesDialog.KEY_PROXY_PORT, "80")); p = new Proxy(type, new InetSocketAddress(address, port)); } catch (Exception ex) { throw new RuntimeException("Gui_DownloadPictures : error 1 - " + ex); in = new BufferedReader(new InputStreamReader(serverListURL.openConnection(p).getInputStream())); } catch (SocketTimeoutException | FileNotFoundException | UnknownHostException ex) { logger.warn("Could not read serverlist from: " + serverListURL.toString()); File f = new File("serverlist.txt"); if (f.exists() && !f.isDirectory()) {
URL proxyUrl = new URL(signatureConfig.getProxyUrl()); String host = proxyUrl.getHost(); int port = proxyUrl.getPort(); proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(InetAddress.getByName(host), (port == -1 ? 80 : port)));
@Test public void testUseProxyForSocketsSettingShouldntAffectHTTP() throws Exception { ProxyConfig config = new MyProxyConfig(false, Proxy.Type.HTTP, hostAndPort, creds); ProxyForURI proxy = new ProxyForURI(config); Field useProxyForSockets = proxy.getClass().getDeclaredField("useProxyForSockets"); useProxyForSockets.setAccessible(true); useProxyForSockets.setBoolean(proxy, false); URI uri = new URI("http://example.com/file"); assertEquals(proxy.apply(uri), new Proxy(Proxy.Type.HTTP, new InetSocketAddress("proxy.example.com", 8080))); }
this.url = MoreObjects.firstNonNull( url, new URL(MoreObjects.firstNonNull( (String) this.getSettings().get(SETTING_URL), DEFAULT_LIBRATO_API_URL))); this.proxyPort = proxyPort != null ? proxyPort : Settings.getIntegerSetting(getSettings(), SETTING_PROXY_PORT, null); if (this.proxyHost != null && this.proxyPort != null) { this.proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(this.proxyHost, this.proxyPort)); } else { this.proxy = null;
Socket socket = null; try { socket = new Socket(); socket.connect(proxy.address(), connTime); // 5 * 1000 OutputStream out = socket.getOutputStream(); out.write('\n'); out.flush(); conn = (HttpURLConnection) request.getUrl().openConnection(proxy); conn.setConnectTimeout(connTime); conn.setInstanceFollowRedirects(followRedirects); String host = url.getHost(); conn = (HttpURLConnection) url.openConnection(); if (conn instanceof HttpsURLConnection) { HttpsURLConnection httpsc = (HttpsURLConnection)conn;
public Socket createSocket(final String host, final int port, final InetAddress localAddress, final int localPort, final HttpConnectionParams params) throws IOException, UnknownHostException, ConnectTimeoutException { InetSocketAddress socksAddr = new InetSocketAddress(socksHost, socksPort); Proxy proxy = new Proxy(Proxy.Type.SOCKS, socksAddr); int timeout = params.getConnectionTimeout(); Socket socket = new Socket(proxy); socket.setSoTimeout(timeout); SocketAddress localaddr = new InetSocketAddress(localAddress, localPort); SocketAddress remoteaddr = new InetSocketAddress(host, port); socket.bind(localaddr); socket.connect(remoteaddr, timeout); return socket; }
private void connectInternal() throws IOException { int port = url.getPort(); int connectTimeout = getConnectTimeout(); if (port <= 0) { port = FTP_PORT; if (currentProxy == null || Proxy.Type.HTTP == currentProxy.type()) { controlSocket = new Socket(); } else { controlSocket = new Socket(currentProxy); InetSocketAddress addr = new InetSocketAddress(hostName, port); controlSocket.connect(addr, connectTimeout); connected = true; ctrlOutput = controlSocket.getOutputStream();
private static TTransport createRaw(HostAndPort address, Optional<SSLContext> sslContext, Optional<HostAndPort> socksProxy, int timeoutMillis) throws TTransportException { Proxy proxy = socksProxy .map(socksAddress -> new Proxy(SOCKS, InetSocketAddress.createUnresolved(socksAddress.getHost(), socksAddress.getPort()))) .orElse(Proxy.NO_PROXY); Socket socket = new Socket(proxy); try { socket.connect(new InetSocketAddress(address.getHost(), address.getPort()), timeoutMillis); socket.setSoTimeout(timeoutMillis); if (sslContext.isPresent()) { // SSL will connect to the SOCKS address when present HostAndPort sslConnectAddress = socksProxy.orElse(address); socket = sslContext.get().getSocketFactory().createSocket(socket, sslConnectAddress.getHost(), sslConnectAddress.getPort(), true); } return new TSocket(socket); } catch (Throwable t) { // something went wrong, close the socket and rethrow try { socket.close(); } catch (IOException e) { t.addSuppressed(e); } throw new TTransportException(t); } }
final Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(httpProxyUri.getHost(), httpProxyUri.getPort())); final ProxySelector proxySelector = new ProxySelector() { @Override if (!isNullOrEmpty(httpProxyUri.getUserInfo())) { final List<String> list = Splitter.on(":") .limit(2)
@Override public Permission getPermission() throws IOException { URL url = getURL(); String hostname = url.getHost(); int hostPort = url.getPort() != -1 ? url.getPort() : HttpUrl.defaultPort(url.getProtocol()); if (usingProxy()) { InetSocketAddress proxyAddress = (InetSocketAddress) client.proxy().address(); hostname = proxyAddress.getHostName(); hostPort = proxyAddress.getPort(); } return new SocketPermission(hostname + ":" + hostPort, "connect, resolve"); }
InetSocketAddress address = params != null && params.getProxy() != null ? (InetSocketAddress) params .getProxy().address() : new InetSocketAddress( InetAddress.getByName(uri.getHost()), getPort(uri)); bootstrap.connect(address).addListener(listener); response = future.get(); throw new HttpClientException("I/O error on " + request.getMethod().name() + " request for \"" + request.getURI().toString(), e); } catch (InterruptedException e) { throw new HttpClientException("Execute error on " + request.getMethod().name() + " request for \"" + request.getURI().toString(), e); } catch (ExecutionException e) { throw new HttpClientException("Execute error on "
public static Socket getSocket(String host, int port) { if (host == null || host.length() == 0) return new Socket(Proxy.NO_PROXY); else return new Socket(new Proxy(Proxy.Type.SOCKS, new InetSocketAddress(host, port))); } }
if (proxyHost != null && !matchNonProxy(url)) { final int proxyPort = settings.getInt(Settings.KEYS.PROXY_PORT); final SocketAddress address = new InetSocketAddress(proxyHost, proxyPort); final Proxy proxy = new Proxy(Proxy.Type.HTTP, address); conn = (HttpURLConnection) url.openConnection(proxy); } else { conn = (HttpURLConnection) url.openConnection();