Java – set httpparams object in httpclient 4.0

I'm creating a new httpclient by passing threadsafeclientconnmanager and httpparams in its constructor, but it always throws wrong request errors

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); Displaying the 400 status code in my code means that there is something wrong with my request If I pass null in the defaulthttpclient constructor

g_client = new DefaultHttpClient(connmgr,null);

The client is executing successfully, but it is incorrect because I need the basic parameter set maxconnectionperroute I pasted suspicious code here Please look and help me I'm stuck here

I use httpclient version 4.0

Solution

In one of our old projects, except that we directly set the default maximum number of connections for each route on the setdefaultmaxperroute (int) method in threadsafeclientconnmanager, we almost follow the actions you want to perform (we don't need to set the parameters like you)

In addition, I believe in connmanagerparams SetTimeout (basicparams, 2000) is equivalent to (and can be replaced by) g_ client. getParams(). setParameter(ConnManagerPNames.TIMEOUT,2000);.

Alternative code example:

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(ConnManagerPNames.TIMEOUT,2000);   /// Alternative approach to yours ///
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>