Pregunta

In old 4.0 library you would simply do:

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

but now DefaultHttpClient is deprecated and in order to create an httpclient you have to do:

HttpClients.createDefault();

this is nice but doesn't allow me to set a timeout for requests. So, is there a workaround/way without returning to deprecated class?

¿Fue útil?

Solución

Solved...in the new library this is the way:

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

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

    return hcBuilder.build();
}

You set this in the RequestConfig object and pass it to the httpclientbuilder so to build your own http client instance.

Otros consejos

You can use java.net.HttpURLConnection and java.net.URL libraries. I use a little util class to do any http request I need in my apps. It allows for setting both connect and read timeouts.

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;
    }
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top