org.apache.http.client.config.CookieSpecs源码实例Demo

org.apache.http.impl.client.StandardHttpRequestRetryHandler#org.apache.http.client.config.CookieSpecs源码实例Demo

下面列出了org.apache.http.impl.client.StandardHttpRequestRetryHandler#org.apache.http.client.config.CookieSpecs 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: pay-spring-boot   文件: AbstractSSLHttpClient.java
public AbstractSSLHttpClient(){
    /**
     * 请求配置
     */
    RequestConfig globalConfig = RequestConfig.
                                    custom().
                                    setCookieSpec(CookieSpecs.DEFAULT).
                                    setSocketTimeout(10000).
                                    setConnectTimeout(20000).
                                    setConnectionRequestTimeout(20000).
                                    build();
    /**
     * cookie容器
     */
    CookieStore cookieStore = new BasicCookieStore();
    
    /**
     * 核心请求对象
     */
    httpclient = HttpClients.
                    custom().
                    setDefaultRequestConfig(globalConfig).
                    setDefaultCookieStore(cookieStore).
                    setSSLSocketFactory(getSSLSocketFactory()).
                    build();
}
 
源代码2 项目: oxAuth   文件: ClientUtil.java
/**
 * Creates a special SSLContext using a custom TLS version and a set of ciphers enabled to process SSL connections.
 * @param tlsVersion TLS version, for example TLSv1.2
 * @param ciphers Set of ciphers used to create connections.
 */
public static CloseableHttpClient createHttpClient(String tlsVersion, String[] ciphers) {
    try {
        SSLContext sslContext = SSLContexts.createDefault();
        SSLConnectionSocketFactory sslConnectionFactory = new SSLConnectionSocketFactory(sslContext,
                new String[] { tlsVersion }, ciphers, NoopHostnameVerifier.INSTANCE);

        Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory> create()
                .register("https", sslConnectionFactory)
                .register("http", new PlainConnectionSocketFactory())
                .build();

        PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(registry);

        return HttpClients.custom()
                .setSSLContext(sslContext)
                .setDefaultRequestConfig(RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD).build())
                .setConnectionManager(cm)
                .build();
    } catch (Exception e) {
        log.error("Error creating HttpClient with a custom TLS version and custom ciphers", e);
        return null;
    }
}
 
源代码3 项目: uyuni   文件: SaltService.java
/**
 * Default constructor
 */
public SaltService() {
    RequestConfig requestConfig = RequestConfig.custom()
            .setConnectTimeout(0)
            .setSocketTimeout(0)
            .setConnectionRequestTimeout(0)
            .setCookieSpec(CookieSpecs.STANDARD)
            .build();
    HttpAsyncClientBuilder httpClientBuilder = HttpAsyncClients.custom();
    httpClientBuilder.setDefaultRequestConfig(requestConfig);

    asyncHttpClient = httpClientBuilder
            .setMaxConnPerRoute(20)
            .setMaxConnTotal(20)
            .build();
    asyncHttpClient.start();

    SALT_CLIENT = new SaltClient(SALT_MASTER_URI, new HttpAsyncClientImpl(asyncHttpClient));
    saltSSHService = new SaltSSHService(SALT_CLIENT, SaltActionChainGeneratorService.INSTANCE);
    defaultBatch = Batch.custom().withBatchAsAmount(ConfigDefaults.get().getSaltBatchSize())
                    .withDelay(ConfigDefaults.get().getSaltBatchDelay())
                    .withPresencePingTimeout(ConfigDefaults.get().getSaltPresencePingTimeout())
                    .withPresencePingGatherJobTimeout(ConfigDefaults.get().getSaltPresencePingGatherJobTimeout())
                    .build();
}
 
