Httpclient 设置读取超时及连接重试等配置

  DefaultHttpClient httpclient = new DefaultHttpClient();
        httpclient.getParams().setParameter("http.protocol.cookie-policy",
                CookiePolicy.BROWSER_COMPATIBILITY);
        HttpParams params = httpclient.getParams();
        HttpConnectionParams.setConnectionTimeout(params, 5000);
        HttpConnectionParams.setSoTimeout(params, 1000*60*10);
        DefaultHttpRequestRetryHandler dhr = new DefaultHttpRequestRetryHandler(3,true);
        HttpContext localContext = new BasicHttpContext();
        HttpRequest request2 = (HttpRequest) localContext.getAttribute(
                ExecutionContext.HTTP_REQUEST);
        httpclient.setHttpRequestRetryHandler(dhr);
        BasicCookieStore cookieStore = new BasicCookieStore();
        BasicClientCookie2 cookie = new BasicClientCookie2(name,value);
       cookieStore.addCookie(cookie);
      localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
      HttpGet request = new HttpGet();
        request.setURI(URI.create(url));
        HttpResponse response = null;
        try {
            response = httpclient.execute(request,localContext);
            System.out.println(response.getStatusLine().getStatusCode());
        } catch (IOException e) {
            System.out.println(e);
        }

apache下的:org.apache.http.client类;


你可能感兴趣的:(httpclient,设置超时)