當前位置: 首頁>>代碼示例>>Java>>正文


Java HttpClientContext.getRequest方法代碼示例

本文整理匯總了Java中org.apache.http.client.protocol.HttpClientContext.getRequest方法的典型用法代碼示例。如果您正苦於以下問題:Java HttpClientContext.getRequest方法的具體用法?Java HttpClientContext.getRequest怎麽用?Java HttpClientContext.getRequest使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在org.apache.http.client.protocol.HttpClientContext的用法示例。


在下文中一共展示了HttpClientContext.getRequest方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: retryRequest

import org.apache.http.client.protocol.HttpClientContext; //導入方法依賴的package包/類
@Override
public boolean retryRequest(IOException exception, int executionCount, HttpContext context) {
    if (executionCount >= 5) {// 如果已經重試了5次,就放棄
        return false;
    }
    if (exception instanceof NoHttpResponseException) {// 如果服務器丟掉了連接,那麽就重試
        return true;
    }
    if (exception instanceof InterruptedIOException) {// 超時
        return false;
    }
    if (exception instanceof SSLHandshakeException) {// 不要重試SSL握手異常
        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;
}
 
開發者ID:adealjason,項目名稱:dtsopensource,代碼行數:32,代碼來源:HttpProtocolParent.java

示例2: retryRequest

import org.apache.http.client.protocol.HttpClientContext; //導入方法依賴的package包/類
@Override
public boolean retryRequest(IOException exception, int executionCount, HttpContext context) {

    if (executionCount >= 3) {// 如果已經重試了3次,就放棄
        return false;
    }

    if (exception instanceof NoHttpResponseException) {// 如果服務器丟掉了連接,那麽就重試
        return true;
    }

    if (exception instanceof SSLHandshakeException) {// 不要重試SSL握手異常
        return false;
    }

    if (exception instanceof InterruptedIOException) {// 超時
        return true;
    }

    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;
}
 
開發者ID:fengzhizi715,項目名稱:PicCrawler,代碼行數:41,代碼來源:RetryHandler.java

示例3: retryRequest

import org.apache.http.client.protocol.HttpClientContext; //導入方法依賴的package包/類
public boolean retryRequest(
        IOException exception,
        int executionCount,
        HttpContext context) {
    if (executionCount >= 5) {
        // Do not retry if over max retry count
        return false;
    }
    if (exception instanceof InterruptedIOException) {
        // Timeout
        return false;
    }
    if (exception instanceof UnknownHostException) {
        // Unknown host
        return false;
    }
    if (exception instanceof ConnectTimeoutException) {
        // Connection refused
        return false;
    }
    if (exception instanceof SSLException) {
        // SSL handshake exception
        return false;
    }
    HttpClientContext clientContext = HttpClientContext.adapt(context);
    HttpRequest request = clientContext.getRequest();
    boolean idempotent = !(request instanceof HttpEntityEnclosingRequest);
    if (idempotent) {
        // Retry if the request is considered idempotent
        return true;
    }
    return false;
}
 
開發者ID:NOAA-PMEL,項目名稱:LAS,代碼行數:34,代碼來源:LASProxy.java

示例4: retryRequest

import org.apache.http.client.protocol.HttpClientContext; //導入方法依賴的package包/類
/**
 * 自定義的恢複策略
 */
public boolean retryRequest(IOException exception, int exceptionCount,
		HttpContext context) {
	if (exceptionCount >= 3)
		return false;
	if (exception instanceof InterruptedIOException) {
		return false;
	}
	if (exception instanceof UnknownHostException) {
		return false;
	}
	if (exception instanceof ConnectTimeoutException) {
		return false;
	}
	if (exception instanceof SSLException) {
		return false;
	}
	HttpClientContext clientContext = HttpClientContext.adapt(context);
	HttpRequest request = clientContext.getRequest();
	boolean idempotent = !(request instanceof HttpEntityEnclosingRequest);
	if (idempotent) {
		return true;
	}
	return false;
}
 
開發者ID:xmomen,項目名稱:dms-webapp,代碼行數:28,代碼來源:HttpConnectionManager.java

示例5: retryRequest

import org.apache.http.client.protocol.HttpClientContext; //導入方法依賴的package包/類
/**
 * Used <code>retryCount</code> and <code>requestSentRetryEnabled</code> to determine
 * if the given method should be retried.
 */
public boolean retryRequest(
        final IOException exception,
        final int executionCount,
        final HttpContext context) {
    Args.notNull(exception, "Exception parameter");
    Args.notNull(context, "HTTP context");
    if (executionCount > this.retryCount) {
        // Do not retry if over max retry count
        return false;
    }
    if (this.nonRetriableClasses.contains(exception.getClass())) {
        return false;
    } else {
        for (final Class<? extends IOException> rejectException : this.nonRetriableClasses) {
            if (rejectException.isInstance(exception)) {
                return false;
            }
        }
    }
    final HttpClientContext clientContext = HttpClientContext.adapt(context);
    final HttpRequest request = clientContext.getRequest();

    if(requestIsAborted(request)){
        return false;
    }

    if (handleAsIdempotent(request)) {
        // Retry if the request is considered idempotent
        return true;
    }

    if (!clientContext.isRequestSent() || this.requestSentRetryEnabled) {
        // Retry if the request has not been sent fully or
        // if it's OK to retry methods that have been sent
        return true;
    }
    // otherwise do not retry
    return false;
}
 
開發者ID:xxonehjh,項目名稱:remote-files-sync,代碼行數:44,代碼來源:DefaultHttpRequestRetryHandlerHC4.java

示例6: testPostNoRedirect

import org.apache.http.client.protocol.HttpClientContext; //導入方法依賴的package包/類
@Test
public void testPostNoRedirect() throws Exception {
    this.serverBootstrap.registerHandler("*", new BasicRedirectService());

    final HttpHost target = start();

    final HttpClientContext context = HttpClientContext.create();

    final HttpPost httppost = new HttpPost("/oldlocation/");
    httppost.setEntity(new StringEntity("stuff"));

    final HttpResponse response = this.httpclient.execute(target, httppost, context);
    EntityUtils.consume(response.getEntity());

    final HttpRequest reqWrapper = context.getRequest();

    Assert.assertEquals(HttpStatus.SC_MOVED_TEMPORARILY, response.getStatusLine().getStatusCode());
    Assert.assertEquals("/oldlocation/", reqWrapper.getRequestLine().getUri());
    Assert.assertEquals("POST", reqWrapper.getRequestLine().getMethod());
}
 
開發者ID:MyPureCloud,項目名稱:purecloud-iot,代碼行數:21,代碼來源:TestRedirects.java

示例7: testRelativeRedirect2

import org.apache.http.client.protocol.HttpClientContext; //導入方法依賴的package包/類
@Test
public void testRelativeRedirect2() throws Exception {
    this.serverBootstrap.registerHandler("*", new RelativeRedirectService2());

    final HttpHost target = start();

    final HttpClientContext context = HttpClientContext.create();

    final RequestConfig config = RequestConfig.custom().setRelativeRedirectsAllowed(true).build();
    final HttpGet httpget = new HttpGet("/test/oldlocation");
    httpget.setConfig(config);

    final HttpResponse response = this.httpclient.execute(target, httpget, context);
    EntityUtils.consume(response.getEntity());

    final HttpRequest reqWrapper = context.getRequest();
    final HttpHost host = context.getTargetHost();

    Assert.assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode());
    Assert.assertEquals("/test/relativelocation", reqWrapper.getRequestLine().getUri());
    Assert.assertEquals(host, target);
}
 
