2011-10-15 22 views
11

Quiero ejecutar peticiones HTTP asincrónicas seguras con HTTPClient. Noté que no respeta mi argumento CONNECTION_TIMEOUT.¿Cómo configuro un tiempo de espera de conexión en Apache http client?

El código es ColdFusion/Java hybrid.

client = loader.create("org.apache.http.impl.nio.client.DefaultHttpAsyncClient").init(); 
CoreConnectionPNames = loader.create("org.apache.http.params.CoreConnectionPNames"); 

client.getParams() 
    .setIntParameter(JavaCast("string", CoreConnectionPNames.SO_TIMEOUT), 10) 
    .setIntParameter(JavaCast("string", CoreConnectionPNames.CONNECTION_TIMEOUT), 10); 

client.start(); 

request = loader.create("org.apache.http.client.methods.HttpGet").init("http://www.google.com"); 
future = client.execute(request, javacast("null", "")); 

try { 
   response = future.get(); 
} 
catch(e any) {} 

client.getConnectionManager().shutdown(); 

Independientemente de lo que yo proporciono para connection_timeout, las solicitudes siempre devuelven 200 OK. Verifique el resultado a continuación.

  1. ¿Cómo configuro un tiempo de espera de conexión efectivo?
  2. ¿CONNECTION_TIMEOUT hace algo?

salida

200 OK http://www.google.com/ 

200 OK http://www.google.com/ 

[snip] 

5 requests using Async Client in: 2308 ms 

Respuesta

3

La documentación para apache HttpClient es una especie de manchas. Pruebe esto en su configuración (que trabajó para mí con la versión 4):

HttpConnectionParams.setConnectionTimeout(params, 10000); 
HttpConnectionParams.setSoTimeout(params, 10000); 

... set more parameters here if you want to ... 

SchemeRegistry schemeRegistry = new SchemeRegistry(); 

.. do whatever you ant with the scheme registry here ... 

ThreadSafeClientConnManager connectionManager = new ThreadSafeClientConnManager(params, schemeRegistry); 

client = new DefaultHttpClient(connectionManager, params); 
+0

Gracias. Estoy intentando sacar tu código, pero requiere mucho trabajo traducirlo a CF. Tan desnudo conmigo En los primeros dos linse 'HTTPConnectionParams' se refiere a un objeto que necesito crear. Porfablemente 'org.apache.http.params. *' Pero ¿de dónde viene el argumento 'params' que usted suministra a' setConnectionTimeout'? No puedo arrojarlo allí, de lo contrario lanzaría un error 'params undefined' ... lo siento si esto suena obtuso, pero trabajar con código híbrido java/CF es tedioso. Además, es necesario el objeto SchemeRegistry, ¿puedo dejarlo? – Mohamad

+0

HttpParams params = new BasicHttpParams(); Hay más información aquí: http://hc.apache.org/httpcomponents-client-ga/tutorial/html/index.html y el javadoc está aquí: http://hc.apache.org/httpcomponents-core-ga /httpcore/apidocs/org/apache/http/params/HttpParams.html – Seth

2

tiene que definir un objeto HttpParams utilizando métodos de la clase del marco.

 HttpParams params = new BasicHttpParams(); 
     HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); 
     HttpProtocolParams.setContentCharset(params, HTTP.UTF_8); 
     HttpConnectionParams.setConnectionTimeout(params, 2000); 

     SchemeRegistry registry = new SchemeRegistry(); 
     registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); 
     registry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443)); 

     ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry); 

     HttpClient client = DefaultHttpClient(ccm, params); 
Cuestiones relacionadas