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

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

Introduction

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

Prototype

public static void setSoTimeout(HttpParams httpParams, int i) 

Source Link

Usage

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

/**
 * Perform a HTTP GET request./* w w w .java  2s . c o m*/
 */
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:com.qingzhi.apps.fax.client.NetworkUtilities.java

private static String executeGet(String url, Map<String, String> map) throws IOException {

    HttpClient httpclient = new DefaultHttpClient();
    HttpParams params = httpclient.getParams();
    HttpConnectionParams.setConnectionTimeout(params, HTTP_CONNECTION_TIMEOUT);
    HttpConnectionParams.setSoTimeout(params, HTTP_SO_TIMEOUT);

    HttpPost post = new HttpPost(url);

    ArrayList<BasicNameValuePair> postDate = new ArrayList<BasicNameValuePair>();
    Set<String> set = map.keySet();
    Iterator<String> iterator = set.iterator();
    while (iterator.hasNext()) {
        String key = iterator.next();
        postDate.add(new BasicNameValuePair(key, map.get(key)));
    }//from   ww  w. j a v a2s .  c  o  m
    post.setEntity(new UrlEncodedFormEntity(postDate, HTTP.UTF_8));
    HttpResponse httpResponse = httpclient.execute(post);
    throwErrors(httpResponse);

    HttpEntity httpEntity = httpResponse.getEntity();
    if (httpEntity != null) {
        String response = EntityUtils.toString(httpEntity);
        LOGD(TAG, response);
        return response;
    }
    return null;
}

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   www  .  j  av  a 2  s.c  om*/
    Log.e(TAG, "Already started");
    return false;
}

From source file:com.geeksville.location.LeonardoUpload.java

/**
 * Upload a flight to Leonardo//from  w ww .j  av  a2s  .  c o m
 * 
 * @param username
 * @param password
 * @param postURL
 * @param shortFilename
 * @param igcFile
 *            we will take care of closing this stram
 * @return null for success, otherwise a string description of the problem
 * @throws IOException
 */
public static String upload(String username, String password, String postURL, int competitionClass,
        String shortFilename, String igcFile, int connectionTimeout, int operationTimeout) throws IOException {

    // Strip off extension (leonado docs say they don't want it
    int i = shortFilename.lastIndexOf('.');
    if (i >= 1)
        shortFilename = shortFilename.substring(0, i);
    String sCompetitionClass = String.valueOf(competitionClass);
    HttpParams httpParameters = new BasicHttpParams();
    // Set the timeout in milliseconds until a connection is established.
    HttpConnectionParams.setConnectionTimeout(httpParameters, connectionTimeout);
    // Set the default socket timeout (SO_TIMEOUT) 
    // in milliseconds which is the timeout for waiting for data.
    HttpConnectionParams.setSoTimeout(httpParameters, operationTimeout);

    HttpClient httpclient = new DefaultHttpClient(httpParameters);
    httpclient.getParams().setBooleanParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, false);
    HttpPost httppost = new HttpPost(postURL);
    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
    nameValuePairs.add(new BasicNameValuePair("user", username));
    nameValuePairs.add(new BasicNameValuePair("pass", password));
    nameValuePairs.add(new BasicNameValuePair("igcfn", shortFilename));
    nameValuePairs.add(new BasicNameValuePair("Klasse", sCompetitionClass));
    nameValuePairs.add(new BasicNameValuePair("IGCigcIGC", igcFile));
    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

    HttpResponse response = httpclient.execute(httppost);
    HttpEntity entity = response.getEntity();

    String resp = EntityUtils.toString(entity);

    // An error looks like:
    // <html><body>problem<br>This is not a valid .igc
    // file</body></html>

    // Check for success
    if (resp.contains("flight scored"))
        resp = null;
    else {
        int bodLoc = resp.indexOf("<body>");
        if (bodLoc >= 0)
            resp = resp.substring(bodLoc + 6);
        int probLoc = resp.indexOf("problem");
        if (probLoc >= 0)
            resp = resp.substring(probLoc + 7);
        if (resp.startsWith("<br>"))
            resp = resp.substring(4);
        int markLoc = resp.indexOf('<');
        if (markLoc >= 0)
            resp = resp.substring(0, markLoc);
        resp = resp.trim();
    }

    return resp;
}

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;/* ww w .ja  va2 s  .  c om*/
    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:edu.vanderbilt.android.vuparking.network.ParkingClient.java

