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:cn.edu.szjm.support.http.IgnitedHttp.java

protected void setupHttpClient() {
    BasicHttpParams httpParams = new BasicHttpParams();

    ConnManagerParams.setTimeout(httpParams, DEFAULT_WAIT_FOR_CONNECTION_TIMEOUT);
    ConnManagerParams.setMaxConnectionsPerRoute(httpParams, new ConnPerRouteBean(DEFAULT_MAX_CONNECTIONS));
    ConnManagerParams.setMaxTotalConnections(httpParams, DEFAULT_MAX_CONNECTIONS);
    HttpConnectionParams.setSoTimeout(httpParams, DEFAULT_SOCKET_TIMEOUT);
    HttpConnectionParams.setTcpNoDelay(httpParams, true);
    HttpProtocolParams.setVersion(httpParams, HttpVersion.HTTP_1_1);
    HttpProtocolParams.setUserAgent(httpParams, DEFAULT_HTTP_USER_AGENT);

    SchemeRegistry schemeRegistry = new SchemeRegistry();
    schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
    if (IgnitedDiagnostics.ANDROID_API_LEVEL >= 7) {
        schemeRegistry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));
    } else {/*from w w  w.  j av a2 s .co  m*/
        // used to work around a bug in Android 1.6:
        // http://code.google.com/p/android/issues/detail?id=1946
        // TODO: is there a less rigorous workaround for this?
        schemeRegistry.register(new Scheme("https", new EasySSLSocketFactory(), 443));
    }

    ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager(httpParams, schemeRegistry);
    httpClient = new DefaultHttpClient(cm, httpParams);
}

From source file:com.personalserver.HttpThread.java

public void run() {
    DefaultHttpServerConnection httpServer = new DefaultHttpServerConnection();
    try {/*from  w  ww.j  a  v a 2 s .com*/
        httpServer.bind(mSocket, new BasicHttpParams());
        mHttpService.handleRequest(httpServer, mHttpContext);
    } catch (IOException e) {
        System.err.println("Exception in HttpThread.java:can't bind");
        e.printStackTrace();
    } catch (HttpException e) {
        System.err.println("Exception in HttpThread.java:handle request");
        e.printStackTrace();
    } catch (Exception exce) {
        System.err.println("debug : error again !");
        System.err.println(exce.getMessage());
        exce.printStackTrace();
    } finally {
        try {
            httpServer.close();
        } catch (IOException e) {
            System.err.println("Excetion in HttpThread.java:can't shutdown");
            e.printStackTrace();
        }
    }
}

From source file:eu.sisob.uma.api.crawler4j.crawler.PageFetcher.java

public synchronized static void startConnectionMonitorThread() {
    if (connectionMonitorThread == null) {
        HttpParams params = new BasicHttpParams();
        HttpProtocolParamBean paramsBean = new HttpProtocolParamBean(params);
        paramsBean.setVersion(HttpVersion.HTTP_1_1);
        paramsBean.setContentCharset("UTF-8");
        paramsBean.setUseExpectContinue(false);

        params.setParameter("http.useragent", Configurations.getStringProperty("fetcher.user_agent",
                "crawler4j (http://code.google.com/p/crawler4j/)"));

        params.setIntParameter("http.socket.timeout",
                Configurations.getIntProperty("fetcher.socket_timeout", 20000));

        params.setIntParameter("http.connection.timeout",
                Configurations.getIntProperty("fetcher.connection_timeout", 30000));

        params.setBooleanParameter("http.protocol.handle-redirects", false);

        ConnPerRouteBean connPerRouteBean = new ConnPerRouteBean();
        connPerRouteBean/*from www.  ja v a 2 s  .  c  om*/
                .setDefaultMaxPerRoute(Configurations.getIntProperty("fetcher.max_connections_per_host", 100));
        ConnManagerParams.setMaxConnectionsPerRoute(params, connPerRouteBean);
        ConnManagerParams.setMaxTotalConnections(params,
                Configurations.getIntProperty("fetcher.max_total_connections", 100));

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

        if (Configurations.getBooleanProperty("fetcher.crawl_https", false)) {
            schemeRegistry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));
        }

        connectionManager = new ThreadSafeClientConnManager(params, schemeRegistry);

        ProjectLogger.LOGGER.setLevel(Level.INFO);
        httpclient = new DefaultHttpClient(connectionManager, params);
        connectionMonitorThread = new IdleConnectionMonitorThread(connectionManager);
    }
    connectionMonitorThread.start();
}

From source file:org.sbs.goodcrawler.fetcher.PageFetcher.java