開發者ID:MyPureCloud,項目名稱:purecloud-iot,代碼行數:23,代碼來源:TestRedirects.java

示例8: testRelativeRequestURIWithFragment

import org.apache.http.client.protocol.HttpClientContext; //導入方法依賴的package包/類
@Test
public void testRelativeRequestURIWithFragment() throws Exception {
    this.serverBootstrap.registerHandler("*", new SimpleService());

    final HttpHost target = start();

    final HttpGet httpget = new HttpGet("/stuff#blahblah");
    final HttpClientContext context = HttpClientContext.create();

    final HttpResponse response = this.httpclient.execute(target, httpget, context);
    Assert.assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode());
    EntityUtils.consume(response.getEntity());

    final HttpRequest request = context.getRequest();
    Assert.assertEquals("/stuff", request.getRequestLine().getUri());
}
 
開發者ID:MyPureCloud,項目名稱:purecloud-iot,代碼行數:17,代碼來源:TestClientRequestExecution.java

示例9: testRelativeRedirect

import org.apache.http.client.protocol.HttpClientContext; //導入方法依賴的package包/類
@Test
public void testRelativeRedirect() throws Exception {
    this.serverBootstrap.registerHandler("*", new RelativeRedirectService());

    final HttpHost target = start();

    final HttpClientContext context = HttpClientContext.create();

    final RequestConfig config = RequestConfig.custom().setRelativeRedirectsAllowed(true).build();
    final HttpGet httpget = new HttpGet("/oldlocation/");
    httpget.setConfig(config);

    final HttpResponse response = this.httpclient.execute(target, httpget, context);
    EntityUtils.consume(response.getEntity());

    final HttpRequest reqWrapper = context.getRequest();
    final HttpHost host = context.getTargetHost();

    Assert.assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode());
    Assert.assertEquals("/relativelocation/", reqWrapper.getRequestLine().getUri());
    Assert.assertEquals(host, target);
}
 
開發者ID:MyPureCloud,項目名稱:purecloud-iot,代碼行數:23,代碼來源:TestRedirects.java

示例10: testBasicRedirect302