源代码4 项目: Alice-LiveMan   文件: HttpRequestUtil.java
public static byte[] downloadUrl(URI url) throws IOException {
    HttpGet httpGet = new HttpGet(url);
    HttpClientContext context = HttpClientContext.create();
    RequestConfig.Builder builder = RequestConfig.custom();
    builder.setConnectTimeout(2000).setConnectionRequestTimeout(2000).setSocketTimeout(5000).setCookieSpec(CookieSpecs.IGNORE_COOKIES).setRedirectsEnabled(true);
    httpGet.setConfig(builder.build());
    httpGet.addHeader("Accept", "*/*");
    httpGet.addHeader("Accept-Encoding", "gzip, deflate");
    httpGet.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36");
    try (CloseableHttpResponse httpResponse = client.execute(httpGet, context)) {
        HttpEntity responseEntity = httpResponse.getEntity();
        if (httpResponse.getStatusLine().getStatusCode() != 200) {
            throw new IOException(httpResponse.getStatusLine().getStatusCode() + " " + httpResponse.getStatusLine().getReasonPhrase() + "\n Headers:" + Arrays.toString(httpResponse.getAllHeaders()) + "\n" + EntityUtils.toString(responseEntity));
        }
        return EntityUtils.toByteArray(responseEntity);
    } catch (IllegalStateException e) {
        initClient();
        throw e;
    }
}
 
源代码5 项目: oxAuth   文件: BaseTest.java
public static CloseableHttpClient createHttpClientTrustAll()
		throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException, UnrecoverableKeyException {
	SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(new TrustStrategy() {
		@Override
		public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
			return true;
		}
	}).build();
	SSLConnectionSocketFactory sslContextFactory = new SSLConnectionSocketFactory(sslContext);
	CloseableHttpClient httpclient = HttpClients.custom()
			.setDefaultRequestConfig(RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD).build())
			.setSSLSocketFactory(sslContextFactory)
			.setRedirectStrategy(new LaxRedirectStrategy()).build();

	return httpclient;
}
 
源代码6 项目: GoogleTranslation   文件: HttpPostParams.java
@Override
protected CloseableHttpResponse send(CloseableHttpClient httpClient, String base) throws Exception {
    List<NameValuePair> formParams = new ArrayList<>();
    for (String key : params.keySet()) {
        String value = params.get(key);
        formParams.add(new BasicNameValuePair(key, value));
    }
    HttpPost request = new HttpPost(base);

    RequestConfig localConfig = RequestConfig.custom()
            .setCookieSpec(CookieSpecs.STANDARD)
            .build();
    request.setConfig(localConfig);
    request.setEntity(new UrlEncodedFormEntity(formParams, "UTF-8"));
    request.setHeader("Content-Type", "application/x-www-form-urlencoded");        //内容为post
    return httpClient.execute(request);
}
 
源代码7 项目: cryptotrader   文件: TemplateContext.java
protected TemplateContext(String id) {

        this.id = id;

        this.executor = Executors.newCachedThreadPool();

        this.state = new AtomicReference<>(StateType.ACTIVE);

        this.client = HttpClients.custom()
                .evictExpiredConnections()
                .evictIdleConnections(CLIENT_TIMEOUT.toMillis(), MILLISECONDS)
                .setConnectionTimeToLive(CLIENT_TIMEOUT.toMillis(), MILLISECONDS)
                .setMaxConnPerRoute(Byte.MAX_VALUE)
                .setMaxConnTotal(Byte.MAX_VALUE)
                .setDefaultRequestConfig(
                        RequestConfig.custom()
                                .setCookieSpec(CookieSpecs.STANDARD)
                                .setSocketTimeout((int) CLIENT_TIMEOUT.toMillis())
                                .setConnectTimeout((int) CLIENT_TIMEOUT.toMillis())
                                .setConnectionRequestTimeout((int) CLIENT_TIMEOUT.toMillis())
                                .build()
                )
                .build();


    }
 
源代码8 项目: vk-java-sdk   文件: HttpTransportClient.java
public HttpTransportClient(int retryAttemptsNetworkErrorCount, int retryAttemptsInvalidStatusCount) {
    this.retryAttemptsNetworkErrorCount = retryAttemptsNetworkErrorCount;
    this.retryAttemptsInvalidStatusCount = retryAttemptsInvalidStatusCount;

    CookieStore cookieStore = new BasicCookieStore();
    RequestConfig requestConfig = RequestConfig.custom()
            .setSocketTimeout(SOCKET_TIMEOUT_MS)
            .setConnectTimeout(CONNECTION_TIMEOUT_MS)
            .setConnectionRequestTimeout(CONNECTION_TIMEOUT_MS)
            .setCookieSpec(CookieSpecs.STANDARD)
            .build();

    PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();

    connectionManager.setMaxTotal(MAX_SIMULTANEOUS_CONNECTIONS);
    connectionManager.setDefaultMaxPerRoute(MAX_SIMULTANEOUS_CONNECTIONS);

    httpClient = HttpClients.custom()
            .setConnectionManager(connectionManager)
            .setDefaultRequestConfig(requestConfig)
            .setDefaultCookieStore(cookieStore)
            .setUserAgent(USER_AGENT)
            .build();
}
 
