Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set HttpParams object in HttpClient 4.0

I am creating new HttpClient, by passing ThreadSafeClientConnManager and HttpParams in it's constructor, but it is always throws bad request error. To figure out what went wrong I had debugged it but all in vain. Provide me with some solution Here is my code block

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

HttpParams basicParams = new BasicHttpParams();
ThreadSafeClientConnManager connmgr = new ThreadSafeClientConnManager(basicParams, sc);
ConnManagerParams.setMaxConnectionsPerRoute(
    basicParams,
    // if we have more than 5 concurrent leads, good problem to have
    new ConnPerRoute() {
        public int getMaxForRoute(HttpRoute httproute) {
        return 5;
    }
}); 

g_client = new DefaultHttpClient(connmgr, basicParams);

// It's a lead, be forgiving with timeout
g_client.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 5000);
g_client.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 5000);
ConnManagerParams.setTimeout(basicParams, 2000);

when I call g_client.execute(postMethod); in my code it is shows 400 status code means something is wrong with my request. If I pass null in my DefaultHttpClient constructor

g_client = new DefaultHttpClient(connmgr, null);

The client is executing successfully, but it's not right way as I need basic param set maxconnectionperroute. I had paste here the doubtful code. Please have a look and help me. I am stuck here.

I am using HttpClient 4.0 version.

like image 326
kailash gaur Avatar asked Sep 04 '13 12:09

kailash gaur


2 Answers

In one of our older projects we do almost exactly what you are trying to do except we set the default max connections per route directly on the setDefaultMaxPerRoute(int) method in ThreadSafeClientConnManager (without the need for setting params in the way that you do).

Also, I believe that ConnManagerParams.setTimeout(basicParams, 2000) is equivalent to (and can be replaced with) g_client.getParams().setParameter(ConnManagerPNames.TIMEOUT, 2000);.

Example alternative code:

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

ThreadSafeClientConnManager connmgr = new ThreadSafeClientConnManager(sc);
connmgr.setDefaultMaxPerRoute(5);  /// Alternative approach to yours ///

DefaultHttpClient g_client = new DefaultHttpClient(connmgr);
g_client.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 5000);
g_client.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 5000);
g_client.getParams().setParameter(ConnManagerPNames.TIMEOUT, 2000);   /// Alternative approach to yours ///
like image 51
cosjav Avatar answered Nov 05 '22 05:11

cosjav


Create the object with the default (working) constructor. Then modify the properties of the object as defined in the Apache HttpClient documentation for the version being used. Example for 4.5:

http://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/impl/client/DefaultHttpClient.html

For managing things like max connections per route, take a look at that (different classes involved) documentation:

http://hc.apache.org/httpcomponents-client-ga/tutorial/html/connmgmt.html

like image 36
Darrell Teague Avatar answered Nov 05 '22 05:11

Darrell Teague