Search in sources :

Example 36 with Proxy

use of java.net.Proxy in project XobotOS by xamarin.

the class ProxySelectorRoutePlanner method determineProxy.

/**
     * Determines a proxy for the given target.
     *
     * @param target    the planned target, never <code>null</code>
     * @param request   the request to be sent, never <code>null</code>
     * @param context   the context, or <code>null</code>
     *
     * @return  the proxy to use, or <code>null</code> for a direct route
     *
     * @throws HttpException
     *         in case of system proxy settings that cannot be handled
     */
protected HttpHost determineProxy(HttpHost target, HttpRequest request, HttpContext context) throws HttpException {
    // the proxy selector can be 'unset', so we better deal with null here
    ProxySelector psel = this.proxySelector;
    if (psel == null)
        psel = ProxySelector.getDefault();
    if (psel == null)
        return null;
    URI targetURI = null;
    try {
        targetURI = new URI(target.toURI());
    } catch (URISyntaxException usx) {
        throw new HttpException("Cannot convert host to URI: " + target, usx);
    }
    List<Proxy> proxies = psel.select(targetURI);
    Proxy p = chooseProxy(proxies, target, request, context);
    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;
}
Also used : ProxySelector(java.net.ProxySelector) Proxy(java.net.Proxy) HttpHost(org.apache.http.HttpHost) InetSocketAddress(java.net.InetSocketAddress) HttpException(org.apache.http.HttpException) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI)

Example 37 with Proxy

use of java.net.Proxy in project NoHttp by yanzhenjie.

the class URLConnectionNetworkExecutor method execute.

@Override
public Network execute(IBasicRequest request) throws Exception {
    URL url = new URL(request.url());
    HttpURLConnection connection;
    Proxy proxy = request.getProxy();
    if (proxy == null)
        connection = (HttpURLConnection) url.openConnection();
    else
        connection = (HttpURLConnection) url.openConnection(proxy);
    connection.setConnectTimeout(request.getConnectTimeout());
    connection.setReadTimeout(request.getReadTimeout());
    connection.setInstanceFollowRedirects(false);
    if (connection instanceof HttpsURLConnection) {
        SSLSocketFactory sslSocketFactory = request.getSSLSocketFactory();
        if (sslSocketFactory != null)
            ((HttpsURLConnection) connection).setSSLSocketFactory(sslSocketFactory);
        HostnameVerifier hostnameVerifier = request.getHostnameVerifier();
        if (hostnameVerifier != null)
            ((HttpsURLConnection) connection).setHostnameVerifier(hostnameVerifier);
    }
    // Base attribute
    connection.setRequestMethod(request.getRequestMethod().toString());
    connection.setDoInput(true);
    boolean isAllowBody = isAllowBody(request.getRequestMethod());
    connection.setDoOutput(isAllowBody);
    // Adds all request header to connection.
    Headers headers = request.headers();
    // To fix bug: accidental EOFException before API 19.
    List<String> values = headers.getValues(Headers.HEAD_KEY_CONNECTION);
    if (values == null || values.size() == 0)
        headers.set(Headers.HEAD_KEY_CONNECTION, Build.VERSION.SDK_INT > AndroidVersion.KITKAT ? Headers.HEAD_VALUE_CONNECTION_KEEP_ALIVE : Headers.HEAD_VALUE_CONNECTION_CLOSE);
    if (isAllowBody) {
        long contentLength = request.getContentLength();
        if (contentLength < Integer.MAX_VALUE)
            connection.setFixedLengthStreamingMode((int) contentLength);
        else if (Build.VERSION.SDK_INT >= AndroidVersion.KITKAT)
            try {
                Class<?> connectionClass = connection.getClass();
                Method setFixedLengthStreamingModeMethod = connectionClass.getMethod("setFixedLengthStreamingMode", long.class);
                setFixedLengthStreamingModeMethod.invoke(connection, contentLength);
            } catch (Throwable e) {
                connection.setChunkedStreamingMode(256 * 1024);
            }
        else
            connection.setChunkedStreamingMode(256 * 1024);
        headers.set(Headers.HEAD_KEY_CONTENT_LENGTH, Long.toString(contentLength));
    }
    Map<String, String> requestHeaders = headers.toRequestHeaders();
    for (Map.Entry<String, String> headerEntry : requestHeaders.entrySet()) {
        String headKey = headerEntry.getKey();
        String headValue = headerEntry.getValue();
        Logger.i(headKey + ": " + headValue);
        connection.setRequestProperty(headKey, headValue);
    }
    // 5. Connect
    connection.connect();
    return new URLConnectionNetwork(connection);
}
Also used : Method(java.lang.reflect.Method) URL(java.net.URL) HostnameVerifier(javax.net.ssl.HostnameVerifier) Proxy(java.net.Proxy) HttpURLConnection(java.net.HttpURLConnection) SSLSocketFactory(javax.net.ssl.SSLSocketFactory) Map(java.util.Map) HttpsURLConnection(javax.net.ssl.HttpsURLConnection)