源代码9 项目: SnowGraph   文件: HttpPostParams.java
@Override
protected CloseableHttpResponse send(CloseableHttpClient httpClient, String base) throws Exception {
	List<NameValuePair> formparams = new ArrayList<NameValuePair>();
	for (String key : params.keySet()) {
		String value = params.get(key);
		formparams.add(new BasicNameValuePair(key, value));
	}
	HttpPost request = new HttpPost(base);

       RequestConfig localConfig = RequestConfig.custom()
               .setCookieSpec(CookieSpecs.STANDARD)
               .build();
       request.setConfig(localConfig); 
	request.setEntity(new UrlEncodedFormEntity(formparams, "UTF-8"));
	request.setHeader("Content-Type", "application/x-www-form-urlencoded");		//内容为post
	return httpClient.execute(request);
}
 
源代码10 项目: cosmic   文件: HttpClientHelper.java
public static CloseableHttpClient createHttpClient(final int maxRedirects) throws KeyManagementException, NoSuchAlgorithmException, KeyStoreException {
    s_logger.info("Creating new HTTP connection pool and client");
    final Registry<ConnectionSocketFactory> socketFactoryRegistry = createSocketFactoryConfigration();
    final BasicCookieStore cookieStore = new BasicCookieStore();
    final PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
    connManager.setDefaultMaxPerRoute(MAX_ALLOCATED_CONNECTIONS_PER_ROUTE);
    connManager.setMaxTotal(MAX_ALLOCATED_CONNECTIONS);
    final RequestConfig requestConfig = RequestConfig.custom()
                                                     .setCookieSpec(CookieSpecs.DEFAULT)
                                                     .setMaxRedirects(maxRedirects)
                                                     .setSocketTimeout(DEFAULT_SOCKET_TIMEOUT)
                                                     .setConnectionRequestTimeout(DEFAULT_CONNECTION_REQUEST_TIMEOUT)
                                                     .setConnectTimeout(DEFAULT_CONNECT_TIMEOUT)
                                                     .build();
    return HttpClientBuilder.create()
                            .setConnectionManager(connManager)
                            .setRedirectStrategy(new LaxRedirectStrategy())
                            .setDefaultRequestConfig(requestConfig)
                            .setDefaultCookieStore(cookieStore)
                            .setRetryHandler(new StandardHttpRequestRetryHandler())
                            .build();
}
 
源代码11 项目: jasperreports   文件: HttpDataService.java
protected CloseableHttpClient createHttpClient(Map<String, Object> parameters)
{
	HttpClientBuilder clientBuilder = HttpClients.custom();
	
	// single connection
	BasicHttpClientConnectionManager connManager = new BasicHttpClientConnectionManager();
	clientBuilder.setConnectionManager(connManager);
	
	// ignore cookies for now
	RequestConfig requestConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.IGNORE_COOKIES).build();
	clientBuilder.setDefaultRequestConfig(requestConfig);
	
	setAuthentication(parameters, clientBuilder);
	
	CloseableHttpClient client = clientBuilder.build();
	return client;
}
 
源代码12 项目: jasperreports   文件: ProcessConnection.java
public ProcessConnection(ProcessDirector director, PhantomJSProcess process)
{
	this.process = process;
	
	HttpClientBuilder clientBuilder = HttpClients.custom();
	
	// single connection
	BasicHttpClientConnectionManager connManager = new BasicHttpClientConnectionManager();
	clientBuilder.setConnectionManager(connManager);
	
	RequestConfig requestConfig = RequestConfig.custom()
			// ignore cookies for now
			.setCookieSpec(CookieSpecs.IGNORE_COOKIES)
			.setSocketTimeout(director.getRequestTimeout()).build();
	clientBuilder.setDefaultRequestConfig(requestConfig);
	
	this.httpClient = clientBuilder.build();
}
 
