Search in sources :

Example 21 with Proxy

use of java.net.Proxy in project spring-framework by spring-projects.

the class ProxyFactoryBeanTests method normal.

@Test
public void normal() {
    Proxy.Type type = Proxy.Type.HTTP;
    factoryBean.setType(type);
    String hostname = "example.com";
    factoryBean.setHostname(hostname);
    int port = 8080;
    factoryBean.setPort(port);
    factoryBean.afterPropertiesSet();
    Proxy result = factoryBean.getObject();
    assertEquals(type, result.type());
    InetSocketAddress address = (InetSocketAddress) result.address();
    assertEquals(hostname, address.getHostName());
    assertEquals(port, address.getPort());
}
Also used : Proxy(java.net.Proxy) InetSocketAddress(java.net.InetSocketAddress) Test(org.junit.Test)

Example 22 with Proxy

use of java.net.Proxy in project platform_frameworks_base by android.

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 23 with Proxy

use of java.net.Proxy in project androidquery by androidquery.

the class AbstractAjaxCallback method httpMulti.

private void httpMulti(String url, Map<String, Object> params, AjaxStatus status) throws IOException {
    AQUtility.debug("multipart", url);
    HttpURLConnection conn = null;
    DataOutputStream dos = null;
    URL u = new URL(url);
    Proxy py = null;
    if (proxy != null) {
        AQUtility.debug("proxy", proxy);
        py = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxy.getHostName(), proxy.getPort()));
    } else if (proxyHandle != null) {
        py = proxyHandle.makeProxy(this);
    }
    if (py == null) {
        conn = (HttpURLConnection) u.openConnection();
    } else {
        conn = (HttpURLConnection) u.openConnection(py);
    }
    conn.setInstanceFollowRedirects(false);
    conn.setConnectTimeout(NET_TIMEOUT * 4);
    conn.setDoInput(true);
    conn.setDoOutput(true);
    conn.setUseCaches(false);
    conn.setRequestMethod("POST");
    conn.setRequestProperty("Connection", "Keep-Alive");
    conn.setRequestProperty("Content-Type", "multipart/form-data;charset=utf-8;boundary=" + boundary);
    if (headers != null) {
        for (String name : headers.keySet()) {
            conn.setRequestProperty(name, headers.get(name));
        }
    }
    String cookie = makeCookie();
    if (cookie != null) {
        conn.setRequestProperty("Cookie", cookie);
    }
    if (ah != null) {
        ah.applyToken(this, conn);
    }
    dos = new DataOutputStream(conn.getOutputStream());
    for (Map.Entry<String, Object> entry : params.entrySet()) {
        writeObject(dos, entry.getKey(), entry.getValue());
    }
    dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
    dos.flush();
    dos.close();
    conn.connect();
    int code = conn.getResponseCode();
    String message = conn.getResponseMessage();
    byte[] data = null;
    String encoding = conn.getContentEncoding();
    String error = null;
    if (code < 200 || code >= 300) {
        error = new String(toData(encoding, conn.getErrorStream()), "UTF-8");
        AQUtility.debug("error", error);
    } else {
        data = toData(encoding, conn.getInputStream());
    }
    AQUtility.debug("response", code);
    if (data != null) {
        AQUtility.debug(data.length, url);
    }
    status.code(code).message(message).redirect(url).time(new Date()).data(data).error(error).client(null);
}
Also used : Proxy(java.net.Proxy) HttpURLConnection(java.net.HttpURLConnection) DataOutputStream(java.io.DataOutputStream) InetSocketAddress(java.net.InetSocketAddress) JSONObject(org.json.JSONObject) Map(java.util.Map) HashMap(java.util.HashMap) URL(java.net.URL) Date(java.util.Date)

Example 24 with Proxy

use of java.net.Proxy in project okhttp by square.

the class RetryAndFollowUpInterceptor method followUpRequest.

/**
   * Figures out the HTTP request to make in response to receiving {@code userResponse}. This will
   * either add authentication headers, follow redirects or handle a client request timeout. If a
   * follow-up is either unnecessary or not applicable, this returns null.
   */
