Example usage for org.apache.http.params HttpConnectionParams setConnectionTimeout

List of usage examples for org.apache.http.params HttpConnectionParams setConnectionTimeout

Introduction

In this page you can find the example usage for org.apache.http.params HttpConnectionParams setConnectionTimeout.

Prototype

public static void setConnectionTimeout(HttpParams httpParams, int i) 

Source Link

Usage

From source file:com.primitive.applicationmanager.BaseApplicationManager.java

/**
 * HttpClient??????/*from  w  w  w.j a v a  2  s .c o  m*/
 * @return HttpClient
 */
protected HttpClient createHttpClient() {
    final HttpClient httpClient = new DefaultHttpClient();
    final HttpParams params = httpClient.getParams();
    HttpConnectionParams.setConnectionTimeout(params, 1000);
    return httpClient;
}

From source file:org.doubango.ngn.services.impl.NgnHttpClientService.java

@Override
public boolean start() {
    Log.d(TAG, "Starting...");

    if (mClient == null) {
        mClient = new DefaultHttpClient();
        final HttpParams params = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(params, sTimeoutConnection);
        HttpConnectionParams.setSoTimeout(params, sTimeoutSocket);
        ((DefaultHttpClient) mClient).setParams(params);
        return true;
    }//from  w w  w  .  j  a  va  2  s.  com
    Log.e(TAG, "Already started");
    return false;
}

From source file:net.giovannicapuano.galax.util.Utils.java

/**
 * Perform a HTTP GET request.//from   w w  w  .j  a va  2s  . c  om
 */
public static HttpData get(String path, Context context) {
    HttpParams httpParameters = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(httpParameters, 5000);
    HttpConnectionParams.setSoTimeout(httpParameters, 10000);

    int status = HttpStatus.SC_INTERNAL_SERVER_ERROR;
    String body = "";

    HttpClient httpClient = new DefaultHttpClient(httpParameters);
    HttpContext localContext = new BasicHttpContext();

    localContext.setAttribute(ClientContext.COOKIE_STORE, new PersistentCookieStore(context));
    StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().permitAll().build());

    try {
        HttpGet httpGet = new HttpGet(context.getString(R.string.server) + path);
        HttpResponse response = httpClient.execute(httpGet, localContext);

        status = response.getStatusLine().getStatusCode();
        body = EntityUtils.toString(response.getEntity());
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }

    return new HttpData(status, body);
}

From source file:de.jetwick.snacktory.HttpPageReader.java

/** {@inheritDoc}*/
@SuppressWarnings("deprecation")
@Override/*from  w  w  w.  j a v  a  2s.c  o m*/
public String readPage(String url) throws PageReadException {
    LOG.info("Reading " + url);
    HttpParams httpParameters = new BasicHttpParams();
    // Set the timeout in milliseconds until a connection is established.
    int timeoutConnection = 3000;
    HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
    // Set the default socket timeout (SO_TIMEOUT) 
    // in milliseconds which is the timeout for waiting for data.
    int timeoutSocket = 10000;
    HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
    DefaultHttpClient httpclient = new DefaultHttpClient(httpParameters);
    HttpContext localContext = new BasicHttpContext();
    HttpGet get = new HttpGet(url);
    InputStream response = null;
    HttpResponse httpResponse = null;
    try {
        try {
            httpResponse = httpclient.execute(get, localContext);
            int resp = httpResponse.getStatusLine().getStatusCode();
            if (HttpStatus.SC_OK != resp) {
                LOG.error("Download failed of " + url + " status " + resp + " "
                        + httpResponse.getStatusLine().getReasonPhrase());
                return null;
            }
            String respCharset = EntityUtils.getContentCharSet(httpResponse.getEntity());

            return readContent(httpResponse.getEntity().getContent(), respCharset);
        } finally {
            if (response != null) {
                response.close();
            }
            if (httpResponse != null && httpResponse.getEntity() != null) {
                httpResponse.getEntity().consumeContent();
            }

        }
    } catch (IOException e) {
        LOG.error("Download failed of " + url, e);
        throw new PageReadException("Failed to read " + url, e);
    }
}