源代码13 项目: clouddisk   文件: UserInfoParser.java
@Override
public HttpGet initRequest(final UserInfoParameter userInfoParameter) {
	try {
		final URIBuilder uriBuilder = new URIBuilder(CONST.URI_PATH);
		uriBuilder.addParameter(CONST.SRC_KEY, CONST.SRC_VAL);
		uriBuilder.addParameter(CONST.FROM_KEY, CONST.FROM_VAL);
		uriBuilder.addParameter(CONST.CHARSET_KEY, CONST.CHARSET_VAL);
		uriBuilder.addParameter(CONST.METHOD_KEY, CONST.METHOD_VAL);
		uriBuilder.addParameter(CONST.REQUESTSCEMA_KEY, CONST.REQUESTSCEMA_VAL);
		uriBuilder.addParameter(CONST.O_KEY, CONST.O_VAL);
		uriBuilder.addParameter(CONST.SHOW_NAME_FLAG_NAME, CONST.SHOW_NAME_FLAG_VALUE);
		uriBuilder.addParameter(CONST.HEAD_TYPE_NAME, CONST.HEAD_TYPE_VAL);
		uriBuilder.addParameter("-", TimeUtil.getTimeLenth(13));
		final HttpGet request = new HttpGet(uriBuilder.build());
		request.setConfig(RequestConfig.custom().setCookieSpec(CookieSpecs.NETSCAPE).build());
		return request;
	} catch (URISyntaxException e) {
		LOGGER.error("Error",e);
	}
	return null;
}
 
源代码14 项目: clouddisk   文件: UserCheckLoginParser.java
@Override
public HttpPost initRequest(final UserCheckLoginParameter userCheckLoginParameter) {
	setBinaryFilename(LoginConst.USER_INFO_PATH_NAME);
	Login login = readObjForDisk(Login.class);
	if(null==login){
		login = new Login();
	}
	final HttpPost request = new HttpPost(CONST.URI_PATH);
	final List<NameValuePair> data = new ArrayList<>();
	data.add(new BasicNameValuePair(CONST.QID_NAME, login.getQid()));
	data.add(new BasicNameValuePair(CONST.METHOD_KEY, CONST.METHOD_VAL));
	data.add(new BasicNameValuePair(CONST.AJAX_KEY, CONST.AJAX_VAL));
	data.add(new BasicNameValuePair("t", TimeUtil.getTimeLenth(13)));
	request.setConfig(RequestConfig.custom().setCookieSpec(CookieSpecs.NETSCAPE).build());
	request.setEntity(new UrlEncodedFormEntity(data, Consts.UTF_8));
	return request;
}
 
源代码15 项目: nexus-public   文件: DefaultsCustomizer.java
@Override
public void customize(final HttpClientPlan plan) {
  checkNotNull(plan);

  plan.setUserAgentBase(userAgentGenerator.generate());

  plan.getClient().setKeepAliveStrategy(new NexusConnectionKeepAliveStrategy(keepAliveDuration.toMillis()));
  plan.getClient().setRetryHandler(new StandardHttpRequestRetryHandler(2, false));

  plan.getConnection().setBufferSize(bufferSize.toBytesI());

  plan.getRequest().setConnectionRequestTimeout(connectionRequestTimeout.toMillisI());
  plan.getRequest().setCookieSpec(CookieSpecs.IGNORE_COOKIES);
  plan.getRequest().setExpectContinueEnabled(false);

  int requestTimeoutMillis = requestTimeout.toMillisI();
  plan.getSocket().setSoTimeout(requestTimeoutMillis);
  plan.getRequest().setConnectTimeout(requestTimeoutMillis);
  plan.getRequest().setSocketTimeout(requestTimeoutMillis);
}
 
