use of java.net.Proxy in project nutz by nutzam.
the class Http method setHttpProxy.
public static void setHttpProxy(String host, int port) {
final Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(host, port));
proxySwitcher = new ProxySwitcher() {
public Proxy getProxy(URL url) {
return proxy;
}
public Proxy getProxy(Request req) {
if ("close".equals(req.getHeader().get("NoProxy")))
return null;
String url = req.getUrl().toString();
if (url.startsWith("http") && url.contains("://") && url.length() > "https://".length()) {
url = url.substring(url.indexOf("://") + "://".length());
if (url.startsWith("127.0.0") || url.startsWith("localhost"))
return null;
}
req.getHeader().set("Connection", "close");
return getProxy(req.getUrl());
}
};
}
use of java.net.Proxy in project nutz by nutzam.
the class Sender method openConnection.
protected void openConnection() throws IOException {
if (this.interceptor != null)
this.interceptor.beforeConnect(request);
ProxySwitcher proxySwitcher = Http.proxySwitcher;
if (proxySwitcher != null) {
try {
Proxy proxy = proxySwitcher.getProxy(request);
if (proxy != null) {
if (Http.autoSwitch) {
Socket socket = null;
try {
socket = new Socket();
socket.connect(proxy.address(), 5 * 1000);
OutputStream out = socket.getOutputStream();
out.write('\n');
out.flush();
} finally {
if (socket != null)
socket.close();
}
}
log.debug("connect via proxy : " + proxy + " for " + request.getUrl());
conn = (HttpURLConnection) request.getUrl().openConnection(proxy);
conn.setConnectTimeout(Default_Conn_Timeout);
conn.setInstanceFollowRedirects(followRedirects);
if (timeout > 0)
conn.setReadTimeout(timeout);
else
conn.setReadTimeout(Default_Read_Timeout);
return;
}
} catch (IOException e) {
if (!Http.autoSwitch) {
throw e;
}
log.info("Test proxy FAIl, fallback to direct connection", e);
}
}
URL url = request.getUrl();
String host = url.getHost();
conn = (HttpURLConnection) url.openConnection();
if (conn instanceof HttpsURLConnection) {
if (sslSocketFactory != null)
((HttpsURLConnection) conn).setSSLSocketFactory(sslSocketFactory);
else if (Http.sslSocketFactory != null)
((HttpsURLConnection) conn).setSSLSocketFactory(Http.sslSocketFactory);
}
if (!Lang.isIPv4Address(host)) {
if (url.getPort() > 0 && url.getPort() != 80)
host += ":" + url.getPort();
conn.addRequestProperty("Host", host);
}
conn.setConnectTimeout(Default_Conn_Timeout);
conn.setRequestMethod(request.getMethod().name());
if (timeout > 0)
conn.setReadTimeout(timeout);
else
conn.setReadTimeout(Default_Read_Timeout);
conn.setInstanceFollowRedirects(followRedirects);
if (interceptor != null)
this.interceptor.afterConnect(request, conn);
}
use of java.net.Proxy in project nutz by nutzam.
the class Http method setSocketProxy.
public static void setSocketProxy(String host, int port) {
final Proxy proxy = new Proxy(Proxy.Type.SOCKS, new InetSocketAddress(host, port));
proxySwitcher = new ProxySwitcher() {
public Proxy getProxy(URL url) {
return proxy;
}
public Proxy getProxy(Request req) {
if ("close".equals(req.getHeader().get("NoProxy")))
return null;
String url = req.getUrl().toString();
if (url.startsWith("http") && url.contains("://") && url.length() > "https://".length()) {
url = url.substring(url.indexOf("://"));
if (url.startsWith("127.0.0") || url.startsWith("localhost"))
return null;
}
req.getHeader().set("Connection", "close");
return getProxy(req.getUrl());
}
};
}
use of java.net.Proxy in project hadoop by apache.
the class TestSocketFactory method testProxy.
/**
* test proxy methods
*/
@Test(timeout = 5000)
public void testProxy() throws Exception {
SocksSocketFactory templateWithoutProxy = new SocksSocketFactory();
Proxy proxy = new Proxy(Type.SOCKS, InetSocketAddress.createUnresolved("localhost", 0));
SocksSocketFactory templateWithProxy = new SocksSocketFactory(proxy);
assertFalse(templateWithoutProxy.equals(templateWithProxy));
Configuration configuration = new Configuration();
configuration.set("hadoop.socks.server", "localhost:0");
templateWithoutProxy.setConf(configuration);
assertTrue(templateWithoutProxy.equals(templateWithProxy));
}
use of java.net.Proxy in project platformlayer by platformlayer.
the class ITAptCacheService method testProxy.
private String testProxy(InetSocketAddress proxySocketAddress, String fetchUrl) throws IOException {
Proxy proxy = new Proxy(Proxy.Type.HTTP, proxySocketAddress);
URL url = new URL(fetchUrl);
HttpURLConnection uc = (HttpURLConnection) url.openConnection(proxy);
uc.connect();
InputStream is = uc.getInputStream();
String html = IoUtils.readAll(is);
is.close();
System.out.println(html);
return html;
}
Aggregations