Search in sources :

Example 11 with ConnectTimeoutException

use of org.apache.http.conn.ConnectTimeoutException in project robovm by robovm.

the class PlainSocketFactory method connectSocket.

// non-javadoc, see interface org.apache.http.conn.SocketFactory
public Socket connectSocket(Socket sock, String host, int port, InetAddress localAddress, int localPort, HttpParams params) throws IOException {
    if (host == null) {
        throw new IllegalArgumentException("Target host may not be null.");
    }
    if (params == null) {
        throw new IllegalArgumentException("Parameters may not be null.");
    }
    if (sock == null)
        sock = createSocket();
    if ((localAddress != null) || (localPort > 0)) {
        // we need to bind explicitly
        if (localPort < 0)
            // indicates "any"
            localPort = 0;
        InetSocketAddress isa = new InetSocketAddress(localAddress, localPort);
        sock.bind(isa);
    }
    int timeout = HttpConnectionParams.getConnectionTimeout(params);
    InetSocketAddress remoteAddress;
    if (this.nameResolver != null) {
        remoteAddress = new InetSocketAddress(this.nameResolver.resolve(host), port);
    } else {
        remoteAddress = new InetSocketAddress(host, port);
    }
    try {
        sock.connect(remoteAddress, timeout);
    } catch (SocketTimeoutException ex) {
        throw new ConnectTimeoutException("Connect to " + remoteAddress + " timed out");
    }
    return sock;
}
Also used : SocketTimeoutException(java.net.SocketTimeoutException) InetSocketAddress(java.net.InetSocketAddress) ConnectTimeoutException(org.apache.http.conn.ConnectTimeoutException)

Example 12 with ConnectTimeoutException

use of org.apache.http.conn.ConnectTimeoutException in project robolectric by robolectric.

the class FakeHttpLayer method emulateRequest.

public HttpResponse emulateRequest(HttpHost httpHost, HttpRequest httpRequest, HttpContext httpContext, RequestDirector requestDirector) throws HttpException, IOException {
    if (logHttpRequests) {
        System.out.println("  <-- " + httpRequest.getRequestLine());
    }
    HttpResponse httpResponse = findResponse(httpRequest);
    if (logHttpRequests) {
        System.out.println("  --> " + (httpResponse == null ? null : httpResponse.getStatusLine().getStatusCode()));
    }
    if (httpResponse == null) {
        throw new RuntimeException("Unexpected call to execute, no pending responses are available. See Robolectric.addPendingResponse(). Request was: " + httpRequest.getRequestLine().getMethod() + " " + httpRequest.getRequestLine().getUri());
    } else {
        HttpParams params = httpResponse.getParams();
        if (HttpConnectionParams.getConnectionTimeout(params) < 0) {
            throw new ConnectTimeoutException("Socket is not connected");
        } else if (HttpConnectionParams.getSoTimeout(params) < 0) {
            throw new ConnectTimeoutException("The operation timed out");
        }
    }
    addRequestInfo(new HttpRequestInfo(httpRequest, httpHost, httpContext, requestDirector));
    addHttpResponse(httpResponse);
    return httpResponse;
}
Also used : HttpParams(org.apache.http.params.HttpParams) HttpResponse(org.apache.http.HttpResponse) ConnectTimeoutException(org.apache.http.conn.ConnectTimeoutException)

Example 13 with ConnectTimeoutException

use of org.apache.http.conn.ConnectTimeoutException in project robolectric by robolectric.

the class ShadowDefaultRequestDirectorTest method shouldSupportSocketTimeoutWithExceptions.

@Test
public void shouldSupportSocketTimeoutWithExceptions() throws Exception {
    FakeHttp.setDefaultHttpResponse(new TestHttpResponse() {

        @Override
        public HttpParams getParams() {
            HttpParams httpParams = super.getParams();
            HttpConnectionParams.setSoTimeout(httpParams, -1);
            return httpParams;
        }
    });
    DefaultHttpClient client = new DefaultHttpClient();
    try {
        client.execute(new HttpGet("http://www.nowhere.org"));
    } catch (ConnectTimeoutException x) {
        return;
    }
    fail("Exception should have been thrown");
}
Also used : HttpParams(org.apache.http.params.HttpParams) HttpGet(org.apache.http.client.methods.HttpGet) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) ConnectTimeoutException(org.apache.http.conn.ConnectTimeoutException) Test(org.junit.Test)

Example 14 with ConnectTimeoutException

use of org.apache.http.conn.ConnectTimeoutException in project robolectric by robolectric.

the class ShadowDefaultRequestDirectorTest method shouldSupportConnectionTimeoutWithExceptions.

@Test
public void shouldSupportConnectionTimeoutWithExceptions() throws Exception {
    FakeHttp.setDefaultHttpResponse(new TestHttpResponse() {

        @Override
        public HttpParams getParams() {
            HttpParams httpParams = super.getParams();
            HttpConnectionParams.setConnectionTimeout(httpParams, -1);
            return httpParams;
        }
    });
    DefaultHttpClient client = new DefaultHttpClient();
    try {
        client.execute(new HttpGet("http://www.nowhere.org"));
    } catch (ConnectTimeoutException x) {
        return;
    }
    fail("Exception should have been thrown");
}
Also used : HttpParams(org.apache.http.params.HttpParams) HttpGet(org.apache.http.client.methods.HttpGet) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) ConnectTimeoutException(org.apache.http.conn.ConnectTimeoutException) Test(org.junit.Test)