源代码16 项目: DataHubSystem   文件: ODataProductSynchronizer.java
@Override
public CloseableHttpAsyncClient generateClient ()
{
   CredentialsProvider credsProvider = new BasicCredentialsProvider();
   credsProvider.setCredentials(new AuthScope (AuthScope.ANY),
           new UsernamePasswordCredentials(serviceUser, servicePass));
   RequestConfig rqconf = RequestConfig.custom()
         .setCookieSpec(CookieSpecs.DEFAULT)
         .setSocketTimeout(Timeouts.SOCKET_TIMEOUT)
         .setConnectTimeout(Timeouts.CONNECTION_TIMEOUT)
         .setConnectionRequestTimeout(Timeouts.CONNECTION_REQUEST_TIMEOUT)
         .build();
   CloseableHttpAsyncClient res = HttpAsyncClients.custom ()
         .setDefaultCredentialsProvider (credsProvider)
         .setDefaultRequestConfig(rqconf)
         .build ();
   res.start ();
   return res;
}
 
源代码17 项目: DataHubSystem   文件: ODataClient.java
@Override
public CloseableHttpAsyncClient generateClient ()
{
   CredentialsProvider credsProvider = new BasicCredentialsProvider();
   credsProvider.setCredentials(new AuthScope (AuthScope.ANY),
            new UsernamePasswordCredentials(username, password));
   RequestConfig rqconf = RequestConfig.custom()
         .setCookieSpec(CookieSpecs.DEFAULT)
         .setSocketTimeout(Timeouts.SOCKET_TIMEOUT)
         .setConnectTimeout(Timeouts.CONNECTION_TIMEOUT)
         .setConnectionRequestTimeout(Timeouts.CONNECTION_REQUEST_TIMEOUT)
         .build();
   CloseableHttpAsyncClient res = HttpAsyncClients.custom ()
         .setDefaultCredentialsProvider (credsProvider)
         .setDefaultRequestConfig(rqconf)
         .build ();
   res.start ();
   return res;
}
 
源代码18 项目: blynk-server   文件: HttpsAdminServerTest.java
@Test
public void testGetUserFromAdminPageNoAccessWithFakeCookie2() throws Exception {
    login(admin.email, admin.pass);

    SSLContext sslcontext = TestUtil.initUnsecuredSSLContext();
    // Allow TLSv1 protocol only
    SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext, new MyHostVerifier());
    CloseableHttpClient httpclient2 = HttpClients.custom()
            .setSSLSocketFactory(sslsf)
            .setDefaultRequestConfig(RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD).build())
            .build();


    String testUser = "[email protected]";
    String appName = "Blynk";
    HttpGet request = new HttpGet(httpsAdminServerUrl + "/users/" + testUser + "-" + appName);
    request.setHeader("Cookie", "session=123");

    try (CloseableHttpResponse response = httpclient2.execute(request)) {
        assertEquals(404, response.getStatusLine().getStatusCode());
    }
}
 
源代码19 项目: blynk-server   文件: OTATest.java
@Before
public void init() throws Exception {
    httpServer = new HardwareAndHttpAPIServer(holder).start();
    httpsServer = new MobileAndHttpsServer(holder).start();
    httpsAdminServerUrl = String.format("https://localhost:%s/admin", properties.getHttpsPort());

    String pass = "admin";
    User user = new User();
    user.isSuperAdmin = true;
    user.email = "[email protected]";
    user.pass = SHA256Util.makeHash(pass, user.email);
    holder.userDao.add(user);

    auth = (user.email + ":" + pass).getBytes();

    // Allow TLSv1 protocol only
    SSLContext sslcontext = initUnsecuredSSLContext();
    SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext, new MyHostVerifier());
    this.httpclient = HttpClients.custom()
            .setSSLSocketFactory(sslsf)
            .setDefaultRequestConfig(RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD).build())
            .build();
    clientPair = initAppAndHardPair(properties);
}
 
