本文整理匯總了Java中org.apache.http.conn.routing.HttpRoute類的典型用法代碼示例。如果您正苦於以下問題:Java HttpRoute類的具體用法?Java HttpRoute怎麽用?Java HttpRoute使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
HttpRoute類屬於org.apache.http.conn.routing包,在下文中一共展示了HttpRoute類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: test
import org.apache.http.conn.routing.HttpRoute; //導入依賴的package包/類
public static void test(){
PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
// Increase max total connection to 200
cm.setMaxTotal(200);
// Increase default max connection per route to 20
cm.setDefaultMaxPerRoute(20);
// Increase max connections for localhost:80 to 50
HttpHost localhost = new HttpHost("http://cc.0071515.com", 80);
cm.setMaxPerRoute(new HttpRoute(localhost), 2);
// CloseableHttpClient httpClient = HttpClients.custom()
// .setConnectionManager(cm)
// .build();
httpClient = HttpClients.custom()
.setConnectionManager(cm)
.build();
}
示例2: requestConnection
import org.apache.http.conn.routing.HttpRoute; //導入依賴的package包/類
public final ClientConnectionRequest requestConnection(
final HttpRoute route,
final Object state) {
return new ClientConnectionRequest() {
public void abortRequest() {
// Nothing to abort, since requests are immediate.
}
public ManagedClientConnection getConnection(
long timeout, TimeUnit tunit) {
return BasicClientConnectionManager.this.getConnection(
route, state);
}
};
}
示例3: requestConnection
import org.apache.http.conn.routing.HttpRoute; //導入依賴的package包/類
public final ClientConnectionRequest requestConnection(
final HttpRoute route,
final Object state) {
return new ClientConnectionRequest() {
public void abortRequest() {
// Nothing to abort, since requests are immediate.
}
public ManagedClientConnection getConnection(
long timeout, TimeUnit tunit) {
return SingleClientConnManager.this.getConnection(
route, state);
}
};
}
示例4: requestConnection
import org.apache.http.conn.routing.HttpRoute; //導入依賴的package包/類
public ClientConnectionRequest requestConnection(
final HttpRoute route,
final Object state) {
if (route == null) {
throw new IllegalArgumentException("HTTP route may not be null");
}
if (this.log.isDebugEnabled()) {
this.log.debug("Connection request: " + format(route, state) + formatStats(route));
}
final Future<HttpPoolEntry> future = this.pool.lease(route, state);
return new ClientConnectionRequest() {
public void abortRequest() {
future.cancel(true);
}
public ManagedClientConnection getConnection(
final long timeout,
final TimeUnit tunit) throws InterruptedException, ConnectionPoolTimeoutException {
return leaseConnection(future, timeout, tunit);
}
};
}
示例5: getRoutePool
import org.apache.http.conn.routing.HttpRoute; //導入依賴的package包/類
/**
* Get a route-specific pool of available connections.
*
* @param route the route
* @param create whether to create the pool if it doesn't exist
*
* @return the pool for the argument route,
* never <code>null</code> if <code>create</code> is <code>true</code>
*/
protected RouteSpecificPool getRoutePool(HttpRoute route,
boolean create) {
RouteSpecificPool rospl = null;
poolLock.lock();
try {
rospl = routeToPool.get(route);
if ((rospl == null) && create) {
// no pool for this route yet (or anymore)
rospl = newRouteSpecificPool(route);
routeToPool.put(route, rospl);
}
} finally {
poolLock.unlock();
}
return rospl;
}
示例6: deleteEntry
import org.apache.http.conn.routing.HttpRoute; //導入依賴的package包/類
/**
* Deletes a given pool entry.
* This closes the pooled connection and removes all references,
* so that it can be GCed.
*
* <p><b>Note:</b> Does not remove the entry from the freeConnections list.
* It is assumed that the caller has already handled this step.</p>
* <!-- @@@ is that a good idea? or rather fix it? -->
*
* @param entry the pool entry for the connection to delete
*/
protected void deleteEntry(BasicPoolEntry entry) {
HttpRoute route = entry.getPlannedRoute();
if (log.isDebugEnabled()) {
log.debug("Deleting connection"
+ " [" + route + "][" + entry.getState() + "]");
}
poolLock.lock();
try {
closeConnection(entry);
RouteSpecificPool rospl = getRoutePool(route, true);
rospl.deleteEntry(entry);
numConnections--;
if (rospl.isUnused()) {
routeToPool.remove(route);
}
} finally {
poolLock.unlock();
}
}
示例7: handleLostEntry
import org.apache.http.conn.routing.HttpRoute; //導入依賴的package包/類
@Override
protected void handleLostEntry(HttpRoute route) {
poolLock.lock();
try {
RouteSpecificPool rospl = getRoutePool(route, true);
rospl.dropEntry();
if (rospl.isUnused()) {
routeToPool.remove(route);
}
numConnections--;
notifyWaitingThread(rospl);
} finally {
poolLock.unlock();
}
}
示例8: connect
import org.apache.http.conn.routing.HttpRoute; //導入依賴的package包/類
@Override
public void connect(final HttpClientConnection conn, final HttpRoute route,
final int connectTimeout, final HttpContext context) throws IOException {
try {
super.connect(conn, route, connectTimeout, context);
} catch (SSLProtocolException e) {
Boolean enableSniValue =
(Boolean) context.getAttribute(SniSSLConnectionSocketFactory.ENABLE_SNI);
boolean enableSni = enableSniValue == null || enableSniValue;
if (enableSni && e.getMessage() != null &&
e.getMessage().equals("handshake alert: unrecognized_name")) {
logger.warn("Server saw wrong SNI host, retrying without SNI");
context.setAttribute(SniSSLConnectionSocketFactory.ENABLE_SNI, false);
super.connect(conn, route, connectTimeout, context);
} else {
throw e;
}
}
}
示例9: execute
import org.apache.http.conn.routing.HttpRoute; //導入依賴的package包/類
@Override
public CloseableHttpResponse execute(HttpRoute route, HttpRequestWrapper request, HttpClientContext clientContext,
HttpExecutionAware execAware) throws IOException, HttpException {
Proxy proxy = (Proxy) clientContext.getAttribute(VSCrawlerConstant.VSCRAWLER_AVPROXY_KEY);
if (proxy != null) {
proxy.recordUsage();
}
try {
return delegate.execute(route, request, clientContext, execAware);
} catch (IOException ioe) {
if (proxy != null) {
proxy.recordFailed();
}
throw ioe;
}
}
示例10: buildHttpClient
import org.apache.http.conn.routing.HttpRoute; //導入依賴的package包/類
/**
* Build a HTTP client based on the current properties.
*
* @return the built HTTP client
*/
private CloseableHttpClient buildHttpClient() {
try {
final ConnectionSocketFactory plainsf = PlainConnectionSocketFactory.getSocketFactory();
final LayeredConnectionSocketFactory sslsf = this.sslSocketFactory;
final Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
.register("http", plainsf)
.register("https", sslsf)
.build();
final PoolingHttpClientConnectionManager connMgmr = new PoolingHttpClientConnectionManager(registry);
connMgmr.setMaxTotal(this.maxPooledConnections);
connMgmr.setDefaultMaxPerRoute(this.maxConnectionsPerRoute);
connMgmr.setValidateAfterInactivity(DEFAULT_TIMEOUT);
final HttpHost httpHost = new HttpHost(InetAddress.getLocalHost());
final HttpRoute httpRoute = new HttpRoute(httpHost);
connMgmr.setMaxPerRoute(httpRoute, MAX_CONNECTIONS_PER_ROUTE);
final RequestConfig requestConfig = RequestConfig.custom()
.setSocketTimeout(this.readTimeout)
.setConnectTimeout(this.connectionTimeout)
.setConnectionRequestTimeout(this.connectionTimeout)
.setCircularRedirectsAllowed(this.circularRedirectsAllowed)
.setRedirectsEnabled(this.redirectsEnabled)
.setAuthenticationEnabled(this.authenticationEnabled)
.build();
final HttpClientBuilder builder = HttpClients.custom()
.setConnectionManager(connMgmr)
.setDefaultRequestConfig(requestConfig)
.setSSLSocketFactory(sslsf)
.setSSLHostnameVerifier(this.hostnameVerifier)
.setRedirectStrategy(this.redirectionStrategy)
.setDefaultCredentialsProvider(this.credentialsProvider)
.setDefaultCookieStore(this.cookieStore)
.setConnectionReuseStrategy(this.connectionReuseStrategy)
.setConnectionBackoffStrategy(this.connectionBackoffStrategy)
.setServiceUnavailableRetryStrategy(this.serviceUnavailableRetryStrategy)
.setProxyAuthenticationStrategy(this.proxyAuthenticationStrategy)
.setDefaultHeaders(this.defaultHeaders)
.useSystemProperties();
return builder.build();
} catch (final Exception e) {
LOGGER.error(e.getMessage(), e);
throw new RuntimeException(e);
}
}
示例11: buildHttpClient
import org.apache.http.conn.routing.HttpRoute; //導入依賴的package包/類
/**
* Build a HTTP client based on the current properties.
*
* @return the built HTTP client
*/
private CloseableHttpClient buildHttpClient() {
try {
final ConnectionSocketFactory plainsf = PlainConnectionSocketFactory.getSocketFactory();
final LayeredConnectionSocketFactory sslsf = this.sslSocketFactory;
final Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
.register("http", plainsf)
.register("https", sslsf)
.build();
final PoolingHttpClientConnectionManager connMgmr = new PoolingHttpClientConnectionManager(registry);
connMgmr.setMaxTotal(this.maxPooledConnections);
connMgmr.setDefaultMaxPerRoute(this.maxConnectionsPerRoute);
connMgmr.setValidateAfterInactivity(DEFAULT_TIMEOUT);
final HttpHost httpHost = new HttpHost(InetAddress.getLocalHost());
final HttpRoute httpRoute = new HttpRoute(httpHost);
connMgmr.setMaxPerRoute(httpRoute, MAX_CONNECTIONS_PER_ROUTE);
final RequestConfig requestConfig = RequestConfig.custom()
.setSocketTimeout(this.readTimeout)
.setConnectTimeout(Long.valueOf(this.connectionTimeout).intValue())
.setConnectionRequestTimeout(Long.valueOf(this.connectionTimeout).intValue())
.setCircularRedirectsAllowed(this.circularRedirectsAllowed)
.setRedirectsEnabled(this.redirectsEnabled)
.setAuthenticationEnabled(this.authenticationEnabled)
.build();
final HttpClientBuilder builder = HttpClients.custom()
.setConnectionManager(connMgmr)
.setDefaultRequestConfig(requestConfig)
.setSSLSocketFactory(sslsf)
.setSSLHostnameVerifier(this.hostnameVerifier)
.setRedirectStrategy(this.redirectionStrategy)
.setDefaultCredentialsProvider(this.credentialsProvider)
.setDefaultCookieStore(this.cookieStore)
.setConnectionReuseStrategy(this.connectionReuseStrategy)
.setConnectionBackoffStrategy(this.connectionBackoffStrategy)
.setServiceUnavailableRetryStrategy(this.serviceUnavailableRetryStrategy)
.setProxyAuthenticationStrategy(this.proxyAuthenticationStrategy)
.setDefaultHeaders(this.defaultHeaders)
.useSystemProperties();
return builder.build();
} catch (final Exception e) {
LOGGER.error(e.getMessage(), e);
throw Throwables.propagate(e);
}
}
示例12: createHttpClient
import org.apache.http.conn.routing.HttpRoute; //導入依賴的package包/類
private static CloseableHttpClient createHttpClient(int maxTotal, int maxPerRoute, int maxRoute, String hostname, int port) {
ConnectionSocketFactory plainsf = PlainConnectionSocketFactory
.getSocketFactory();
LayeredConnectionSocketFactory sslsf = SSLConnectionSocketFactory
.getSocketFactory();
Registry<ConnectionSocketFactory> registry = RegistryBuilder
.<ConnectionSocketFactory>create()
.register("http", plainsf)
.register("https", sslsf)
.build();
PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(
registry);
// 將最大連接數增加
cm.setMaxTotal(maxTotal);
// 將每個路由基礎的連接增加
cm.setDefaultMaxPerRoute(maxPerRoute);
// 將目標主機的最大連接數增加
HttpHost httpHost = new HttpHost(hostname, port);
cm.setMaxPerRoute(new HttpRoute(httpHost), maxRoute);
// 請求重試處理
HttpRequestRetryHandler httpRequestRetryHandler = (exception, executionCount, context) -> {
if (executionCount >= 5) {// 如果已經重試了5次,就放棄
return false;
}
if (exception instanceof NoHttpResponseException) {// 如果服務器丟掉了連接,那麽就重試
return true;
}
if (exception instanceof SSLHandshakeException) {// 不要重試SSL握手異常
return false;
}
if (exception instanceof InterruptedIOException) {// 超時
return false;
}
if (exception instanceof UnknownHostException) {// 目標服務器不可達
return false;
}
if (exception instanceof ConnectTimeoutException) {// 連接被拒絕
return false;
}
if (exception instanceof SSLException) {// SSL握手異常
return false;
}
HttpClientContext clientContext = HttpClientContext.adapt(context);
HttpRequest request = clientContext.getRequest();
// 如果請求是冪等的,就再次嘗試
if (!(request instanceof HttpEntityEnclosingRequest)) {
return true;
}
return false;
};
CloseableHttpClient httpClient = HttpClients.custom()
.setConnectionManager(cm)
.setRetryHandler(httpRequestRetryHandler)
.build();
return httpClient;
}
示例13: requestConnection
import org.apache.http.conn.routing.HttpRoute; //導入依賴的package包/類
public ClientConnectionRequest requestConnection(HttpRoute route, Object state) {
if (count > MAX_COUNT) {
LOG.log(Level.INFO, "****Getting connection in pool. Current total max = "
+ getConnectionsInPool()
+ " Current for route = " + getConnectionsInPool(route) + " Route = "
+ route.getTargetHost());
count = 0;
}
++count;
return super.requestConnection(route, state);
}
示例14: process
import org.apache.http.conn.routing.HttpRoute; //導入依賴的package包/類
public void process(final HttpRequest request, final HttpContext context)
throws HttpException, IOException {
if (request == null) {
throw new IllegalArgumentException("HTTP request may not be null");
}
String method = request.getRequestLine().getMethod();
if (method.equalsIgnoreCase("CONNECT")) {
request.setHeader(PROXY_CONN_DIRECTIVE, HTTP.CONN_KEEP_ALIVE);
return;
}
// Obtain the client connection (required)
HttpRoutedConnection conn = (HttpRoutedConnection) context.getAttribute(
ExecutionContext.HTTP_CONNECTION);
if (conn == null) {
this.log.debug("HTTP connection not set in the context");
return;
}
HttpRoute route = conn.getRoute();
if (route.getHopCount() == 1 || route.isTunnelled()) {
if (!request.containsHeader(HTTP.CONN_DIRECTIVE)) {
request.addHeader(HTTP.CONN_DIRECTIVE, HTTP.CONN_KEEP_ALIVE);
}
}
if (route.getHopCount() == 2 && !route.isTunnelled()) {
if (!request.containsHeader(PROXY_CONN_DIRECTIVE)) {
request.addHeader(PROXY_CONN_DIRECTIVE, HTTP.CONN_KEEP_ALIVE);
}
}
}
示例15: process
import org.apache.http.conn.routing.HttpRoute; //導入依賴的package包/類
public void process(final HttpRequest request, final HttpContext context)
throws HttpException, IOException {
if (request == null) {
throw new IllegalArgumentException("HTTP request may not be null");
}
if (context == null) {
throw new IllegalArgumentException("HTTP context may not be null");
}
if (request.containsHeader(AUTH.PROXY_AUTH_RESP)) {
return;
}
HttpRoutedConnection conn = (HttpRoutedConnection) context.getAttribute(
ExecutionContext.HTTP_CONNECTION);
if (conn == null) {
this.log.debug("HTTP connection not set in the context");
return;
}
HttpRoute route = conn.getRoute();
if (route.isTunnelled()) {
return;
}
// Obtain authentication state
AuthState authState = (AuthState) context.getAttribute(
ClientContext.PROXY_AUTH_STATE);
if (authState == null) {
this.log.debug("Proxy auth state not set in the context");
return;
}
if (this.log.isDebugEnabled()) {
this.log.debug("Proxy auth state: " + authState.getState());
}
process(authState, request, context);
}