Example 15 with ConnectTimeoutException

use of org.apache.http.conn.ConnectTimeoutException in project iosched by google.

the class BasicNetwork method performRequest.

@Override
public NetworkResponse performRequest(Request<?> request) throws VolleyError {
    long requestStart = SystemClock.elapsedRealtime();
    while (true) {
        HttpResponse httpResponse = null;
        byte[] responseContents = null;
        Map<String, String> responseHeaders = new HashMap<String, String>();
        try {
            // Gather headers.
            Map<String, String> headers = new HashMap<String, String>();
            addCacheHeaders(headers, request.getCacheEntry());
            httpResponse = mHttpStack.performRequest(request, headers);
            StatusLine statusLine = httpResponse.getStatusLine();
            int statusCode = statusLine.getStatusCode();
            responseHeaders = convertHeaders(httpResponse.getAllHeaders());
            // Handle cache validation.
            if (statusCode == HttpStatus.SC_NOT_MODIFIED) {
                return new NetworkResponse(HttpStatus.SC_NOT_MODIFIED, request.getCacheEntry() == null ? null : request.getCacheEntry().data, responseHeaders, true);
            }
            // Some responses such as 204s do not have content.  We must check.
            if (httpResponse.getEntity() != null) {
                responseContents = entityToBytes(httpResponse.getEntity());
            } else {
                // Add 0 byte response as a way of honestly representing a
                // no-content request.
                responseContents = new byte[0];
            }
            // if the request is slow, log it.
            long requestLifetime = SystemClock.elapsedRealtime() - requestStart;
            logSlowRequests(requestLifetime, request, responseContents, statusLine);
            if (statusCode < 200 || statusCode > 299) {
                throw new IOException();
            }
            return new NetworkResponse(statusCode, responseContents, responseHeaders, false);
        } catch (SocketTimeoutException e) {
            attemptRetryOnException("socket", request, new TimeoutError());
        } catch (ConnectTimeoutException e) {
            attemptRetryOnException("connection", request, new TimeoutError());
        } catch (MalformedURLException e) {
            throw new RuntimeException("Bad URL " + request.getUrl(), e);
        } catch (IOException e) {
            int statusCode = 0;
            NetworkResponse networkResponse = null;
            if (httpResponse != null) {
                statusCode = httpResponse.getStatusLine().getStatusCode();
            } else {
                throw new NoConnectionError(e);
            }
            VolleyLog.e("Unexpected response code %d for %s", statusCode, request.getUrl());
            if (responseContents != null) {
                networkResponse = new NetworkResponse(statusCode, responseContents, responseHeaders, false);
                if (statusCode == HttpStatus.SC_UNAUTHORIZED || statusCode == HttpStatus.SC_FORBIDDEN) {
                    attemptRetryOnException("auth", request, new AuthFailureError(networkResponse));
                } else {
                    // TODO: Only throw ServerError for 5xx status codes.
                    throw new ServerError(networkResponse);
                }
            } else {
                throw new NetworkError(networkResponse);
            }
        }
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) HashMap(java.util.HashMap) ServerError(com.android.volley.ServerError) HttpResponse(org.apache.http.HttpResponse) AuthFailureError(com.android.volley.AuthFailureError) NetworkError(com.android.volley.NetworkError) NoConnectionError(com.android.volley.NoConnectionError) IOException(java.io.IOException) TimeoutError(com.android.volley.TimeoutError) StatusLine(org.apache.http.StatusLine) SocketTimeoutException(java.net.SocketTimeoutException) NetworkResponse(com.android.volley.NetworkResponse) ConnectTimeoutException(org.apache.http.conn.ConnectTimeoutException)

Aggregations

ConnectTimeoutException (org.apache.http.conn.ConnectTimeoutException)38 IOException (java.io.IOException)17 SocketTimeoutException (java.net.SocketTimeoutException)16 HttpResponse (org.apache.http.HttpResponse)14 HashMap (java.util.HashMap)12 StatusLine (org.apache.http.StatusLine)11 MalformedURLException (java.net.MalformedURLException)10 TimeoutError (com.android.volley.TimeoutError)9 SocketException (java.net.SocketException)9 AuthFailureError (com.android.volley.AuthFailureError)8 NetworkError (com.android.volley.NetworkError)8 NetworkResponse (com.android.volley.NetworkResponse)8 NoConnectionError (com.android.volley.NoConnectionError)8 ServerError (com.android.volley.ServerError)8 DefaultHttpClient (org.apache.http.impl.client.DefaultHttpClient)8 ConnectException (java.net.ConnectException)6 HttpPost (org.apache.http.client.methods.HttpPost)6 Test (org.junit.Test)6 Entry (com.android.volley.Cache.Entry)4 InetSocketAddress (java.net.InetSocketAddress)4