private HttpClientBuilder prepareHttpClientBuilderWithTimeoutsAndProxySettings(Proxy proxySettings) throws MojoExecutionException {
    SocketConfig socketConfig = SocketConfig.custom().setSoTimeout(FILE_DOWNLOAD_READ_TIMEOUT).build();
    RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(FILE_DOWNLOAD_CONNECT_TIMEOUT)
            .setCookieSpec(CookieSpecs.IGNORE_COOKIES).build();
    HttpClientBuilder httpClientBuilder = HttpClients.custom();
    httpClientBuilder
            .setDefaultSocketConfig(socketConfig)
            .setDefaultRequestConfig(requestConfig)
            .disableContentCompression();
    HttpHost proxy = ProxyUtils.createProxyFromSettings(proxySettings);
    if (proxy != null) {
        httpClientBuilder.setProxy(proxy);
        CredentialsProvider proxyCredentials = ProxyUtils.createProxyCredentialsFromSettings(proxySettings);
        if (proxyCredentials != null) {
            httpClientBuilder.setDefaultCredentialsProvider(proxyCredentials);
        }
    }
    return httpClientBuilder;
}
 
源代码21 项目: esigate   文件: VaryMasterTest.java
/**
 * Send a request with a Cookie "test-cookie" to vary.jsp (which will get content from provider) and ensure the
 * result is valid.
 * 
 * @param cookieValue
 * @param forceRefresh
 * @return Page timestamp. Can be used to detect cache hits.
 * @throws Exception
 */
private String doCookieRequest(String cookieValue, boolean forceRefresh) throws Exception {
    CookieStore cookieStore = new BasicCookieStore();
    HttpClientContext context = new HttpClientContext();
    context.setCookieStore(cookieStore);
    CloseableHttpClient client = HttpClients.createDefault();
    RequestConfig config = RequestConfig.custom().setCookieSpec(CookieSpecs.DEFAULT).build();
    HttpGet request = new HttpGet("http://localhost:8080/esigate-app-master/vary.jsp");
    request.setConfig(config);

    if (cookieValue != null) {
        BasicClientCookie cookie = new BasicClientCookie("test-cookie", cookieValue);
        cookie.setDomain("localhost");
        cookie.setPath("/");
        cookieStore.addCookie(cookie);
    }
    if (forceRefresh) {
        request.addHeader("Cache-Control", "no-cache");
    }
    HttpResponse response = client.execute(request, context);
    // Ensure content is valid.
    String text = IOUtils.toString(response.getEntity().getContent());
    assertNotNull(text);
    LOG.debug("----- Request with cookie " + cookieValue + " and forceRefresh=" + forceRefresh + " -----> \n"
            + text);
    if (cookieValue != null) {
        assertTrue("no value '" + cookieValue + "' found", text.contains(cookieValue));
    } else {
        assertTrue("no cookie found", text.contains("no cookie"));
    }

    client.close();

    return text.substring(text.indexOf("stime") + 5, text.indexOf("etime"));
}
 
源代码22 项目: pay-spring-boot   文件: HttpClient.java
private HttpClient(){
    /**
     * 请求配置
     */
    RequestConfig globalConfig = RequestConfig.
                                    custom().
                                    setCookieSpec(CookieSpecs.DEFAULT).
                                    setSocketTimeout(10000).
                                    setConnectTimeout(20000).
                                    setConnectionRequestTimeout(20000).
                                    build();
    /**
     * cookie容器
     */
    CookieStore cookieStore = new BasicCookieStore();
    
    /**
     * 核心请求对象
     */
    httpclient = HttpClients.
                    custom().
                    setDefaultRequestConfig(globalConfig).
                    setDefaultCookieStore(cookieStore).
                    setRetryHandler(new HttpRequestRetryHandler() {
                        @Override
                        public boolean retryRequest(IOException exception, int retryTimes, HttpContext httpContext) {
                            if(retryTimes > 10){  //最多重试10次
                                return false;
                            }
                            if(Arrays.asList(InterruptedIOException.class, UnknownHostException.class, ConnectException.class, SSLException.class).contains(exception.getClass())){  //此类异常不进行重试
                                return false;
                            }
                            return true;  //重点是这,非幂等的post请求也进行重试
                        }
                    }).
                    build();
}
 
