Search in sources :

Example 66 with HttpParams

use of org.apache.http.params.HttpParams in project platform_external_apache-http by android.

the class AndroidHttpClient method newInstance.

/**
     * Create a new HttpClient with reasonable defaults (which you can update).
     *
     * @param userAgent to report in your HTTP requests
     * @param context to use for caching SSL sessions (may be null for no caching)
     * @return AndroidHttpClient for you to use for all your requests.
     */
public static AndroidHttpClient newInstance(String userAgent, Context context) {
    HttpParams params = new BasicHttpParams();
    // Turn off stale checking.  Our connections break all the time anyway,
    // and it's not worth it to pay the penalty of checking every time.
    HttpConnectionParams.setStaleCheckingEnabled(params, false);
    HttpConnectionParams.setConnectionTimeout(params, SOCKET_OPERATION_TIMEOUT);
    HttpConnectionParams.setSoTimeout(params, SOCKET_OPERATION_TIMEOUT);
    HttpConnectionParams.setSocketBufferSize(params, 8192);
    // Don't handle redirects -- return them to the caller.  Our code
    // often wants to re-POST after a redirect, which we must do ourselves.
    HttpClientParams.setRedirecting(params, false);
    // Use a session cache for SSL sockets
    SSLSessionCache sessionCache = context == null ? null : new SSLSessionCache(context);
    // Set the specified user agent and register standard protocols.
    HttpProtocolParams.setUserAgent(params, userAgent);
    SchemeRegistry schemeRegistry = new SchemeRegistry();
    schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
    schemeRegistry.register(new Scheme("https", SSLCertificateSocketFactory.getHttpSocketFactory(SOCKET_OPERATION_TIMEOUT, sessionCache), 443));
    ClientConnectionManager manager = new ThreadSafeClientConnManager(params, schemeRegistry);
    // parameters without the funny call-a-static-method dance.
    return new AndroidHttpClient(manager, params);
}
Also used : BasicHttpParams(org.apache.http.params.BasicHttpParams) HttpParams(org.apache.http.params.HttpParams) Scheme(org.apache.http.conn.scheme.Scheme) ThreadSafeClientConnManager(org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager) SchemeRegistry(org.apache.http.conn.scheme.SchemeRegistry) SSLSessionCache(android.net.SSLSessionCache) BasicHttpParams(org.apache.http.params.BasicHttpParams) ClientConnectionManager(org.apache.http.conn.ClientConnectionManager)

Example 67 with HttpParams

use of org.apache.http.params.HttpParams in project openstack4j by ContainX.

the class ApacheHttpClientExecutor method create.

public static ApacheHttpClientExecutor create(Config config) {
    HttpParams params = new BasicHttpParams();
    if (config.getReadTimeout() > 0)
        HttpConnectionParams.setSoTimeout(params, config.getReadTimeout());
    if (config.getConnectTimeout() > 0)
        HttpConnectionParams.setConnectionTimeout(params, config.getConnectTimeout());
    HttpClient client = new DefaultHttpClient(params);
    if (config.getProxy() != null) {
        try {
            URL url = new URL(config.getProxy().getHost());
            HttpHost proxy = new HttpHost(url.getHost(), config.getProxy().getPort(), url.getProtocol());
            client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
    }
    return new ApacheHttpClientExecutor(client);
}
Also used : BasicHttpParams(org.apache.http.params.BasicHttpParams) HttpParams(org.apache.http.params.HttpParams) MalformedURLException(java.net.MalformedURLException) HttpHost(org.apache.http.HttpHost) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) HttpClient(org.apache.http.client.HttpClient) BasicHttpParams(org.apache.http.params.BasicHttpParams) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) URL(java.net.URL)

Example 68 with HttpParams

use of org.apache.http.params.HttpParams in project TaEmCasa by Dionen.

the class HttpClientStack method performRequest.

@Override
public HttpResponse performRequest(Request<?> request, Map<String, String> additionalHeaders) throws IOException, AuthFailureError {
    HttpUriRequest httpRequest = createHttpRequest(request, additionalHeaders);
    addHeaders(httpRequest, additionalHeaders);
    addHeaders(httpRequest, request.getHeaders());
    onPrepareRequest(httpRequest);
    HttpParams httpParams = httpRequest.getParams();
    int timeoutMs = request.getTimeoutMs();
    // TODO: Reevaluate this connection timeout based on more wide-scale
    // data collection and possibly different for wifi vs. 3G.
    HttpConnectionParams.setConnectionTimeout(httpParams, 5000);
    HttpConnectionParams.setSoTimeout(httpParams, timeoutMs);
    return mClient.execute(httpRequest);
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) HttpParams(org.apache.http.params.HttpParams)

Example 69 with HttpParams

use of org.apache.http.params.HttpParams in project dhis2-core by dhis2.

the class HttpUtils method httpPOST.

/**
     * <pre>
     * <b>Description : </b>
     * Method to make an http POST call to a given URL with/without authentication.
     *
     * @param requestURL
     * @param body
     * @param authorize
     * @param username
     * @param password
     * @param contentType
     * @param timeout
     * @return </pre>
     */