public ParkingClient(Context context) {
    mContext = context;/*from  www  . j a  v  a2s  . c om*/
    HttpParams httpParameters = new BasicHttpParams();
    // Set the timeout in milliseconds.
    int timeoutConnection = 3000;
    HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
    // Set the default socket timeout (SO_TIMEOUT) 
    int timeoutSocket = 5000;
    HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
    client = new DefaultHttpClient(httpParameters);
}

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   ww  w  .j  av a  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.dss886.nForumSDK.NForumSDK.java

/**
 * ?appkey/*from   ww  w . j a va 2s  . c  om*/
 * @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:fr.eoidb.util.AndroidUrlDownloader.java

@Override
public InputStream urlToInputStream(Context context, String url) throws DownloadException {

    if (!isNetworkAvailable(context)) {
        throw new DownloadException("No internet connection!");
    }//from   w  ww  . ja v  a 2 s .c  o  m

    try {
        HttpParams httpParameters = new BasicHttpParams();
        // Set the timeout in milliseconds until a connection is established.
        // The default value is zero, that means the timeout is not used.
        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 = 5000;
        HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);

        HttpClient httpclient = new DefaultHttpClient(httpParameters);

        HttpGet httpget = new HttpGet(url);
        httpget.addHeader("Accept-Encoding", "gzip");

        HttpResponse response;

        response = httpclient.execute(httpget);
        Log.v(LOG_TAG, response.getStatusLine().toString());

        HttpEntity entity = response.getEntity();

        final int statusCode = response.getStatusLine().getStatusCode();
        if (statusCode != HttpStatus.SC_OK) {
            String message = "Error " + statusCode + " while retrieving url " + url;
            Log.w("AndroidUrlDownloader", message);
            throw new DownloadException(message);
        }

        if (entity != null) {

            InputStream instream = entity.getContent();
            Header contentEncoding = response.getFirstHeader("Content-Encoding");
            if (contentEncoding != null && contentEncoding.getValue().equalsIgnoreCase("gzip")) {
                Log.v(LOG_TAG, "Accepting gzip for url : " + url);
                instream = new GZIPInputStream(instream);
            }

            return instream;
        }

    } catch (IllegalStateException e) {
        throw new DownloadException(e);
    } catch (IOException e) {
        throw new DownloadException(e);
    }

    return null;
}

From source file:com.googlecode.noweco.webmail.httpclient.UnsecureHttpClientFactory.java

public DefaultHttpClient createUnsecureHttpClient(final HttpHost proxy) {
    DefaultHttpClient httpclient = new DefaultHttpClient(new ThreadSafeClientConnManager());
    SchemeRegistry schemeRegistry = httpclient.getConnectionManager().getSchemeRegistry();
    schemeRegistry.unregister("https");
    try {//from  w  w w .  jav  a2 s  .  c om
        SSLContext instance = SSLContext.getInstance("TLS");
        TrustManager tm = UnsecureX509TrustManager.INSTANCE;
        instance.init(null, new TrustManager[] { tm }, null);
        schemeRegistry.register(new Scheme("https", 443,
                new SSLSocketFactory(instance, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER)));
    } catch (Exception e) {
        throw new RuntimeException("TLS issue", e);
    }
    httpclient.removeResponseInterceptorByClass(ResponseProcessCookies.class);
    httpclient.addResponseInterceptor(new UnsecureResponseProcessCookies());
    HttpParams params = httpclient.getParams();
    if (proxy != null) {
        ConnRouteParams.setDefaultProxy(params, proxy);
    }
    HttpConnectionParams.setSoTimeout(params, 7000);
    return httpclient;
}