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);
}
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);
}
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);
}
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;
}
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;
}
Aggregations