public static DhisHttpResponse httpPOST(String requestURL, Object body, boolean authorize, String username, String password, String contentType, int timeout) throws Exception {
    DefaultHttpClient httpclient = null;
    HttpParams params = new BasicHttpParams();
    List<NameValuePair> pairs = new ArrayList<NameValuePair>();
    DhisHttpResponse dhisHttpResponse = null;
    try {
        HttpConnectionParams.setConnectionTimeout(params, timeout);
        HttpConnectionParams.setSoTimeout(params, timeout);
        httpclient = new DefaultHttpClient(params);
        HttpPost httpPost = new HttpPost(requestURL);
        if (body instanceof Map) {
            @SuppressWarnings("unchecked") Map<String, String> parameters = (Map<String, String>) body;
            for (Map.Entry<String, String> parameter : parameters.entrySet()) {
                if (parameter.getValue() != null) {
                    pairs.add(new BasicNameValuePair(parameter.getKey(), parameter.getValue()));
                }
            }
            httpPost.setEntity(new UrlEncodedFormEntity(pairs, "UTF-8"));
        } else if (body instanceof String) {
            httpPost.setEntity(new StringEntity((String) body));
        }
        if (!StringUtils.isNotEmpty(contentType))
            httpPost.setHeader("Content-Type", contentType);
        if (authorize) {
            httpPost.setHeader("Authorization", CodecUtils.getBasicAuthString(username, password));
        }
        HttpResponse response = httpclient.execute(httpPost);
        log.info("Successfully got response from http POST.");
        dhisHttpResponse = processResponse(requestURL, username, response);
    } catch (Exception e) {
        log.error("Exception occurred in httpPOST call with username " + username, e);
        throw e;
    } finally {
        if (httpclient != null) {
            httpclient.getConnectionManager().shutdown();
        }
    }
    return dhisHttpResponse;
}
Also used : BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) HttpPost(org.apache.http.client.methods.HttpPost) ArrayList(java.util.ArrayList) HttpResponse(org.apache.http.HttpResponse) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) IOException(java.io.IOException) StringEntity(org.apache.http.entity.StringEntity) BasicHttpParams(org.apache.http.params.BasicHttpParams) HttpParams(org.apache.http.params.HttpParams) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) BasicHttpParams(org.apache.http.params.BasicHttpParams) Map(java.util.Map)

Example 70 with HttpParams

use of org.apache.http.params.HttpParams in project dhis2-core by dhis2.

the class HttpUtils method httpGET.

/**
     * <pre>
     * <b>Description : </b>
     * Method to make an http GET call to a given URL with/without authentication.
     *
     * @param requestURL
     * @param authorize
     * @param username
     * @param password
     * @param headers
     * @param timeout
     * @return
     * @throws Exception </pre>
     */
public static DhisHttpResponse httpGET(String requestURL, boolean authorize, String username, String password, Map<String, String> headers, int timeout, boolean processResponse) throws Exception {
    DefaultHttpClient httpclient = null;
    DhisHttpResponse dhisHttpResponse = null;
    HttpParams params = new BasicHttpParams();
    try {
        HttpConnectionParams.setConnectionTimeout(params, timeout);
        HttpConnectionParams.setSoTimeout(params, timeout);
        httpclient = new DefaultHttpClient(params);
        HttpGet httpGet = new HttpGet(requestURL);
        if (headers instanceof Map) {
            for (Map.Entry<String, String> e : headers.entrySet()) {
                httpGet.addHeader(e.getKey(), e.getValue());
            }
        }
        if (authorize) {
            httpGet.setHeader("Authorization", CodecUtils.getBasicAuthString(username, password));
        }
        HttpResponse response = httpclient.execute(httpGet);
        if (processResponse) {
            dhisHttpResponse = processResponse(requestURL, username, response);
        } else {
            dhisHttpResponse = new DhisHttpResponse(response, null, response.getStatusLine().getStatusCode());
        }
    } catch (Exception e) {
        log.error("Exception occurred in the httpGET call with username " + username, e);
        throw e;
    } finally {
        if (httpclient != null) {
            httpclient.getConnectionManager().shutdown();
        }
    }
    return dhisHttpResponse;
}
Also used : BasicHttpParams(org.apache.http.params.BasicHttpParams) HttpParams(org.apache.http.params.HttpParams) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) BasicHttpParams(org.apache.http.params.BasicHttpParams) Map(java.util.Map) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) IOException(java.io.IOException)

Aggregations

HttpParams (org.apache.http.params.HttpParams)122 BasicHttpParams (org.apache.http.params.BasicHttpParams)86 DefaultHttpClient (org.apache.http.impl.client.DefaultHttpClient)44 SchemeRegistry (org.apache.http.conn.scheme.SchemeRegistry)32 Scheme (org.apache.http.conn.scheme.Scheme)31 ClientConnectionManager (org.apache.http.conn.ClientConnectionManager)29 IOException (java.io.IOException)28 ThreadSafeClientConnManager (org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager)28 HttpResponse (org.apache.http.HttpResponse)24 HttpHost (org.apache.http.HttpHost)23 Header (org.apache.http.Header)18 HttpGet (org.apache.http.client.methods.HttpGet)13 SSLSocketFactory (org.apache.http.conn.ssl.SSLSocketFactory)11 URI (java.net.URI)10 HttpRequest (org.apache.http.HttpRequest)10 HttpClient (org.apache.http.client.HttpClient)10 HttpUriRequest (org.apache.http.client.methods.HttpUriRequest)10 Socket (java.net.Socket)9 ClientProtocolException (org.apache.http.client.ClientProtocolException)9 GeneralSecurityException (java.security.GeneralSecurityException)8