Search in sources :

Example 81 with Proxy

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"));
}
Also used : Proxy(java.net.Proxy) Connection(org.jsoup.Connection) Document(org.jsoup.nodes.Document) Test(org.junit.Test)

Example 82 with Proxy

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"));
}
Also used : Proxy(java.net.Proxy) Document(org.jsoup.nodes.Document) Test(org.junit.Test)

Example 83 with Proxy

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);
}
Also used : ConnectionFactory(com.github.kevinsawicki.http.HttpRequest.ConnectionFactory) Proxy(java.net.Proxy) HttpURLConnection(java.net.HttpURLConnection) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) URL(java.net.URL) Test(org.junit.Test)

Example 84 with Proxy

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());
}
Also used : Proxy(java.net.Proxy) InetSocketAddress(java.net.InetSocketAddress) InetAddress(java.net.InetAddress) TimeoutException(java.util.concurrent.TimeoutException) AsyncSSLException(com.koushikdutta.async.AsyncSSLException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) SuppressLint(android.annotation.SuppressLint)

Example 85 with Proxy

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);
    }
}
Also used : Proxy(java.net.Proxy) InetSocketAddress(java.net.InetSocketAddress) SSLSession(javax.net.ssl.SSLSession) URL(java.net.URL) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) HostnameVerifier(javax.net.ssl.HostnameVerifier)

Aggregations

Proxy (java.net.Proxy)132 InetSocketAddress (java.net.InetSocketAddress)71 URL (java.net.URL)44 IOException (java.io.IOException)35 Test (org.junit.Test)22 URI (java.net.URI)21 ProxySelector (java.net.ProxySelector)20 HttpURLConnection (java.net.HttpURLConnection)19 SocketAddress (java.net.SocketAddress)19 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)13 URISyntaxException (java.net.URISyntaxException)11 ServerSocket (java.net.ServerSocket)10 Socket (java.net.Socket)9 PasswordAuthentication (java.net.PasswordAuthentication)8 InterruptedIOException (java.io.InterruptedIOException)6 URLConnection (java.net.URLConnection)6 SSLServerSocket (javax.net.ssl.SSLServerSocket)6 List (java.util.List)5 SSLSocket (javax.net.ssl.SSLSocket)5 OkHttpClient (okhttp3.OkHttpClient)5