public PageFetcher(FetchConfig config) {
    super(config);
    HttpParams params = new BasicHttpParams();
    HttpProtocolParamBean paramsBean = new HttpProtocolParamBean(params);
    paramsBean.setVersion(HttpVersion.HTTP_1_1);
    paramsBean.setContentCharset("UTF-8");
    paramsBean.setUseExpectContinue(false);

    params.setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY);
    params.setParameter(CoreProtocolPNames.USER_AGENT, config.getAgent());
    params.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, config.getSocketTimeoutMilliseconds());
    params.setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, config.getConnectionTimeout());

    params.setBooleanParameter("http.protocol.handle-redirects", false);

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

    if (config.isHttps()) {
        schemeRegistry.register(new Scheme("https", 443, SSLSocketFactory.getSocketFactory()));
    }/*from  w  w  w.j a va2s.c om*/

    connectionManager = new PoolingClientConnectionManager(schemeRegistry);
    connectionManager.setMaxTotal(config.getMaxTotalConnections());
    connectionManager.setDefaultMaxPerRoute(config.getMaxConnectionsPerHost());
    httpClient = new DefaultHttpClient(connectionManager, params);

    if (config.getProxyHost() != null) {

        if (config.getProxyUsername() != null) {
            httpClient.getCredentialsProvider().setCredentials(
                    new AuthScope(config.getProxyHost(), config.getProxyPort()),
                    new UsernamePasswordCredentials(config.getProxyUsername(), config.getProxyPassword()));
        }

        HttpHost proxy = new HttpHost(config.getProxyHost(), config.getProxyPort());
        httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
    }

    httpClient.addResponseInterceptor(new HttpResponseInterceptor() {

        @Override
        public void process(final HttpResponse response, final HttpContext context)
                throws HttpException, IOException {
            HttpEntity entity = response.getEntity();
            Header contentEncoding = entity.getContentEncoding();
            if (contentEncoding != null) {
                HeaderElement[] codecs = contentEncoding.getElements();
                for (HeaderElement codec : codecs) {
                    if (codec.getName().equalsIgnoreCase("gzip")) {
                        response.setEntity(new GzipDecompressingEntity(response.getEntity()));
                        return;
                    }
                }
            }
        }

    });

    if (connectionMonitorThread == null) {
        connectionMonitorThread = new IdleConnectionMonitorThread(connectionManager);
    }
    connectionMonitorThread.start();

}

From source file:com.ihongqiqu.util.LocationUtils.java

/**
 * ????/*from   w  ww  .j a v a2  s . c o  m*/
 *
 * @param longitude ?
 * @param latitude  
 * @param lang       ?en
 * @return ??
 * @throws Exception
 */
public static String getAddress(double longitude, double latitude, String lang) throws Exception {
    if (DEBUG) {
        LogUtils.d(TAG, "location : (" + longitude + "," + latitude + ")");
    }
    if (lang == null) {
        lang = "en";
    }
    // 
    HttpParams params = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(params, 10 * 1000);
    HttpConnectionParams.setSoTimeout(params, 10 * 1000);
    // HttpClient????
    HttpClient client = new DefaultHttpClient(params);
    // ????GET
    HttpGet httpGet = new HttpGet("https://maps.googleapis.com/maps/api/" + "geocode/json?latlng=" + latitude
            + "," + longitude + "&sensor=false&language=" + lang);
    if (DEBUG) {
        LogUtils.d(TAG, "URL : " + httpGet.getURI());
    }
    StringBuilder sb = new StringBuilder();
    // 
    HttpResponse response = client.execute(httpGet);
    HttpEntity entity = response.getEntity();
    // ???
    InputStream stream = entity.getContent();
    int b;
    while ((b = stream.read()) != -1) {
        sb.append((char) b);
    }
    // ??JSONObject
    JSONObject jsonObj = new JSONObject(sb.toString());
    Log.d("ConvertUtil", "getAddress:" + sb.toString());
    // ????
    JSONObject addressObject = jsonObj.getJSONArray("results").getJSONObject(0);
    String address = decodeLocationName(addressObject);
    if (DEBUG) {
        LogUtils.d(TAG, "address : " + address);
    }
    return address;
}

From source file:Main.java

public static DefaultHttpClient setHttpPostProxyParams() {

    Log.d(TAG, "() default called ");

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

    HttpParams params = new BasicHttpParams();

    //Log.d(TAG, "----Add Proxy---");
    /*HttpHost proxy = new HttpHost(Constants.PROXY_HOST.trim(),
      Constants.PROXY_PORT);//www.  j av a2 s .c om
    params.setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);*/

    params.setParameter(ConnManagerPNames.MAX_TOTAL_CONNECTIONS, 30);
    params.setParameter(ConnManagerPNames.MAX_CONNECTIONS_PER_ROUTE, new ConnPerRouteBean(30));
    params.setParameter(HttpProtocolParams.USE_EXPECT_CONTINUE, false);
    HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);

    ClientConnectionManager cm = new SingleClientConnManager(params, schemeRegistry);

    DefaultHttpClient mHttpClient = new DefaultHttpClient(cm, params);

    return mHttpClient;
}

