Example usage for org.apache.http.params HttpParams setLongParameter

List of usage examples for org.apache.http.params HttpParams setLongParameter

Introduction

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

Prototype

HttpParams setLongParameter(String str, long j);

Source Link

Usage

From source file:Main.java

public static HttpClient getHttpClient() {

    if (httpClient != null) {
        return httpClient;
    }// w  w w.  j ava2s . c  o m

    synchronized (HTTP_CLIENT) {
        HttpParams defaultParams = new BasicHttpParams();
        defaultParams.setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 6000);
        defaultParams.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, 6000);
        defaultParams.setLongParameter(ConnManagerPNames.TIMEOUT, 6000);

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

        ClientConnectionManager connMgr = new ThreadSafeClientConnManager(defaultParams, scheme);

        httpClient = new DefaultHttpClient(connMgr, defaultParams);
    }

    return httpClient;
}

From source file:com.google.apphosting.vmruntime.VmApiProxyDelegate.java

/**
 * Create an HTTP post request suitable for sending to the API server.
 *
 * @param environment The current VMApiProxyEnvironment
 * @param packageName The API call package
 * @param methodName The API call method
 * @param requestData The POST payload./*  ww  w  .j a v a  2  s.c o  m*/
 * @param timeoutMs The timeout for this request
 * @return an HttpPost object to send to the API.
 */
// 
static HttpPost createRequest(VmApiProxyEnvironment environment, String packageName, String methodName,
        byte[] requestData, int timeoutMs) {
    // Wrap the payload in a RemoteApi Request.
    RemoteApiPb.Request remoteRequest = new RemoteApiPb.Request();
    remoteRequest.setServiceName(packageName);
    remoteRequest.setMethod(methodName);
    remoteRequest.setRequestId(environment.getTicket());
    remoteRequest.setRequestAsBytes(requestData);

    HttpPost request = new HttpPost("http://" + environment.getServer() + REQUEST_ENDPOINT);
    request.setHeader(RPC_STUB_ID_HEADER, REQUEST_STUB_ID);
    request.setHeader(RPC_METHOD_HEADER, REQUEST_STUB_METHOD);

    // Set TCP connection timeouts.
    HttpParams params = new BasicHttpParams();
    params.setLongParameter(ConnManagerPNames.TIMEOUT, timeoutMs + ADDITIONAL_HTTP_TIMEOUT_BUFFER_MS);
    params.setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT,
            timeoutMs + ADDITIONAL_HTTP_TIMEOUT_BUFFER_MS);
    params.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, timeoutMs + ADDITIONAL_HTTP_TIMEOUT_BUFFER_MS);

    // Performance tweaks.
    params.setBooleanParameter(CoreConnectionPNames.TCP_NODELAY, Boolean.TRUE);
    params.setBooleanParameter(CoreConnectionPNames.STALE_CONNECTION_CHECK, Boolean.FALSE);
    request.setParams(params);

    // The request deadline can be overwritten by the environment, read deadline if available.
    Double deadline = (Double) (environment.getAttributes().get(API_DEADLINE_KEY));
    if (deadline == null) {
        request.setHeader(RPC_DEADLINE_HEADER,
                Double.toString(TimeUnit.SECONDS.convert(timeoutMs, TimeUnit.MILLISECONDS)));
    } else {
        request.setHeader(RPC_DEADLINE_HEADER, Double.toString(deadline));
    }

    // If the incoming request has a dapper trace header: set it on outgoing API calls
    // so they are tied to the original request.
    Object dapperHeader = environment.getAttributes()
            .get(VmApiProxyEnvironment.AttributeMapping.DAPPER_ID.attributeKey);
    if (dapperHeader instanceof String) {
        request.setHeader(VmApiProxyEnvironment.AttributeMapping.DAPPER_ID.headerKey, (String) dapperHeader);
    }

    // If the incoming request has a Cloud trace header: set it on outgoing API calls
    // so they are tied to the original request.
    // TODO(user): For now, this uses the incoming span id - use the one from the active span.
    Object traceHeader = environment.getAttributes()
            .get(VmApiProxyEnvironment.AttributeMapping.CLOUD_TRACE_CONTEXT.attributeKey);
    if (traceHeader instanceof String) {
        request.setHeader(VmApiProxyEnvironment.AttributeMapping.CLOUD_TRACE_CONTEXT.headerKey,
                (String) traceHeader);
    }

    ByteArrayEntity postPayload = new ByteArrayEntity(remoteRequest.toByteArray(),
            ContentType.APPLICATION_OCTET_STREAM);
    postPayload.setChunked(false);
    request.setEntity(postPayload);

    return request;
}

From source file:outfox.dict.contest.util.HttpToolKit.java

/**
 * @param maxConnectPerHost/*  w ww.  j a va 2  s .  c  o  m*/
 * @param maxConnection
 * @param connectTimeOut
 * @param socketTimeOut
 * @param cookiePolicy
 * @param isAutoRetry
 * @param redirect
 */
