use of org.apache.http.params.HttpParams in project Java-readability by basis-technology-corp.
the class HttpPageReader method readPage.
/** {@inheritDoc}*/
@Override
public String readPage(String url) throws PageReadException {
LOG.info("Reading " + url);
HttpParams httpParameters = new BasicHttpParams();
// Set the timeout in milliseconds until a connection is established.
int timeoutConnection = 3000;
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
// Set the default socket timeout (SO_TIMEOUT)
// in milliseconds which is the timeout for waiting for data.
int timeoutSocket = 10000;
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
DefaultHttpClient httpclient = new DefaultHttpClient(httpParameters);
HttpContext localContext = new BasicHttpContext();
HttpGet get = new HttpGet(url);
InputStream response = null;
HttpResponse httpResponse = null;
try {
try {
httpResponse = httpclient.execute(get, localContext);
int resp = httpResponse.getStatusLine().getStatusCode();
if (HttpStatus.SC_OK != resp) {
LOG.error("Download failed of " + url + " status " + resp + " " + httpResponse.getStatusLine().getReasonPhrase());
return null;
}
String respCharset = EntityUtils.getContentCharSet(httpResponse.getEntity());
return readContent(httpResponse.getEntity().getContent(), respCharset);
} finally {
if (response != null) {
response.close();
}
if (httpResponse != null && httpResponse.getEntity() != null) {
httpResponse.getEntity().consumeContent();
}
}
} catch (IOException e) {
LOG.error("Download failed of " + url, e);
throw new PageReadException("Failed to read " + url, e);
}
}
use of org.apache.http.params.HttpParams in project baker-android by bakerframework.
the class CheckInternetTask method hasInternetConnection.
public boolean hasInternetConnection() {
boolean result;
try {
final int timeout = this.context.getResources().getInteger(R.integer.check_internet_timeout);
final String host = this.context.getString(R.string.check_internet_with_host);
Log.d(this.getClass().toString(), "TESTING INTERNET CONNECTION WITH HOST " + host + " AND TIMEOUT " + timeout);
//result = InetAddress.getByName(host).isReachable(timeout);
HttpGet httpGet = new HttpGet(host);
HttpParams httpParameters = new BasicHttpParams();
// Set the timeout in milliseconds until a connection is established.
// The default value is zero, that means the timeout is not used.
//int timeoutConnection = 3000;
//HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
// Set the default socket timeout (SO_TIMEOUT)
// in milliseconds which is the timeout for waiting for data.
HttpConnectionParams.setSoTimeout(httpParameters, timeout);
DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters);
HttpResponse response = httpClient.execute(httpGet);
int statusCode = response.getStatusLine().getStatusCode();
Log.d(this.getClass().toString(), "THE TESTING INTERNET CONNECTION STATUS CODE WAS " + statusCode);
if (statusCode == HttpStatus.SC_OK) {
result = true;
} else {
result = false;
}
} catch (Exception ioe) {
Log.e(this.getClass().toString(), ioe.getMessage());
result = false;
}
return result;
}
use of org.apache.http.params.HttpParams in project baker-android by bakerframework.
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);
Object sessionCache = null;
// Use a session cache for SSL sockets -- Froyo only
if (null != context && null != sSslSessionCacheClass) {
Constructor<?> ct;
try {
ct = sSslSessionCacheClass.getConstructor(Context.class);
sessionCache = ct.newInstance(context);
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// 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));
SocketFactory sslCertificateSocketFactory = null;
if (null != sessionCache) {
Method getHttpSocketFactoryMethod;
try {
getHttpSocketFactoryMethod = SSLCertificateSocketFactory.class.getDeclaredMethod("getHttpSocketFactory", Integer.TYPE, sSslSessionCacheClass);
sslCertificateSocketFactory = (SocketFactory) getHttpSocketFactoryMethod.invoke(null, SOCKET_OPERATION_TIMEOUT, sessionCache);
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (null == sslCertificateSocketFactory) {
sslCertificateSocketFactory = SSLSocketFactory.getSocketFactory();
}
schemeRegistry.register(new Scheme("https", sslCertificateSocketFactory, 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 Libraries-for-Android-Developers by eoecn.
the class AsyncHttpClient method setTimeout.
/**
* Set the connection and socket timeout. By default, 10 seconds.
*
* @param timeout the connect/socket timeout in milliseconds, at least 1 second
*/
public void setTimeout(int timeout) {
if (timeout < 1000)
timeout = DEFAULT_SOCKET_TIMEOUT;
this.timeout = timeout;
final HttpParams httpParams = this.httpClient.getParams();
ConnManagerParams.setTimeout(httpParams, this.timeout);
HttpConnectionParams.setSoTimeout(httpParams, this.timeout);
HttpConnectionParams.setConnectionTimeout(httpParams, this.timeout);
}
use of org.apache.http.params.HttpParams in project android-volley by mcxiaoke.
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);
}
Aggregations