Example usage for org.apache.http.params BasicHttpParams BasicHttpParams

List of usage examples for org.apache.http.params BasicHttpParams BasicHttpParams

Introduction

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

Prototype

public BasicHttpParams() 

Source Link

Usage

From source file:playn.android.AndroidNet.java

private void doHttp(final boolean isPost, final String url, final String data,
        final Callback<String> callback) {
    platform.invokeAsync(new Runnable() {
        @Override/*from  w ww .  j  a v  a2 s  . co  m*/
        public void run() {
            HttpParams params = new BasicHttpParams();
            HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);
            HttpProtocolParams.setHttpElementCharset(params, HTTP.UTF_8);
            HttpClient httpclient = new DefaultHttpClient(params);
            HttpRequestBase req = null;
            if (isPost) {
                HttpPost httppost = new HttpPost(url);
                if (data != null) {
                    try {
                        httppost.setEntity(new StringEntity(data, HTTP.UTF_8));
                    } catch (UnsupportedEncodingException e) {
                        platform.notifyFailure(callback, e);
                    }
                }
                req = httppost;
            } else {
                req = new HttpGet(url);
            }
            try {
                HttpResponse response = httpclient.execute(req);
                StatusLine status = response.getStatusLine();
                int code = status.getStatusCode();
                String body = EntityUtils.toString(response.getEntity());
                if (code == HttpStatus.SC_OK) {
                    platform.notifySuccess(callback, body);
                } else {
                    platform.notifyFailure(callback, new HttpException(code, body));
                }
            } catch (Exception e) {
                platform.notifyFailure(callback, e);
            }
        }

        @Override
        public String toString() {
            return "AndroidNet.doHttp(" + isPost + ", " + url + ")";
        }
    });
}

From source file:ro.zg.netcell.connectors.HttpConnectionManager.java

private void initHttpClient() {
    ConfigurationData cfgData = dataSourceDefinition.getConfigData();

    HttpParams params = new BasicHttpParams();
    ConnManagerParams.setMaxTotalConnections(params, Integer
            .parseInt(cfgData.getParameterValue(DataSourceConfigParameters.MAX_TOTAL_CONNECTIONS).toString()));
    ConnPerRouteBean connPerRoute = new ConnPerRouteBean(10);
    HttpHost localhost = new HttpHost("locahost", 80);
    connPerRoute.setMaxForRoute(new HttpRoute(localhost), 50);
    ConnManagerParams.setMaxConnectionsPerRoute(params, connPerRoute);
    ConnManagerParams.setTimeout(params, Long
            .parseLong(cfgData.getParameterValue(DataSourceConfigParameters.CONNECTION_TIMEOUT).toString()));

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

    ClientConnectionManager cm = new ThreadSafeClientConnManager(params, schemeRegistry);

    /* set config params */
    ConfigurationData configData = dataSourceDefinition.getConfigData();
    Map<String, UserInputParameter> userInputParams = configData.getUserInputParams();
    for (UserInputParameter uip : userInputParams.values()) {
        params.setParameter(uip.getInnerName(), uip.getValue());
    }//from   w  w w . j  a va  2 s.com

    HttpConnectionParams.setSoTimeout(params, 25000);
    httpClient = new DefaultHttpClient(cm, params);

}

From source file:com.devbliss.doctest.httpfactory.DeleteWithoutRedirectImpl.java

public HttpDelete createDeleteRequest(URI uri) throws IOException {
    HttpDelete httpDelete = new HttpDelete(uri);
    HttpParams params = new BasicHttpParams();
    params.setParameter(HANDLE_REDIRECTS, false);
    httpDelete.setParams(params);/*from   w ww. j  a v a  2  s  .  com*/
    return httpDelete;
}

From source file:org.dasein.security.joyent.DefaultClientFactory.java