From source file:eu.musesproject.client.connectionmanager.HttpConnectionsHelper.java

/**
 * Http post implementation //  ww w  .j a va  2 s .c o m
 * @param url
 * @param data
 * @return httpResponse
 * @throws ClientProtocolException
 * @throws IOException
 */

public synchronized HttpResponse doPost(String type, String url, String data)
        throws ClientProtocolException, IOException {
    HttpResponse httpResponse = null;
    HttpPost httpPost = new HttpPost(url);
    HttpParams httpParameters = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(httpParameters, TIMEOUT);
    DefaultHttpClient httpclient = new DefaultHttpClient(httpParameters);
    StringEntity s = new StringEntity(data.toString());
    s.setContentEncoding("UTF-8");
    s.setContentType("application/xml");
    httpPost.addHeader("connection-type", type);
    httpPost.setEntity(s);
    httpPost.addHeader("accept", "application/xml");
    if (cookie == null || cookie.isExpired(new Date())) {
        try {
            httpResponse = httpclient.execute(httpPost);
            List<Cookie> cookies = httpclient.getCookieStore().getCookies();
            if (cookies.isEmpty()) {
                Log.d(TAG, "None");
            } else {
                cookie = cookies.get(0);
                cookieExpiryDate = cookie.getExpiryDate();
                Log.d(TAG, "Curent cookie expiry : " + cookieExpiryDate);
            }
        } catch (ClientProtocolException e) {
            Log.d(TAG, e.getMessage());
        } catch (Exception e) {
            Log.d(TAG, e.getMessage());
        }

    } else {
        httpPost.addHeader("accept", "application/xml");
        httpclient.getCookieStore().addCookie(cookie);
        try {
            httpResponse = httpclient.execute(httpPost);
        } catch (ClientProtocolException e) {
            Log.d(TAG, e.getMessage());
        } catch (Exception e) {
            Log.d(TAG, e.getMessage());
        }
    }
    return httpResponse;
}

From source file:org.safegees.safegees.util.HttpUrlConnection.java