源代码23 项目: BigData-In-Practice   文件: StatefulHttpClient.java
private void initCookieStore() {
    PublicSuffixMatcher publicSuffixMatcher = PublicSuffixMatcherLoader.getDefault();
    Registry<CookieSpecProvider> cookieSpecReg = RegistryBuilder.<CookieSpecProvider>create()
            .register(CookieSpecs.DEFAULT, new DefaultCookieSpecProvider(publicSuffixMatcher))
            .register(CookieSpecs.STANDARD, new RFC6265CookieSpecProvider(publicSuffixMatcher)).build();
    CookieStore cookieStore = new BasicCookieStore();

    context = HttpClientContext.create();
    context.setCookieSpecRegistry(cookieSpecReg);
    context.setCookieStore(cookieStore);
}
 
源代码24 项目: Alice-LiveMan   文件: HttpRequestUtil.java
public static String downloadUrl(URI url, String cookies, Map<String, String> requestProperties, Charset charset) throws IOException {
    HttpGet httpGet = new HttpGet(url);
    HttpClientContext context = HttpClientContext.create();
    RequestConfig.Builder builder = RequestConfig.custom();
    builder.setConnectTimeout(2000).setConnectionRequestTimeout(2000).setSocketTimeout(5000).setCookieSpec(CookieSpecs.IGNORE_COOKIES).setRedirectsEnabled(true);
    httpGet.setConfig(builder.build());
    if (StringUtils.isNotBlank(cookies)) {
        httpGet.setHeader("Cookie", cookies);
    }
    httpGet.setHeader("Accept", "*/*");
    httpGet.setHeader("Accept-Encoding", "gzip, deflate");
    httpGet.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36");
    if (requestProperties != null) {
        for (Map.Entry<String, String> entry : requestProperties.entrySet()) {
            httpGet.setHeader(entry.getKey(), entry.getValue());
        }
    }
    try (CloseableHttpResponse httpResponse = client.execute(httpGet, context)) {
        HttpEntity responseEntity = httpResponse.getEntity();
        if (httpResponse.getStatusLine().getStatusCode() != 200) {
            throw new IOException(httpResponse.getStatusLine().getStatusCode() + " " + httpResponse.getStatusLine().getReasonPhrase() + "\n Headers:" + Arrays.toString(httpResponse.getAllHeaders()) + "\n" + EntityUtils.toString(responseEntity));
        }
        return EntityUtils.toString(responseEntity, charset);
    } catch (IllegalStateException e) {
        initClient();
        throw e;
    }
}
 
源代码25 项目: esigate   文件: VaryAggregatorTest.java
/**
 * Send a request with a Cookie "test-cookie" to vary.jsp (which will get content from provider) and ensure the
 * result is valid.
 * 
 * @param addCookie
 * @param value
 * @param refresh
 * @return
 * @throws MalformedURLException
 * @throws IOException
 * @throws SAXException
 */
private String doRequestWithHeader(String headerValue, boolean forceRefresh) throws Exception {
    RequestConfig requestConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.DEFAULT).build();
    HttpClient client = HttpClientBuilder.create().setDefaultRequestConfig(requestConfig).build();
    HttpClientContext context = new HttpClientContext();
    context.setCookieStore(new BasicCookieStore());

    HttpGet request = new HttpGet(APPLICATION_PATH + "vary.jsp");
    if (headerValue != null) {
        BasicClientCookie cookie = new BasicClientCookie("test-cookie", headerValue);
        cookie.setDomain("localhost");
        cookie.setPath("/");
        context.getCookieStore().addCookie(cookie);
        request.setHeader("foo", headerValue);
    }
    if (forceRefresh) {
        request.addHeader("Cache-Control", "no-cache");
    }
    HttpResponse response = client.execute(request, context);
    // Ensure content is valid.
    String text = HttpResponseUtils.toString(response, null);
    assertNotNull(text);
    if (headerValue != null) {
        assertTrue("no value '" + headerValue + "' found", text.contains(headerValue));
    } else {
        assertTrue("no cookie found", text.contains("no cookie"));
    }

    // Ensure vary and Cache-Control header were forwarded
    assertEquals("foo", response.getFirstHeader("Vary").getValue());
    assertEquals("public, max-age=3600", response.getFirstHeader("Cache-Control").getValue());

    // Return page timestamp. Can be used to detect cache hits.
    return text.substring(text.indexOf("stime") + 5, text.indexOf("etime"));
}
 
