1

我想获得一个网页,但如果返回连接被拒绝,我只想等待 1 秒

我的代码:

final DefaultHttpClient client = HTTPSHelper.getClientThatAllowAnyHTTPS(connectionManager);
        client.getParams().setParameter(ClientPNames.COOKIE_POLICY,
                CookiePolicy.BROWSER_COMPATIBILITY);

        client.getParams().setParameter("http.socket.timeout", 1000);
        client.getParams().setParameter("http.connection.timeout", 1000);
        client.addRequestInterceptor(new RequestAcceptEncoding());
        client.addResponseInterceptor(new ResponseContentEncoding());

final HttpGet get = new HttpGet(url.getUrl());
            final HttpResponse resp = this.httpClient.execute(get, localContext);

当返回连接被拒绝时,我必须等待很多......有没有办法指定等待连接被拒绝的时间?谢谢

4

1 回答 1

1

您可以通过这种方式设置连接超时:

final HttpParams p = new BasicHttpParams();
 HttpConnectionParams.setConnectionTimeout(p, 1000); 
client = new DefaultHttpClient(httpParams) ;
于 2017-05-19T15:59:58.827 回答