import org.apache.http.client.protocol.HttpClientContext; //導入方法依賴的package包/類
@Test
public void testBasicRedirect302() throws Exception {
    this.serverBootstrap.registerHandler("*",
            new BasicRedirectService(HttpStatus.SC_MOVED_TEMPORARILY));

    final HttpHost target = start();

    final HttpClientContext context = HttpClientContext.create();

    final HttpGet httpget = new HttpGet("/oldlocation/");

    final HttpResponse response = this.httpclient.execute(target, httpget, context);
    EntityUtils.consume(response.getEntity());

    final HttpRequest reqWrapper = context.getRequest();
    final HttpHost host = context.getTargetHost();

    Assert.assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode());
    Assert.assertEquals("/newlocation/", reqWrapper.getRequestLine().getUri());
    Assert.assertEquals(target, host);
}
 
開發者ID:MyPureCloud,項目名稱:purecloud-iot,代碼行數:22,代碼來源:TestRedirects.java

示例11: testBasicRedirect303

import org.apache.http.client.protocol.HttpClientContext; //導入方法依賴的package包/類
@Test
public void testBasicRedirect303() throws Exception {
    this.serverBootstrap.registerHandler("*",
            new BasicRedirectService(HttpStatus.SC_SEE_OTHER));

    final HttpHost target = start();

    final HttpClientContext context = HttpClientContext.create();

    final HttpGet httpget = new HttpGet("/oldlocation/");

    final HttpResponse response = this.httpclient.execute(target, httpget, context);
    EntityUtils.consume(response.getEntity());

    final HttpRequest reqWrapper = context.getRequest();
    final HttpHost host = context.getTargetHost();

    Assert.assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode());
    Assert.assertEquals("/newlocation/", reqWrapper.getRequestLine().getUri());
    Assert.assertEquals(target, host);
}
 
開發者ID:MyPureCloud,項目名稱:purecloud-iot,代碼行數:22,代碼來源:TestRedirects.java

示例12: testBasicRedirect305

import org.apache.http.client.protocol.HttpClientContext; //導入方法依賴的package包/類
@Test
public void testBasicRedirect305() throws Exception {
    this.serverBootstrap.registerHandler("*",
            new BasicRedirectService(HttpStatus.SC_USE_PROXY));
    final HttpHost target = start();

    final HttpClientContext context = HttpClientContext.create();

    final HttpGet httpget = new HttpGet("/oldlocation/");

    final HttpResponse response = this.httpclient.execute(target, httpget, context);
    EntityUtils.consume(response.getEntity());

    final HttpRequest reqWrapper = context.getRequest();

    Assert.assertEquals(HttpStatus.SC_USE_PROXY, response.getStatusLine().getStatusCode());
    Assert.assertEquals("/oldlocation/", reqWrapper.getRequestLine().getUri());
}
 
開發者ID:MyPureCloud,項目名稱:purecloud-iot,代碼行數:19,代碼來源:TestRedirects.java

示例13: testBasicRedirect307

import org.apache.http.client.protocol.HttpClientContext; //導入方法依賴的package包/類
@Test
public void testBasicRedirect307() throws Exception {
    this.serverBootstrap.registerHandler("*",
            new BasicRedirectService(HttpStatus.SC_TEMPORARY_REDIRECT));

    final HttpHost target = start();

    final HttpClientContext context = HttpClientContext.create();

    final HttpGet httpget = new HttpGet("/oldlocation/");

    final HttpResponse response = this.httpclient.execute(target, httpget, context);
    EntityUtils.consume(response.getEntity());

    final HttpRequest reqWrapper = context.getRequest();
    final HttpHost host = context.getTargetHost();

    Assert.assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode());
    Assert.assertEquals("/newlocation/", reqWrapper.getRequestLine().getUri());
    Assert.assertEquals(target, host);
}
 
開發者ID:MyPureCloud,項目名稱:purecloud-iot,代碼行數:22,代碼來源:TestRedirects.java

示例14: createHttpClient

import org.apache.http.client.protocol.HttpClientContext; //導入方法依賴的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;
}
 
開發者ID:Evan1120,項目名稱:wechat-api-java,代碼行數:64,代碼來源:HttpRequestUtil.java

示例15: retryRequest

import org.apache.http.client.protocol.HttpClientContext; //導入方法依賴的package包/類
@Override
  public boolean retryRequest(
          IOException exception,
          int executionCount,
          HttpContext context) {
if (executionCount > retryExecutionCount) {
          return false;
      }
      if (exception instanceof InterruptedIOException) {
          return false;
      }
      if (exception instanceof UnknownHostException) {
          return false;
      }
      if (exception instanceof ConnectTimeoutException) {
          return true;
      }
      if (exception instanceof SSLException) {
          return false;
      }
      HttpClientContext clientContext = HttpClientContext.adapt(context);
      HttpRequest request = clientContext.getRequest();
      boolean idempotent = !(request instanceof HttpEntityEnclosingRequest);
      if (idempotent) {
          // Retry if the request is considered idempotent
          return true;
      }
      return false;
  }
 
開發者ID:luotuo,項目名稱:springboot-security-wechat,代碼行數:30,代碼來源:HttpClientFactory.java


注:本文中的org.apache.http.client.protocol.HttpClientContext.getRequest方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。