private Request followUpRequest(Response userResponse) throws IOException {
    if (userResponse == null)
        throw new IllegalStateException();
    Connection connection = streamAllocation.connection();
    Route route = connection != null ? connection.route() : null;
    int responseCode = userResponse.code();
    final String method = userResponse.request().method();
    switch(responseCode) {
        case HTTP_PROXY_AUTH:
            Proxy selectedProxy = route != null ? route.proxy() : client.proxy();
            if (selectedProxy.type() != Proxy.Type.HTTP) {
                throw new ProtocolException("Received HTTP_PROXY_AUTH (407) code while not using proxy");
            }
            return client.proxyAuthenticator().authenticate(route, userResponse);
        case HTTP_UNAUTHORIZED:
            return client.authenticator().authenticate(route, userResponse);
        case HTTP_PERM_REDIRECT:
        case HTTP_TEMP_REDIRECT:
            // or HEAD, the user agent MUST NOT automatically redirect the request"
            if (!method.equals("GET") && !method.equals("HEAD")) {
                return null;
            }
        // fall-through
        case HTTP_MULT_CHOICE:
        case HTTP_MOVED_PERM:
        case HTTP_MOVED_TEMP:
        case HTTP_SEE_OTHER:
            // Does the client allow redirects?
            if (!client.followRedirects())
                return null;
            String location = userResponse.header("Location");
            if (location == null)
                return null;
            HttpUrl url = userResponse.request().url().resolve(location);
            // Don't follow redirects to unsupported protocols.
            if (url == null)
                return null;
            // If configured, don't follow redirects between SSL and non-SSL.
            boolean sameScheme = url.scheme().equals(userResponse.request().url().scheme());
            if (!sameScheme && !client.followSslRedirects())
                return null;
            // Most redirects don't include a request body.
            Request.Builder requestBuilder = userResponse.request().newBuilder();
            if (HttpMethod.permitsRequestBody(method)) {
                final boolean maintainBody = HttpMethod.redirectsWithBody(method);
                if (HttpMethod.redirectsToGet(method)) {
                    requestBuilder.method("GET", null);
                } else {
                    RequestBody requestBody = maintainBody ? userResponse.request().body() : null;
                    requestBuilder.method(method, requestBody);
                }
                if (!maintainBody) {
                    requestBuilder.removeHeader("Transfer-Encoding");
                    requestBuilder.removeHeader("Content-Length");
                    requestBuilder.removeHeader("Content-Type");
                }
            }
            // way to retain them.
            if (!sameConnection(userResponse, url)) {
                requestBuilder.removeHeader("Authorization");
            }
            return requestBuilder.url(url).build();
        case HTTP_CLIENT_TIMEOUT:
            // repeat the request (even non-idempotent ones.)
            if (userResponse.request().body() instanceof UnrepeatableRequestBody) {
                return null;
            }
            return userResponse.request();
        default:
            return null;
    }
}
Also used : ProtocolException(java.net.ProtocolException) Connection(okhttp3.Connection) Request(okhttp3.Request) HttpUrl(okhttp3.HttpUrl) Proxy(java.net.Proxy) Route(okhttp3.Route) RequestBody(okhttp3.RequestBody)

Example 25 with Proxy

use of java.net.Proxy in project okhttp by square.

the class RealConnection method connectSocket.

/** Does all the work necessary to build a full HTTP or HTTPS connection on a raw socket. */
private void connectSocket(int connectTimeout, int readTimeout) throws IOException {
    Proxy proxy = route.proxy();
    Address address = route.address();
    rawSocket = proxy.type() == Proxy.Type.DIRECT || proxy.type() == Proxy.Type.HTTP ? address.socketFactory().createSocket() : new Socket(proxy);
    rawSocket.setSoTimeout(readTimeout);
    try {
        Platform.get().connectSocket(rawSocket, route.socketAddress(), connectTimeout);
    } catch (ConnectException e) {
        ConnectException ce = new ConnectException("Failed to connect to " + route.socketAddress());
        ce.initCause(e);
        throw ce;
    }
    source = Okio.buffer(Okio.source(rawSocket));
    sink = Okio.buffer(Okio.sink(rawSocket));
}
Also used : Proxy(java.net.Proxy) Address(okhttp3.Address) SSLSocket(javax.net.ssl.SSLSocket) RealWebSocket(okhttp3.internal.ws.RealWebSocket) Socket(java.net.Socket) ConnectException(java.net.ConnectException)

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