public HttpToolKit(int maxConnectPerHost, int maxConnection, int connectTimeOut, int socketTimeOut,
        String cookiePolicy, boolean isAutoRetry, boolean redirect) {
    Scheme https = new Scheme("https", 443, SSLSocketFactory.getSocketFactory());
    Scheme http = new Scheme("http", 80, PlainSocketFactory.getSocketFactory());
    SchemeRegistry sr = new SchemeRegistry();
    sr.register(https);
    sr.register(http);

    connectionManager = new PoolingClientConnectionManager(sr, socketTimeOut, TimeUnit.MILLISECONDS);
    connectionManager.setDefaultMaxPerRoute(maxConnectPerHost);
    connectionManager.setMaxTotal(maxConnection);
    HttpParams params = new BasicHttpParams();
    params.setLongParameter(ClientPNames.CONN_MANAGER_TIMEOUT, connectTimeOut);
    params.setParameter(ClientPNames.COOKIE_POLICY, cookiePolicy);
    params.setBooleanParameter(ClientPNames.HANDLE_REDIRECTS, redirect);
    params.setBooleanParameter(ClientPNames.ALLOW_CIRCULAR_REDIRECTS, false);

    if (isAutoRetry) {
        client = new AutoRetryHttpClient(new DefaultHttpClient(connectionManager, params));
    } else {
        client = new DefaultHttpClient(connectionManager, params);
    }
}

From source file:com.android.stockbrowser.search.OpenSearchSearchEngine.java

public OpenSearchSearchEngine(Context context, SearchEngineInfo searchEngineInfo) {
    mSearchEngineInfo = searchEngineInfo;
    mHttpClient = AndroidHttpClient.newInstance(USER_AGENT);
    HttpParams params = mHttpClient.getParams();
    params.setLongParameter(HTTP_TIMEOUT, HTTP_TIMEOUT_MS);
}

From source file:com.android.googlesearch.SuggestionProvider.java

@Override
public boolean onCreate() {
    mHttpClient = new GoogleHttpClient(getContext(), USER_AGENT, false /* not gzip capable */);
    HttpParams params = mHttpClient.getParams();
    params.setLongParameter(HTTP_TIMEOUT, HTTP_TIMEOUT_MS);

    // NOTE:  Do not look up the resource here;  Localization changes may not have completed
    // yet (e.g. we may still be reading the SIM card).
    mSuggestUri = null;// w w w .  j a  va  2  s .  co  m
    return true;
}

From source file:com.android.quicksearchbox.google.GoogleSuggestionProvider.java

@Override
public boolean onCreate() {
    mHttpClient = AndroidHttpClient.newInstance(USER_AGENT, getContext());
    HttpParams params = mHttpClient.getParams();
    params.setLongParameter(HTTP_TIMEOUT, HTTP_TIMEOUT_MS);

    // NOTE:  Do not look up the resource here;  Localization changes may not have completed
    // yet (e.g. we may still be reading the SIM card).
    mSuggestUri = null;//w ww  .  j a v  a  2s.  c o m
    return true;
}

From source file:com.android.quicksearchbox.google.GoogleSuggestClient.java

public GoogleSuggestClient(Context context) {
    super(context);
    mContext = context;/*from  w ww . j  av  a2  s .co m*/
    mHttpClient = new DefaultHttpClient();
    HttpParams params = mHttpClient.getParams();
    HttpProtocolParams.setUserAgent(params, USER_AGENT);
    params.setLongParameter(HTTP_TIMEOUT, HTTP_TIMEOUT_MS);

    // NOTE:  Do not look up the resource here;  Localization changes may not have completed
    // yet (e.g. we may still be reading the SIM card).
    mSuggestUri = null;
}

From source file:org.jasig.portlet.newsreader.adapter.RomeAdapter.java

public void init() throws Exception {
    final HttpParams params = httpClient.getParams();
    params.setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, connectionTimeout);
    params.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, readTimeout);
    params.setLongParameter(ClientPNames.CONN_MANAGER_TIMEOUT, connectionManagerTimeout);

    httpClient.setHttpRequestRetryHandler(new RetryHandler());

    String proxyHost = null;//  w  ww. j ava2 s .  co m
    String proxyPort = null;

    if (StringUtils.isBlank(this.proxyHost) && StringUtils.isBlank(this.proxyPort)) {
        log.trace("Checking for proxy configuration from system properties...");
        proxyHost = System.getProperty("http.proxyHost");
        proxyPort = System.getProperty("http.proxyPort");
        if (StringUtils.isNotBlank(proxyHost) && StringUtils.isNotBlank(proxyPort)) {
            log.debug("Found proxy configuration from system properties");
        }
    } else {
        log.debug("Using proxy settings from fields set during bean construction");
        proxyHost = this.proxyHost;
        proxyPort = this.proxyPort;
    }

    if (!StringUtils.isBlank(proxyHost) && !StringUtils.isBlank(proxyPort)) {
        HttpHost proxy = new HttpHost(proxyHost, Integer.valueOf(proxyPort));
        httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
        log.debug("Using proxy configuration to retrieve news feeds: " + proxyHost + ":" + proxyPort);
    } else {
        log.debug("No proxy configuration is set. Proceeding normally...");
    }

    // Spring configuration prevents us from using type AbstractHttpClient because we are wrapping the HTTP Client
    // with DecompressingHttpClient which only implements HttpClient, so sadly we're getting around it with a
    // second field.
    compressingClient = new DecompressingHttpClient(httpClient);
}