如何处理httpclient 4.3+中的超时?

时间:2014-04-05 15:13:24

标签: java apache-httpclient-4.x

在旧的4.0库中,您只需:

DefaultHttpClient httpClient = new DefaultHttpClient();
HttpParams params = httpClient.getParams();
HttpConnectionParams.setConnectionTimeout(httpParams, connectionTimeoutMillis);
HttpConnectionParams.setSoTimeout(httpParams, socketTimeoutMillis);

但现在不推荐使用DefaultHttpClient,为了创建一个httpclient,你必须这样做:

HttpClients.createDefault();

这很好,但不允许我为请求设置超时。那么,是否有一种解决方法/方式而不返回已弃用的类?

2 个答案:

答案 0 :(得分:1)

解决了...在新图书馆中这就是这样:

private static HttpClient createHttpClient(){
    RequestConfig config = RequestConfig.custom()
            .setSocketTimeout(TIMEOUT)
            .setConnectTimeout(TIMEOUT)
            .build();

    HttpClientBuilder hcBuilder = HttpClients.custom();
    hcBuilder.setDefaultRequestConfig(config);

    return hcBuilder.build();
}

您在RequestConfig对象中设置它并将其传递给httpclientbuilder,以构建您自己的http客户端实例。

答案 1 :(得分:0)

您可以使用java.net.HttpURLConnection和java.net.URL库。我使用一个小的util类来做我在我的应用程序中需要的任何http请求。它允许设置连接和读取超时。

package utility;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class URLReader {
    /**
     * @param args
     */
    public static String getURL(String p_URL) {
        StringBuffer out = new StringBuffer();

        try {
            //get default report id
            URL url = new URL(p_URL);

            HttpURLConnection huc = (HttpURLConnection) url.openConnection();
            HttpURLConnection.setFollowRedirects(false);
             huc.setConnectTimeout(30 * 1000);
             huc.setReadTimeout(300 * 1000);
             huc.setRequestMethod("GET");
             huc.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.1.2) Gecko/20090729 Firefox/3.5.2 (.NET CLR 3.5.30729)");
             huc.connect();

            InputStream input = huc.getInputStream();

            // Read all the text returned by the server
            BufferedReader in = new BufferedReader(new InputStreamReader(input));
            String inputLine;

            //int x = 0;
            while ((inputLine = in.readLine()) != null)
            {
                out.append(inputLine);
            }

            in.close();
        } catch (Exception e) {
             e.printStackTrace();
        } 

        return out.toString();
    }

    public static String getURL(String p_URL, String p_userPass) {
        StringBuffer out = new StringBuffer(); 

        try {
            //get default report id
            URL url = new URL(p_URL);

            // Encode String
            String encoding = new sun.misc.BASE64Encoder().encode (p_userPass.getBytes());

            HttpURLConnection huc = (HttpURLConnection) url.openConnection();
            HttpURLConnection.setFollowRedirects(false);
             huc.setRequestProperty("Authorization", "Basic " + encoding);
             huc.setConnectTimeout(30 * 1000);
             huc.setReadTimeout(300 * 1000);
             huc.setRequestMethod("GET");
             huc.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.1.2) Gecko/20090729 Firefox/3.5.2 (.NET CLR 3.5.30729)");
             huc.connect();

            InputStream input = huc.getInputStream();

            BufferedReader in = new BufferedReader(new InputStreamReader(input, "ISO-8859-1"));
            String line;
            while ((line = in.readLine()) != null) {
                out.append(line);
            }
            in.close();
        } catch (Exception e) {
             e.printStackTrace();
        }

        return out.toString();
    }

    public static InputStream getURLStream(String p_URL, String p_userPass) {
        InputStream out = null;

        try {
            //get default report id
            URL url = new URL(p_URL);

            // Encode String
            String encoding = new sun.misc.BASE64Encoder().encode (p_userPass.getBytes());

            HttpURLConnection huc = (HttpURLConnection) url.openConnection();
            HttpURLConnection.setFollowRedirects(false);
             huc.setRequestProperty("Authorization", "Basic " + encoding);
             huc.setConnectTimeout(30 * 1000);
             huc.setReadTimeout(300 * 1000);
             huc.setRequestMethod("GET");
             huc.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.1.2) Gecko/20090729 Firefox/3.5.2 (.NET CLR 3.5.30729)");
             huc.connect();

            out = huc.getInputStream();

        } catch (Exception e) {
             e.printStackTrace();
        }

        return out;
    }

    public static InputStream getURLStream(String p_URL) {
        InputStream out = null;

        try {
            //get default report id
            URL url = new URL(p_URL);

            HttpURLConnection huc = (HttpURLConnection) url.openConnection();
            HttpURLConnection.setFollowRedirects(false);
             huc.setConnectTimeout(30 * 1000);
             huc.setReadTimeout(300 * 1000);
             huc.setRequestMethod("GET");
             huc.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.1.2) Gecko/20090729 Firefox/3.5.2 (.NET CLR 3.5.30729)");
             huc.connect();

            out = huc.getInputStream();

        } catch (Exception e) {
             e.printStackTrace();
        } 

        return out;
    }
}