Example 38 with Proxy

use of java.net.Proxy in project XobotOS by xamarin.

the class FtpURLConnection method connect.

/**
     * Establishes the connection to the resource specified by this
     * <code>URL</code>
     *
     * @see #connected
     * @see java.io.IOException
     * @see URLStreamHandler
     */
@Override
public void connect() throws IOException {
    // Use system-wide ProxySelect to select proxy list,
    // then try to connect via elements in the proxy list.
    List<Proxy> proxyList = null;
    if (proxy != null) {
        proxyList = new ArrayList<Proxy>(1);
        proxyList.add(proxy);
    } else {
        ProxySelector selector = ProxySelector.getDefault();
        if (selector != null) {
            proxyList = selector.select(uri);
        }
    }
    if (proxyList == null) {
        currentProxy = null;
        connectInternal();
    } else {
        ProxySelector selector = ProxySelector.getDefault();
        Iterator<Proxy> iter = proxyList.iterator();
        boolean connectOK = false;
        String failureReason = "";
        while (iter.hasNext() && !connectOK) {
            currentProxy = iter.next();
            try {
                connectInternal();
                connectOK = true;
            } catch (IOException ioe) {
                failureReason = ioe.getLocalizedMessage();
                // should be invoked.
                if (selector != null && Proxy.NO_PROXY != currentProxy) {
                    selector.connectFailed(uri, currentProxy.address(), ioe);
                }
            }
        }
        if (!connectOK) {
            throw new IOException("Unable to connect to server: " + failureReason);
        }
    }
}
Also used : ProxySelector(java.net.ProxySelector) Proxy(java.net.Proxy) IOException(java.io.IOException) InterruptedIOException(java.io.InterruptedIOException)

Example 39 with Proxy

use of java.net.Proxy in project android_frameworks_base by DirtyUnicorns.

the class PacProxySelector method parseResponse.

private static List<Proxy> parseResponse(String response) {
    String[] split = response.split(";");
    List<Proxy> ret = Lists.newArrayList();
    for (String s : split) {
        String trimmed = s.trim();
        if (trimmed.equals("DIRECT")) {
            ret.add(java.net.Proxy.NO_PROXY);
        } else if (trimmed.startsWith(PROXY)) {
            Proxy proxy = proxyFromHostPort(Type.HTTP, trimmed.substring(PROXY.length()));
            if (proxy != null) {
                ret.add(proxy);
            }
        } else if (trimmed.startsWith(SOCKS)) {
            Proxy proxy = proxyFromHostPort(Type.SOCKS, trimmed.substring(SOCKS.length()));
            if (proxy != null) {
                ret.add(proxy);
            }
        }
    }
    if (ret.size() == 0) {
        ret.add(java.net.Proxy.NO_PROXY);
    }
    return ret;
}
Also used : Proxy(java.net.Proxy)

Example 40 with Proxy

use of java.net.Proxy in project jdk8u_jdk by JetBrains.

the class SocksConnectTimeout method main.

public static void main(String[] args) {
    try {
        serverSocket = new ServerSocket(0);
        (new Thread() {

            @Override
            public void run() {
                serve();
            }
        }).start();
        Proxy socksProxy = new Proxy(Proxy.Type.SOCKS, new InetSocketAddress(InetAddress.getLocalHost(), serverSocket.getLocalPort()));
        test(socksProxy);
    } catch (IOException e) {
        unexpected(e);
    } finally {
        close(serverSocket);
        if (failed > 0)
            throw new RuntimeException("Test Failed: passed:" + passed + ", failed:" + failed);
    }
}
Also used : Proxy(java.net.Proxy) InetSocketAddress(java.net.InetSocketAddress) ServerSocket(java.net.ServerSocket) IOException(java.io.IOException)

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