HttpClient和连接超时

时间:2015-11-04 19:41:24

标签: java httpclient apache-httpclient-4.x

我通过Apache HttpClient发送http请求,我的代码在这里:

    HttpHost proxy = new HttpHost("78.1.1.222", 80);

    DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxy);
    httpClient = HttpClients.custom()
        .setRoutePlanner(routePlanner)
        .build();
        HttpGet httpGet = new HttpGet(url);
        httpGet.addHeader("Authorization","Basic " + encoding);
        httpGet.addHeader("Cache-Control", "no-cache");
        httpResponse = httpClient.execute(httpGet);

        responseCode=httpResponse.getStatusLine().getStatusCode();
        ........
        ........(code continue..)

我的问题是如何为此代码添加连接超时时间? 请注意,我必须使用代理,我使用HttpClient 4.4。

1 个答案:

答案 0 :(得分:3)

http://www.baeldung.com/httpclient-timeout解释了设置连接超时的各种方法。