public String performGetCall(String requestURL, HashMap<String, String> postDataParams,
        String userCredentials) {

    DefaultHttpClient httpclient = new DefaultHttpClient();
    final HttpParams httpConnParams = httpclient.getParams();
    HttpConnectionParams.setConnectionTimeout(httpConnParams, CONNECTION_TIMEOUT);
    HttpConnectionParams.setSoTimeout(httpConnParams, READ_TIMEOUT);
    HttpGet httpGet = new HttpGet(requestURL);
    //Add the auth header
    if (userCredentials != null)
        httpGet.addHeader("auth", userCredentials);
    HttpResponse response = null;/*from  ww w.ja v  a2  s  . c o  m*/
    String responseStr = null;
    try {
        response = httpclient.execute(httpGet);
        StatusLine statusLine = response.getStatusLine();
        if (statusLine.getStatusCode() == 200
                || (statusLine.getStatusCode() > 200 && statusLine.getStatusCode() < 300)) {
            responseStr = EntityUtils.toString(response.getEntity(), HTTP.UTF_8);
        } else {
            Log.e("GET ERROR", response.getStatusLine().toString());
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return responseStr;

}

From source file:com.netflix.raigad.utils.SystemUtils.java

public static String runHttpGetCommand(String url) throws Exception {
    String return_;
    DefaultHttpClient client = new DefaultHttpClient();
    InputStream isStream = null;//from   w  ww .  j a va 2 s . c  o m
    try {
        HttpParams httpParameters = new BasicHttpParams();
        int timeoutConnection = 1000;
        int timeoutSocket = 1000;
        HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
        HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
        client.setParams(httpParameters);

        HttpGet getRequest = new HttpGet(url);
        getRequest.setHeader("Content-type", "application/json");

        HttpResponse resp = client.execute(getRequest);

        if (resp == null || resp.getEntity() == null) {
            throw new ESHttpException("Unable to execute GET URL (" + url
                    + ") Exception Message: < Null Response or Null HttpEntity >");
        }

        isStream = resp.getEntity().getContent();

        if (resp.getStatusLine().getStatusCode() != 200) {

            throw new ESHttpException("Unable to execute GET URL (" + url + ") Exception Message: ("
                    + IOUtils.toString(isStream, StandardCharsets.UTF_8.toString()) + ")");
        }

        return_ = IOUtils.toString(isStream, StandardCharsets.UTF_8.toString());
        logger.debug("GET URL API: {} returns: {}", url, return_);
    } catch (Exception e) {
        throw new ESHttpException(
                "Caught an exception during execution of URL (" + url + ")Exception Message: (" + e + ")");
    } finally {
        if (isStream != null)
            isStream.close();
    }
    return return_;
}

From source file:com.geertvanderploeg.kiekeboek.client.NetworkUtilities.java

/**
 * Configures the httpClient to connect to the URL provided.
 *//*from  w ww .  java 2 s  . c  o  m*/
public static HttpClient getHttpClient() {
    if (httpClient == null) {
        httpClient = new DefaultHttpClient() {

            // No redirects
            @Override
            protected RedirectHandler createRedirectHandler() {
                return new RedirectHandler() {
                    @Override
                    public URI getLocationURI(HttpResponse response, HttpContext context)
                            throws ProtocolException {
                        return null;
                    }

                    @Override
                    public boolean isRedirectRequested(HttpResponse response, HttpContext context) {
                        return false;
                    }
                };
            }
        };
        final HttpParams params = httpClient.getParams();
        HttpConnectionParams.setConnectionTimeout(params, REGISTRATION_TIMEOUT);
        HttpConnectionParams.setSoTimeout(params, REGISTRATION_TIMEOUT);
        ConnManagerParams.setTimeout(params, REGISTRATION_TIMEOUT);
    }
    return httpClient;
}

From source file:com.dss886.nForumSDK.NForumSDK.java

/**
 * ?appkey/*from  ww w.  j  a v a 2  s.c o  m*/
 * @param host ????http://api.byr.cn/
 *           Host.HOST_* ?
 * @param appkey ?????"?appkey=""&"
 * @param username ??
 * @param password ?
 */
public NForumSDK(String host, String appkey, String username, String password) {
    httpClient = new DefaultHttpClient();
    HttpParams param = httpClient.getParams();
    HttpConnectionParams.setConnectionTimeout(param, timeout);
    HttpConnectionParams.setSoTimeout(param, timeout);
    httpClient = new DefaultHttpClient(param);

    auth = new String(Base64.encodeBase64((username + ":" + password).getBytes()));
    this.host = host;
    this.returnFormat = returnFormat + "?";
    this.appkey = "&appkey=" + appkey;
}

From source file:com.mitchtodd.myweatherapp.webservices.MyWeather2Service.java

public Weather requestWeather(String zipCode) {
    Weather weather = null;//from  w w  w.j a  v a  2 s .  co  m

    StringBuilder builder = new StringBuilder();
    int timeoutConnection = 5000;
    HttpParams httpParameters = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
    HttpClient client = new DefaultHttpClient(httpParameters);
    String requestUrl = createRequestUrl(zipCode);
    HttpGet httpGet = new HttpGet(requestUrl);
    try {
        HttpResponse response = client.execute(httpGet);
        StatusLine statusLine = response.getStatusLine();
        int statusCode = statusLine.getStatusCode();
        if (statusCode == 200) {
            HttpEntity entity = response.getEntity();
            InputStream content = entity.getContent();
            BufferedReader reader = new BufferedReader(new InputStreamReader(content));
            String line;
            while ((line = reader.readLine()) != null) {
                builder.append(line);
            }
            weather = new Weather(builder.toString());
        }
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }

    return weather;
}