@Override
public @Nonnull HttpClient getClient(String endpoint) throws CloudException, InternalException {
    if (providerContext == null) {
        throw new CloudException("No context was defined for this request");
    }/* w w w .j  ava2s.c  o m*/

    final HttpParams params = new BasicHttpParams();

    HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
    HttpProtocolParams.setContentCharset(params, Consts.UTF_8.toString());
    HttpProtocolParams.setUserAgent(params, "Dasein Cloud");

    Properties p = providerContext.getCustomProperties();
    if (p != null) {
        String proxyHost = p.getProperty("proxyHost");
        String proxyPortStr = p.getProperty("proxyPort");
        int proxyPort = 0;
        if (proxyPortStr != null) {
            proxyPort = Integer.parseInt(proxyPortStr);
        }
        if (proxyHost != null && proxyHost.length() > 0 && proxyPort > 0) {
            params.setParameter(ConnRoutePNames.DEFAULT_PROXY, new HttpHost(proxyHost, proxyPort));
        }
    }
    DefaultHttpClient client = new DefaultHttpClient(params);
    // Joyent does not support gzip at the moment (7.2), but in case it will
    // in the future we might just leave these here
    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");
            }
            request.setParams(params);
        }
    });
    client.addResponseInterceptor(new HttpResponseInterceptor() {
        public void process(final HttpResponse response, final HttpContext context)
                throws HttpException, IOException {
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                Header header = entity.getContentEncoding();
                if (header != null) {
                    for (HeaderElement codec : header.getElements()) {
                        if (codec.getName().equalsIgnoreCase("gzip")) {
                            response.setEntity(new GzipDecompressingEntity(response.getEntity()));
                            break;
                        }
                    }
                }
            }
        }
    });
    return client;
}

From source file:net.oneandone.sushi.fs.webdav.WebdavRoot.java

public WebdavRoot(WebdavFilesystem filesystem, String protocol, String host, int port) {
    this.filesystem = filesystem;
    this.host = new HttpHost(host, port, protocol);
    this.authorization = null;
    this.params = new BasicHttpParams();

    HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
    HttpProtocolParams.setContentCharset(params, WebdavFilesystem.ENCODING);
}

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();/* w w w.  j  a  v  a2  s .  com*/
    }

    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:es.deustotech.piramide.utils.tts.TextToSpeechWeb.java

private static synchronized void speech(final Context context, final String text, final String language) {
    executor.submit(new Runnable() {
        @Override//from  w w  w.j av  a  2  s.co m
        public void run() {
            try {
                final String encodedUrl = Constants.URL + language + "&q="
                        + URLEncoder.encode(text, Encoding.UTF_8.name());
                final DefaultHttpClient client = new DefaultHttpClient();
                HttpParams params = new BasicHttpParams();
                params.setParameter("http.protocol.content-charset", "UTF-8");
                client.setParams(params);
                final FileOutputStream fos = context.openFileOutput(Constants.MP3_FILE,
                        Context.MODE_WORLD_READABLE);
                try {
                    try {
                        final HttpResponse response = client.execute(new HttpGet(encodedUrl));
                        downloadFile(response, fos);
                    } finally {
                        fos.close();
                    }
                    final String filePath = context.getFilesDir().getAbsolutePath() + "/" + Constants.MP3_FILE;
                    final MediaPlayer player = MediaPlayer.create(context.getApplicationContext(),
                            Uri.fromFile(new File(filePath)));
                    player.start();
                    Thread.sleep(player.getDuration());
                    while (player.isPlaying()) {
                        Thread.sleep(100);
                    }
                    player.stop();

                } finally {
                    context.deleteFile(Constants.MP3_FILE);
                }
            } catch (InterruptedException ie) {
                // ok
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

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!");
    }//  www . ja va 2s. co  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:gmusic.api.comm.ApacheConnector.java

public ApacheConnector() {
    HttpParams params = new BasicHttpParams();
    params.removeParameter("User-Agent");
    params.setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BEST_MATCH);
    // HttpConnectionParams.setConnectionTimeout(params, 150000);
    // HttpConnectionParams.setSoTimeout(params, socketTimeoutMillis);
    httpClient = new DefaultHttpClient(params);
    cookieStore = new BasicCookieStore();
    localContext = new BasicHttpContext();
    localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
}

From source file:eu.trentorise.smartcampus.portfolio.utils.HttpClientFactory.java

private final HttpClient createHttpClient() {
    HttpParams httpParams = new BasicHttpParams();
    HttpProtocolParams.setVersion(httpParams, HttpVersion.HTTP_1_1);
    HttpProtocolParams.setContentCharset(httpParams, HTTP.DEFAULT_CONTENT_CHARSET);
    SchemeRegistry schemeRegistry = new SchemeRegistry();
    Scheme httpScheme = new Scheme(HTTP_SCHEMA, PlainSocketFactory.getSocketFactory(), 80);
    schemeRegistry.register(httpScheme);
    Scheme httpsScheme = new Scheme(HTTPS_SCHEMA, SSLSocketFactory.getSocketFactory(), 443);
    schemeRegistry.register(httpsScheme);
    ClientConnectionManager tsConnManager = new ThreadSafeClientConnManager(httpParams, schemeRegistry);
    HttpClient client = new DefaultHttpClient(tsConnManager, httpParams);
    HttpConnectionParams.setSoTimeout(client.getParams(), TIMEOUT);
    HttpConnectionParams.setConnectionTimeout(client.getParams(), TIMEOUT);
    return client;
}