源代码26 项目: DataflowTemplates   文件: HttpEventPublisher.java
/**
 * Utility method to create a {@link CloseableHttpClient} to make http POSTs against Splunk's
 * HEC.
 *
 * @param maxConnections max number of parallel connections.
 * @param disableCertificateValidation should disable certificate validation.
 */
private CloseableHttpClient getHttpClient(
    int maxConnections, boolean disableCertificateValidation)
    throws NoSuchAlgorithmException, KeyStoreException, KeyManagementException {

  HttpClientBuilder builder = ApacheHttpTransport.newDefaultHttpClientBuilder();

  if (genericUrl().getScheme().equalsIgnoreCase(HTTPS_PROTOCOL_PREFIX)) {
    LOG.info("SSL connection requested");

    HostnameVerifier hostnameVerifier =
        disableCertificateValidation
            ? NoopHostnameVerifier.INSTANCE
            : new DefaultHostnameVerifier();

    SSLContextBuilder sslContextBuilder = SSLContextBuilder.create();
    if (disableCertificateValidation) {
      LOG.info("Certificate validation is disabled");
      sslContextBuilder.loadTrustMaterial((TrustStrategy) (chain, authType) -> true);
    }

    SSLConnectionSocketFactory connectionSocketFactory =
        new SSLConnectionSocketFactory(sslContextBuilder.build(), hostnameVerifier);
    builder.setSSLSocketFactory(connectionSocketFactory);
  }

  builder.setMaxConnTotal(maxConnections);
  builder.setDefaultRequestConfig(
      RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD).build());

  return builder.build();
}
 
源代码27 项目: CloudflareAPI   文件: CloudflareAccess.java
public CloudflareAccess( @NonNull String xAuthKey, @NonNull String xAuthEmail, @Nullable ExecutorService threadPool, String apiBaseUrl ) {
    this.xAuthKey = xAuthKey;
    this.xAuthEmail = xAuthEmail;
    this.threadPool = threadPool;
    restClient = RestClient.builder()
        .baseUrl( apiBaseUrl )
        .defaultHeader( "Content-Type", "application/json" )
        .defaultHeader( "X-Auth-Key", this.getXAuthKey() )
        .defaultHeader( "X-Auth-Email", this.getXAuthEmail() )
        .followRedirect( false )
        .cookieSpec( CookieSpecs.IGNORE_COOKIES )
        .build();
}
 
源代码28 项目: timer   文件: HttpConnectionManager.java
public static HttpClient getHtpClient() {

        RequestConfig requestConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD_STRICT).build();
        CloseableHttpClient httpClient = HttpClients
                .custom()
                .setConnectionManager(cm)
                .setRedirectStrategy(new LaxRedirectStrategy())
                .setDefaultRequestConfig(requestConfig)
                .build();
        return httpClient;
    }
 
源代码29 项目: para   文件: FacebookAuthFilter.java
/**
 * Default constructor.
 * @param defaultFilterProcessesUrl the url of the filter
 */
public FacebookAuthFilter(String defaultFilterProcessesUrl) {
	super(defaultFilterProcessesUrl);
	this.jreader = ParaObjectUtils.getJsonReader(Map.class);
	int timeout = 30 * 1000;
	this.httpclient = HttpClientBuilder.create().
			setConnectionReuseStrategy(new NoConnectionReuseStrategy()).
			setDefaultRequestConfig(RequestConfig.custom().
					setConnectTimeout(timeout).
					setConnectionRequestTimeout(timeout).
					setCookieSpec(CookieSpecs.STANDARD).
					setSocketTimeout(timeout).
					build()).
			build();
}
 
源代码30 项目: timer   文件: HttpConnectionManager.java
public static HttpClient getHtpClient(AuthenticationStrategy proxy) {
    RequestConfig requestConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD_STRICT).build();
    CloseableHttpClient httpClient = HttpClients
            .custom()
            .setConnectionManager(cm)
            .setProxyAuthenticationStrategy(proxy)
            .setRedirectStrategy(new LaxRedirectStrategy())
            .setDefaultRequestConfig(requestConfig)
            .build();
    return httpClient;
}