use of java.net.Proxy in project jsoup by jhy.
the class UrlConnectTest method proxyGetAndSet.
@Test
public void proxyGetAndSet() throws IOException {
String url = "https://jsoup.org";
// invalid
Proxy proxy = new Proxy(Proxy.Type.HTTP, InetSocketAddress.createUnresolved("localhost", 8889));
final Connection con = Jsoup.connect(url).proxy(proxy);
assert con.request().proxy() == proxy;
// disable
con.request().proxy(null);
Document doc = con.get();
// would fail if actually went via proxy
assertTrue(doc.title().contains("jsoup"));
}
use of java.net.Proxy in project jsoup by jhy.
the class UrlConnectTest method fetchViaHttpProxy.
/*
Proxy tests. Assumes local proxy running on 8888, without system propery set (so that specifying it is required).
*/
@Test
public void fetchViaHttpProxy() throws IOException {
String url = "https://jsoup.org";
Proxy proxy = new Proxy(Proxy.Type.HTTP, InetSocketAddress.createUnresolved("localhost", 8888));
Document doc = Jsoup.connect(url).proxy(proxy).get();
assertTrue(doc.title().contains("jsoup"));
}
use of java.net.Proxy in project http-request by kevinsawicki.
the class HttpRequestTest method customConnectionFactory.
/**
* Verify custom connection factory
*/
@Test
public void customConnectionFactory() throws Exception {
handler = new RequestHandler() {
@Override
public void handle(Request request, HttpServletResponse response) {
response.setStatus(HTTP_OK);
}
};
ConnectionFactory factory = new ConnectionFactory() {
public HttpURLConnection create(URL otherUrl) throws IOException {
return (HttpURLConnection) new URL(url).openConnection();
}
public HttpURLConnection create(URL url, Proxy proxy) throws IOException {
throw new IOException();
}
};
HttpRequest.setConnectionFactory(factory);
int code = get("http://not/a/real/url").code();
assertEquals(200, code);
}
use of java.net.Proxy in project AndroidAsync by koush.
the class AsyncHttpClient method setupAndroidProxy.
@SuppressLint("NewApi")
private static void setupAndroidProxy(AsyncHttpRequest request) {
// using a explicit proxy?
if (request.proxyHost != null)
return;
List<Proxy> proxies;
try {
proxies = ProxySelector.getDefault().select(URI.create(request.getUri().toString()));
} catch (Exception e) {
// uri parsing craps itself sometimes.
return;
}
if (proxies.isEmpty())
return;
Proxy proxy = proxies.get(0);
if (proxy.type() != Proxy.Type.HTTP)
return;
if (!(proxy.address() instanceof InetSocketAddress))
return;
InetSocketAddress proxyAddress = (InetSocketAddress) proxy.address();
String proxyHost;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
proxyHost = proxyAddress.getHostString();
} else {
InetAddress address = proxyAddress.getAddress();
if (address != null)
proxyHost = address.getHostAddress();
else
proxyHost = proxyAddress.getHostName();
}
request.enableProxy(proxyHost, proxyAddress.getPort());
}
use of java.net.Proxy in project apjp by jvansteirteghem.
the class HTTPRequest method open.
public void open() throws HTTPRequestException {
try {
url = new URL(APJP.APJP_REMOTE_HTTP_SERVER_REQUEST_URL[i]);
Proxy proxy = Proxy.NO_PROXY;
if (url.getProtocol().equalsIgnoreCase("HTTP") == true) {
if (APJP.APJP_HTTP_PROXY_SERVER_ADDRESS.equalsIgnoreCase("") == false) {
proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(APJP.APJP_HTTP_PROXY_SERVER_ADDRESS, APJP.APJP_HTTP_PROXY_SERVER_PORT));
}
} else {
if (url.getProtocol().equalsIgnoreCase("HTTPS") == true) {
if (APJP.APJP_HTTPS_PROXY_SERVER_ADDRESS.equalsIgnoreCase("") == false) {
proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(APJP.APJP_HTTPS_PROXY_SERVER_ADDRESS, APJP.APJP_HTTPS_PROXY_SERVER_PORT));
}
}
}
urlConnection = url.openConnection(proxy);
if (urlConnection instanceof HttpsURLConnection) {
((HttpsURLConnection) urlConnection).setHostnameVerifier(new HostnameVerifier() {
public boolean verify(String hostname, SSLSession sslSession) {
String value1 = APJP.APJP_REMOTE_HTTP_SERVER_REQUEST_URL[i];
String[] values1 = value1.split("/", -1);
String value2 = values1[2];
String[] values2 = value2.split(":");
String value3 = values2[0];
if (value3.equalsIgnoreCase(hostname)) {
return true;
} else {
return false;
}
}
});
}
if (url.getProtocol().equalsIgnoreCase("HTTP") == true) {
if (APJP.APJP_HTTP_PROXY_SERVER_ADDRESS.equalsIgnoreCase("") == false && APJP.APJP_HTTP_PROXY_SERVER_USERNAME.equalsIgnoreCase("") == false) {
urlConnection.setRequestProperty("Proxy-Authorization", "Basic " + new String(BASE64.encode((APJP.APJP_HTTP_PROXY_SERVER_USERNAME + ":" + APJP.APJP_HTTP_PROXY_SERVER_PASSWORD).getBytes())));
}
} else {
if (url.getProtocol().equalsIgnoreCase("HTTPS") == true) {
if (APJP.APJP_HTTPS_PROXY_SERVER_ADDRESS.equalsIgnoreCase("") == false && APJP.APJP_HTTPS_PROXY_SERVER_USERNAME.equalsIgnoreCase("") == false) {
urlConnection.setRequestProperty("Proxy-Authorization", "Basic " + new String(BASE64.encode((APJP.APJP_HTTPS_PROXY_SERVER_USERNAME + ":" + APJP.APJP_HTTPS_PROXY_SERVER_PASSWORD).getBytes())));
}
}
}
for (int j = 0; j < APJP.APJP_REMOTE_HTTP_SERVER_REQUEST_PROPERTY_KEY[i].length; j = j + 1) {
if (APJP.APJP_REMOTE_HTTP_SERVER_REQUEST_PROPERTY_KEY[i][j].equalsIgnoreCase("") == false) {
urlConnection.setRequestProperty(APJP.APJP_REMOTE_HTTP_SERVER_REQUEST_PROPERTY_KEY[i][j], APJP.APJP_REMOTE_HTTP_SERVER_REQUEST_PROPERTY_VALUE[i][j]);
}
}
urlConnection.setDoOutput(true);
urlConnection.setDoInput(true);
urlConnection.connect();
} catch (Exception e) {
throw new HTTPRequestException("HTTP_REQUEST/OPEN", e);
}
}
Aggregations