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:com.wareninja.opensource.gravatar4android.common.Utils.java

public static String downloadProfile(String profileUrl) throws GenericException {

    String response = "";

    HttpParams myParams = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(myParams, TIMEOUT);
    HttpConnectionParams.setSoTimeout(myParams, TIMEOUT);

    DefaultHttpClient httpClient = new DefaultHttpClient(myParams);

    HttpGet httpGet = new HttpGet(profileUrl);
    if (CONSTANTS.DEBUG)
        Log.d(TAG, "WebGetURL: " + profileUrl);

    HttpResponse httpResponse = null;//from  www.j a va 2 s  .c o  m
    try {
        httpResponse = httpClient.execute(httpGet);

        response = EntityUtils.toString(httpResponse.getEntity());
    } catch (Exception e) {
        Log.e(TAG, e.getMessage());
        throw new GenericException(e);
    }

    return response;
}

From source file:ro.razius.vianet.LoginTask.java

@Override
protected Void doInBackground(String... params) {
    // set the connection and socket timeouts
    HttpParams httpParameters = new BasicHttpParams();
    int timeoutConnection = 3000;
    int timeoutSocket = 5000;
    HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
    HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);

    // build out GET URL
    List<NameValuePair> postParams = new LinkedList<NameValuePair>();
    postParams.add(new BasicNameValuePair(userParam, params[0]));
    postParams.add(new BasicNameValuePair(passParam, params[1]));

    HttpPost httppost = new HttpPost(authURL);

    // finally do the GET request to login to the ISA Server
    DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters);
    try {//from  w  ww.  j ava2 s. c  om
        Log.d(TAG, "Trying to auth with: " + authURL);
        httppost.setEntity(new UrlEncodedFormEntity(postParams));
        HttpResponse response = httpClient.execute(httppost);
        Log.d(TAG, "HTTP Response: " + response.getStatusLine().toString());
    } catch (Exception e) {
        Log.d(TAG, e.toString()); // no use for errors
    }
    return null;
}

From source file:com.entertailion.android.dial.HttpRequestHelper.java

public static DefaultHttpClient createHttpClient() {
    HttpParams params = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(params, 20000);
    HttpConnectionParams.setSoTimeout(params, 20000);
    HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
    HttpProtocolParams.setContentCharset(params, HTTP.DEFAULT_CONTENT_CHARSET);

    SchemeRegistry schReg = new SchemeRegistry();
    schReg.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
    schReg.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));
    ClientConnectionManager conMgr = new ThreadSafeClientConnManager(params, schReg);

    return new DefaultHttpClient(conMgr, params);
}

From source file:org.andstatus.app.net.http.MisconfiguredSslHttpClientFactory.java

private static HttpParams getHttpParams() {
    HttpParams params = new BasicHttpParams();
    HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
    HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);
    HttpConnectionParams.setStaleCheckingEnabled(params, true);

    HttpProtocolParams.setUseExpectContinue(params, false);
    HttpConnectionParams.setSoTimeout(params, MyPreferences.getConnectionTimeoutMs());
    HttpConnectionParams.setSocketBufferSize(params, 2 * 8192);
    return params;
}

From source file:ch.tatool.core.module.creator.FileDownloadWorker.java

/** Loads the module file from the data server using the provided code. */
public void loadFile(String url, int timeout) {
    // give it a timeout to ensure the user does not wait forever in case of connectivity problems
    HttpParams params = new BasicHttpParams();
    if (timeout > 0) {
        HttpConnectionParams.setConnectionTimeout(params, timeout);
        HttpConnectionParams.setSoTimeout(params, timeout);
    }/*from   ww  w . j a  v  a2 s . c  om*/

    // remove whitespaces since they are not allowed
    url = url.replaceAll("\\s+", "").trim();

    // create a http client
    DefaultHttpClient httpclient = new DefaultHttpClient(params);
    HttpGet httpGet = new HttpGet(url);

    errorTitle = null;
    errorText = null;
    file = null;
    try {
        HttpResponse response = httpclient.execute(httpGet);
        if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
            errorTitle = messages.getString("General.errorMessage.windowTitle.error");
            errorText = messages.getString("DataExportError.online.http");
            errorText += "\n" + response.getStatusLine().getReasonPhrase() + " ("
                    + response.getStatusLine().getStatusCode() + ")";
        } else {
            // copy the response into a temporary file
            HttpEntity entity = response.getEntity();
            byte[] data = EntityUtils.toByteArray(entity);
            File tmpFile = File.createTempFile("tatool_module", "tmp");
            FileUtils.writeByteArrayToFile(tmpFile, data);
            tmpFile.deleteOnExit();
            this.file = tmpFile;
        }
    } catch (IOException ioe) {
        errorTitle = messages.getString("General.errorMessage.windowTitle.error");
        errorText = messages.getString("DataExportError.online.http");
    } finally {
        // make sure we close the connection manager
        httpclient.getConnectionManager().shutdown();
    }
}