From source file:org.silvertunnel_ng.demo.download_tool.ApacheHttpComponentsClient.java

/**
 * Initialize HttpComponents Client//  ww  w .j av  a 2s .  c o m
 * 
 * @param lowerNetLayer    TCP/IP compatible layer;
 *                         layer for SSL/TLS/https connections
 *                         will be created inside this class
 *                         and may not be passed as argument here 
 */
public ApacheHttpComponentsClient(NetLayer lowerNetLayer) {
    // register the "http" protocol scheme, it is required
    // by the default operator to look up socket factories.
    SchemeRegistry schemeRegistry = new SchemeRegistry();
    final int DEFAULT_PORT_HTTP = 80;
    schemeRegistry.register(new Scheme("http", new NetlibSocketFactory(lowerNetLayer), DEFAULT_PORT_HTTP));

    /* TODO - DOES CURRENTLY NOT WORK: register the "https" protocol scheme
    final int DEFAULT_PORT_HTTPS = 443;
    NetLayer httpsLowerNetLayer = new TLSNetLayer(lowerNetLayer);  
    schemeRegistry.register(new Scheme("https", new NetlibSocketFactory(httpsLowerNetLayer), DEFAULT_PORT_HTTPS));
    */

    // set http(s) client parameters
    HttpParams params = new BasicHttpParams();
    HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
    HttpProtocolParams.setContentCharset(params, "UTF-8");
    if (USER_AGENT != null) {
        HttpProtocolParams.setUserAgent(params, USER_AGENT);
    }

    // create http(s) client
    ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, schemeRegistry);
    httpClient = new DefaultHttpClient(ccm, params);

    log.info("HttpComponents Client/httpClient initialized=" + httpClient.toString());
}

From source file:com.windigo.http.client.ApacheHttpClient.java

/**
 * Create {@link HttpParams} for {@link DefaultHttpClient}
 * //ww w. ja v a  2 s .co  m
 * @return {@link HttpParams}
 */
private final static HttpParams createHttpParams() {
    final HttpParams params = new BasicHttpParams();

    HttpConnectionParams.setStaleCheckingEnabled(params, false);
    HttpConnectionParams.setConnectionTimeout(params, GlobalSettings.CONNNECTION_TIMEOUT);
    HttpConnectionParams.setSoTimeout(params, GlobalSettings.CONNNECTION_TIMEOUT);
    HttpConnectionParams.setSocketBufferSize(params, 8192);

    return params;
}

From source file:com.antorofdev.util.Http.java

/**
 * Performs http GET petition to server.
 *
 * @param url        URL to perform GET petition.
 * @param parameters Parameters to include in petition.
 *
 * @return Response from the server.//from www .  j  a  va  2  s  .  c  om
 * @throws IOException If the <tt>parameters</tt> have errors, connection timmed out,
 *                     socket timmed out or other error related with the connection occurs.
 */
public static HttpResponse get(String url, ArrayList<NameValuePair> parameters) throws IOException {
    HttpParams httpParameters = new BasicHttpParams();
    int timeoutConnection = 10000;
    HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
    int timeoutSocket = 10000;
    HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);

    HttpClient httpclient = new DefaultHttpClient(httpParameters);

    if (parameters != null) {
        String paramString = URLEncodedUtils.format(parameters, "utf-8");
        url += paramString;
    }

    HttpGet httpget = new HttpGet(url);

    HttpResponse response = httpclient.execute(httpget);

    return response;
}

From source file:org.fashiontec.bodyapps.sync.Sync.java

/**
 * Manages get requests./*from   w  w  w. ja  v a  2 s  . c  o m*/
 *
 * @param url
 * @param conTimeOut
 * @param socTimeOut
 * @return
 */
public HttpResponse get(String url, int conTimeOut, int socTimeOut) {
    HttpResponse response = null;
    try {
        HttpParams httpParameters = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(httpParameters, conTimeOut);
        HttpConnectionParams.setSoTimeout(httpParameters, socTimeOut);
        HttpClient client = new DefaultHttpClient(httpParameters);
        HttpGet request = new HttpGet(url);
        request.setHeader("Accept", "application/json");
        response = client.execute(request);
    } catch (Exception e) {
        Log.e(TAG, e.getMessage());
    }
    return response;

}