Java源码示例:org.apache.http.conn.params.ConnManagerPNames

示例1
public static HttpClient getClient(int httpPort, int httpsPort) {
    SchemeRegistry schemeRegistry = new SchemeRegistry();
    // http scheme
    schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
    // https scheme
    schemeRegistry.register(new Scheme("https", new EasySSLSocketFactory(), 443));

    HttpParams params = new BasicHttpParams();
    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 ThreadSafeClientConnManager(params, schemeRegistry);
    return new DefaultHttpClient(cm, params);

}
 
示例2
public static HttpClient getTrustedClient(int httpPort, int httpsPort, File keystore, String keypass) {
    SchemeRegistry schemeRegistry = new SchemeRegistry();
    // http scheme
    schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), httpPort));
    // https scheme
    schemeRegistry.register(new Scheme("https", new EasySSLSocketFactory(), httpsPort));

    HttpParams params = new BasicHttpParams();
    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 ThreadSafeClientConnManager(params, schemeRegistry);
    return new DispatchHttpClient(keystore, keypass);
}
 
示例3
/**
 * prepare for the https connection
 * call this in the constructor of the class that does the connection if
 * it's used multiple times
 */
private void setup() {
    SchemeRegistry schemeRegistry = new SchemeRegistry();

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

    params = new BasicHttpParams();
    params.setParameter(ConnManagerPNames.MAX_TOTAL_CONNECTIONS, 1);
    params.setParameter(ConnManagerPNames.MAX_CONNECTIONS_PER_ROUTE, new ConnPerRouteBean(1));
    params.setParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, false);
    HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
    HttpProtocolParams.setContentCharset(params, "utf8");

    CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
    // set the user credentials for our site "example.com"
    credentialsProvider.setCredentials(new AuthScope("example.com", AuthScope.ANY_PORT),
            new UsernamePasswordCredentials("UserNameHere", "UserPasswordHere"));
    clientConnectionManager = new ThreadSafeClientConnManager(params, schemeRegistry);

    context = new BasicHttpContext();
    context.setAttribute("http.auth.credentials-provider", credentialsProvider);
}
 
示例4
private static ClientConnectionManager createClientConnectionManager() {
    HttpParams params = new BasicHttpParams();
    params.setIntParameter(ConnManagerPNames.MAX_TOTAL_CONNECTIONS, MAX_CONNECTIONS);
    params.setParameter(ConnManagerParams.MAX_CONNECTIONS_PER_ROUTE, new ConnPerRoute() {
        @Override
        public int getMaxForRoute(HttpRoute httpRoute) {
            return MAX_CONNECTIONS_PER_ROUTE;
        }
    });
    return new ThreadSafeClientConnManager(params, prepareSchemeRegistry());
}
 
示例5
/**
 * 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.
 * @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;
}