From source file:com.aliyun.oss.common.comm.HttpClientFactory.java

public HttpClient createHttpClient(ClientConfiguration config) {
    HttpParams httpClientParams = new BasicHttpParams();
    HttpProtocolParams.setUserAgent(httpClientParams, config.getUserAgent());
    HttpConnectionParams.setConnectionTimeout(httpClientParams, config.getConnectionTimeout());
    HttpConnectionParams.setSoTimeout(httpClientParams, config.getSocketTimeout());
    HttpConnectionParams.setStaleCheckingEnabled(httpClientParams, true);
    HttpConnectionParams.setTcpNoDelay(httpClientParams, true);

    PoolingClientConnectionManager connManager = createConnectionManager(config, httpClientParams);
    DefaultHttpClient httpClient = new DefaultHttpClient(connManager, httpClientParams);

    if (System.getProperty("com.aliyun.oss.disableCertChecking") != null) {
        Scheme sch = new Scheme("https", 443, getSSLSocketFactory());
        httpClient.getConnectionManager().getSchemeRegistry().register(sch);
    }//from  w w  w  . j  a va 2s .c  o m

    String proxyHost = config.getProxyHost();
    int proxyPort = config.getProxyPort();

    if (proxyHost != null && proxyPort > 0) {
        HttpHost proxy = new HttpHost(proxyHost, proxyPort);
        httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);

        String proxyUsername = config.getProxyUsername();
        String proxyPassword = config.getProxyPassword();

        if (proxyUsername != null && proxyPassword != null) {
            String proxyDomain = config.getProxyDomain();
            String proxyWorkstation = config.getProxyWorkstation();

            httpClient.getCredentialsProvider().setCredentials(new AuthScope(proxyHost, proxyPort),
                    new NTCredentials(proxyUsername, proxyPassword, proxyWorkstation, proxyDomain));
        }
    }

    return httpClient;
}

From source file:com.github.commonclasses.network.DownloadUtils.java

public static long download(String urlStr, File dest, boolean append, DownloadListener downloadListener)
        throws Exception {
    int downloadProgress = 0;
    long remoteSize = 0;
    int currentSize = 0;
    long totalSize = -1;

    if (!append && dest.exists() && dest.isFile()) {
        dest.delete();/*from  w w w . jav a  2s.  c  o  m*/
    }

    if (append && dest.exists() && dest.exists()) {
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(dest);
            currentSize = fis.available();
        } catch (IOException e) {
            throw e;
        } finally {
            if (fis != null) {
                fis.close();
            }
        }
    }

    HttpGet request = new HttpGet(urlStr);
    request.setHeader("Content-Type", "application/x-www-form-urlencoded");

    if (currentSize > 0) {
        request.addHeader("RANGE", "bytes=" + currentSize + "-");
    }

    HttpParams params = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(params, CONNECT_TIMEOUT);
    HttpConnectionParams.setSoTimeout(params, DATA_TIMEOUT);
    HttpClient httpClient = new DefaultHttpClient(params);

    InputStream is = null;
    FileOutputStream os = null;
    try {
        HttpResponse response = httpClient.execute(request);
        if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
            is = response.getEntity().getContent();
            remoteSize = response.getEntity().getContentLength();
            Header contentEncoding = response.getFirstHeader("Content-Encoding");
            if (contentEncoding != null && contentEncoding.getValue().equalsIgnoreCase("gzip")) {
                is = new GZIPInputStream(is);
            }
            os = new FileOutputStream(dest, append);
            byte buffer[] = new byte[DATA_BUFFER];
            int readSize = 0;
            while ((readSize = is.read(buffer)) > 0) {
                os.write(buffer, 0, readSize);
                os.flush();
                totalSize += readSize;
                if (downloadListener != null) {
                    downloadProgress = (int) (totalSize * 100 / remoteSize);
                    downloadListener.downloading(downloadProgress);
                }
            }
            if (totalSize < 0) {
                totalSize = 0;
            }
        }
    } catch (Exception e) {
        if (downloadListener != null) {
            downloadListener.exception(e);
        }
        e.printStackTrace();
    } finally {
        if (os != null) {
            os.close();
        }
        if (is != null) {
            is.close();
        }
    }

    if (totalSize < 0) {
        throw new Exception("Download file fail: " + urlStr);
    }

    if (downloadListener != null) {
        downloadListener.downloaded(dest);
    }

    return totalSize;
}

From source file:org.cloudifysource.quality.iTests.test.cli.cloudify.UnsupportedOperationErrorMsgTest.java

@Test(timeOut = AbstractTestSupport.DEFAULT_TEST_TIMEOUT * 2, enabled = true)
public void UnsupportedGetServiceStatusOperationTest() throws MalformedURLException {

    SystemDefaultHttpClient httpClient = new SystemDefaultHttpClient();
    final HttpParams httpParams = httpClient.getParams();
    HttpConnectionParams.setConnectionTimeout(httpParams, CloudifyConstants.DEFAULT_HTTP_CONNECTION_TIMEOUT);
    HttpConnectionParams.setSoTimeout(httpParams, CloudifyConstants.DEFAULT_HTTP_READ_TIMEOUT);

    RestClientExecutor restClientExecutor = new RestClientExecutor(httpClient, new URL(restUrl));

    try {//from w  w w. j  a v a 2  s  .  co m
        restClientExecutor.get(PlatformVersion.getVersion() + "/deployments/default/services/simple",
                new TypeReference<Response<Void>>() {
                });
    } catch (RestClientException e) {
        String messageCode = e.getMessageCode();
        String expectedMessageCode = CloudifyErrorMessages.UNSUPPORTED_OPERATION.getName();
        Assert.assertTrue("message code [" + messageCode + "] expected to be " + expectedMessageCode,
                expectedMessageCode.equals(messageCode));
        String message = e.getMessageFormattedText();
        Assert.assertTrue("Formatted message [" + message + "] does not to contain " + OPERATION_NAME,
                message.contains(OPERATION_NAME));
    }

}

From source file:net.alchemiestick.katana.winehqappdb.WineApp.java

@Override
protected void onPreExecute() {
    String str = "Getting;App data;from server;";
    this.adapter.clear();
    this.adapter.add(str);
    try {// ww w.ja v  a 2 s .c om
        this.client = AndroidHttpClient.newInstance("WineHQ/1.0 App Browser");
        HttpConnectionParams.setConnectionTimeout(this.client.getParams(), TIMEOUT_MS);
        HttpConnectionParams.setSoTimeout(this.client.getParams(), TIMEOUT_MS);
    } catch (Exception e) {
        str = "Couldn't set Params0;;;";
        this.adapter.add(str);
    }
}

From source file:com.alibaba.akita.io.HttpInvoker.java

/**
 * init/*www . j  ava  2 s  . c  om*/
 */
private static void init() {

    SchemeRegistry schemeRegistry = new SchemeRegistry();
    schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
    schemeRegistry.register(new Scheme("https", _FakeSSLSocketFactory.getSocketFactory(), 443));

    HttpParams params = new BasicHttpParams();
    HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
    HttpProtocolParams.setContentCharset(params, "utf-8");
    HttpConnectionParams.setConnectionTimeout(params, 8000);
    HttpConnectionParams.setSoTimeout(params, 15000);
    params.setBooleanParameter("http.protocol.expect-continue", false);

    connectionManager = new ThreadSafeClientConnManager(params, schemeRegistry);
    client = new DefaultHttpClient(connectionManager, params);

    // enable gzip support in Request and Response. 
    client.addRequestInterceptor(new HttpRequestInterceptor() {
        public void process(final HttpRequest request, final HttpContext context)
                throws HttpException, IOException {
            if (!request.containsHeader("Accept-Encoding")) {
                request.addHeader("Accept-Encoding", "gzip");
            }
        }
    });
    client.addResponseInterceptor(new HttpResponseInterceptor() {
        public void process(final HttpResponse response, final HttpContext context)
                throws HttpException, IOException {
            HttpEntity entity = response.getEntity();
            //Log.i("ContentLength", entity.getContentLength()+"");
            Header ceheader = entity.getContentEncoding();
            if (ceheader != null) {
                HeaderElement[] codecs = ceheader.getElements();
                for (int i = 0; i < codecs.length; i++) {
                    if (codecs[i].getName().equalsIgnoreCase("gzip")) {
                        response.setEntity(new GzipDecompressingEntity(response.getEntity()));
                        return;
                    }
                }
            }
        }
    });

}