java类org.apache.http.impl.client.LaxRedirectStrategy的实例源码

UraniumUpdater.java 文件源码 项目:Uranium 阅读 30 收藏 0 点赞 0 评论 0
private static void download(String url, File destination)
        throws IOException {
    HttpUriRequest request = RequestBuilder
            .get()
            .setUri(url)
            .addParameter("hostname",
                    MinecraftServer.getServer().getHostname())
            .addParameter("port",
                    String.valueOf(MinecraftServer.getServer().getPort()))
            .build();
    CloseableHttpClient client = HttpClientBuilder.create()
            .setRedirectStrategy(new LaxRedirectStrategy())
            .setUserAgent("Uranium Updater").build();

    HttpResponse response = client.execute(request);
    if (response.getStatusLine().getStatusCode() != 200) {
        client.close();
        throw new IllegalStateException("Could not download " + url);
    }
    InputStream is = response.getEntity().getContent();
    OutputStream os = new FileOutputStream(destination);
    IOUtils.copy(is, os);
    is.close();
    os.close();
    client.close();
}
LegacyMmsConnection.java 文件源码 项目:PeSanKita-android 阅读 20 收藏 0 点赞 0 评论 0
protected CloseableHttpClient constructHttpClient() throws IOException {
  RequestConfig config = RequestConfig.custom()
                                      .setConnectTimeout(20 * 1000)
                                      .setConnectionRequestTimeout(20 * 1000)
                                      .setSocketTimeout(20 * 1000)
                                      .setMaxRedirects(20)
                                      .build();

  URL                 mmsc          = new URL(apn.getMmsc());
  CredentialsProvider credsProvider = new BasicCredentialsProvider();

  if (apn.hasAuthentication()) {
    credsProvider.setCredentials(new AuthScope(mmsc.getHost(), mmsc.getPort() > -1 ? mmsc.getPort() : mmsc.getDefaultPort()),
                                 new UsernamePasswordCredentials(apn.getUsername(), apn.getPassword()));
  }

  return HttpClients.custom()
                    .setConnectionReuseStrategy(new NoConnectionReuseStrategyHC4())
                    .setRedirectStrategy(new LaxRedirectStrategy())
                    .setUserAgent(TextSecurePreferences.getMmsUserAgent(context, USER_AGENT))
                    .setConnectionManager(new BasicHttpClientConnectionManager())
                    .setDefaultRequestConfig(config)
                    .setDefaultCredentialsProvider(credsProvider)
                    .build();
}
LegacyMmsConnection.java 文件源码 项目:Cable-Android 阅读 22 收藏 0 点赞 0 评论 0
protected CloseableHttpClient constructHttpClient() throws IOException {
  RequestConfig config = RequestConfig.custom()
                                      .setConnectTimeout(20 * 1000)
                                      .setConnectionRequestTimeout(20 * 1000)
                                      .setSocketTimeout(20 * 1000)
                                      .setMaxRedirects(20)
                                      .build();

  URL                 mmsc          = new URL(apn.getMmsc());
  CredentialsProvider credsProvider = new BasicCredentialsProvider();

  if (apn.hasAuthentication()) {
    credsProvider.setCredentials(new AuthScope(mmsc.getHost(), mmsc.getPort() > -1 ? mmsc.getPort() : mmsc.getDefaultPort()),
                                 new UsernamePasswordCredentials(apn.getUsername(), apn.getPassword()));
  }

  return HttpClients.custom()
                    .setConnectionReuseStrategy(new NoConnectionReuseStrategyHC4())
                    .setRedirectStrategy(new LaxRedirectStrategy())
                    .setUserAgent(TextSecurePreferences.getMmsUserAgent(context, USER_AGENT))
                    .setConnectionManager(new BasicHttpClientConnectionManager())
                    .setDefaultRequestConfig(config)
                    .setDefaultCredentialsProvider(credsProvider)
                    .build();
}
DefaultHttpClientGenerator.java 文件源码 项目:vscrawler 阅读 16 收藏 0 点赞 0 评论 0
@Override
public CrawlerHttpClient gen(CrawlerHttpClientBuilder proxyFeedBackDecorateHttpClientBuilder) {
    SocketConfig socketConfig = SocketConfig.custom().setSoKeepAlive(true).setSoLinger(-1).setSoReuseAddress(false)
            .setSoTimeout(ProxyConstant.SOCKETSO_TIMEOUT).setTcpNoDelay(true).build();

    return proxyFeedBackDecorateHttpClientBuilder
            .setDefaultSocketConfig(socketConfig)
            // .setSSLSocketFactory(sslConnectionSocketFactory)
            // dungproxy0.0.6之后的版本,默认忽略https证书检查
            .setRedirectStrategy(new LaxRedirectStrategy())
            //注意,这里使用ua生产算法自动产生ua,如果是mobile,可以使用
            // com.virjar.vscrawler.core.net.useragent.UserAgentBuilder.randomAppUserAgent()
            .setUserAgent(UserAgentBuilder.randomUserAgent())
            //对于爬虫来说,连接池没啥卵用,直接禁止掉(因为我们可能创建大量HttpClient,每个HttpClient一个连接池,会把系统socket资源撑爆)
            //测试开80个httpClient抓数据大概一个小时系统就会宕机
            .setConnectionReuseStrategy(NoConnectionReuseStrategy.INSTANCE)
            .build();

}
HttpClientFactory.java 文件源码 项目:axon-eventstore 阅读 22 收藏 0 点赞 0 评论 0
private static CloseableHttpClient newClosableCachingHttpClient(EventStoreSettings settings) {
   final CacheConfig cacheConfig = CacheConfig.custom()
         .setMaxCacheEntries(Integer.MAX_VALUE)
         .setMaxObjectSize(Integer.MAX_VALUE)
         .build();

   settings.getCacheDirectory()
         .mkdirs();

   return CachingHttpClientBuilder.create()
         .setHttpCacheStorage(new FileCacheStorage(cacheConfig, settings.getCacheDirectory()))
         .setCacheConfig(cacheConfig)
         .setDefaultRequestConfig(requestConfig(settings))
         .setDefaultCredentialsProvider(credentialsProvider(settings))
         .setRedirectStrategy(new LaxRedirectStrategy())
         .setRetryHandler(new StandardHttpRequestRetryHandler())
         .setKeepAliveStrategy(new de.qyotta.eventstore.utils.DefaultConnectionKeepAliveStrategy())
         .setConnectionManagerShared(true)

         .build();
}
GithubHttp.java 文件源码 项目:zest-writer 阅读 19 收藏 0 点赞 0 评论 0
public static String getGithubZipball(String owner, String repo, String destFolder) throws IOException {
    CloseableHttpClient httpclient = HttpClients.custom()
            .setRedirectStrategy(new LaxRedirectStrategy())
            .build();
    String urlForGet = "https://api.github.com/repos/" + owner + "/" + repo + "/zipball/";

    HttpGet get = new HttpGet(urlForGet);

    HttpResponse response = httpclient.execute(get);

    InputStream is = response.getEntity().getContent();
    String filePath = destFolder + File.separator + repo + ".zip";
    FileOutputStream fos = new FileOutputStream(new File(filePath));

    int inByte;
    while ((inByte = is.read()) != -1)
        fos.write(inByte);
    is.close();
    fos.close();
    return filePath;
}
HttpClientHelper.java 文件源码 项目:cosmic 阅读 17 收藏 0 点赞 0 评论 0
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();
}
MmsConnection.java 文件源码 项目:TextSecure 阅读 22 收藏 0 点赞 0 评论 0
protected CloseableHttpClient constructHttpClient()
    throws IOException {
  RequestConfig config = RequestConfig.custom()
                                      .setConnectTimeout(20 * 1000)
                                      .setConnectionRequestTimeout(20 * 1000)
                                      .setSocketTimeout(20 * 1000)
                                      .setMaxRedirects(20)
                                      .build();

  URL mmsc = new URL(apn.getMmsc());
  CredentialsProvider credsProvider = new BasicCredentialsProvider();

  if (apn.hasAuthentication()) {
    credsProvider.setCredentials(new AuthScope(mmsc.getHost(), mmsc.getPort() > -1 ? mmsc.getPort() : mmsc.getDefaultPort()),
                                 new UsernamePasswordCredentials(apn.getUsername(), apn.getPassword()));
  }

  return HttpClients.custom()
                    .setConnectionReuseStrategy(new NoConnectionReuseStrategyHC4())
                    .setRedirectStrategy(new LaxRedirectStrategy())
                    .setUserAgent("Android-Mms/2.0")
                    .setConnectionManager(new BasicHttpClientConnectionManager())
                    .setDefaultRequestConfig(config)
                    .setDefaultCredentialsProvider(credsProvider)
                    .build();
}
LoadImageImdb.java 文件源码 项目:vntscraper 阅读 15 收藏 0 点赞 0 评论 0
@Override
   public void doGet(IHTTPSession session, Response response) {
try {
    CloseableHttpClient httpClient = HttpClientBuilder.create().setRedirectStrategy(new LaxRedirectStrategy()).build();
    String url = session.getParms().get("image");
    HttpGet httpGet = new HttpGet(url);
    httpGet.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:43.0) Gecko/20100101 Firefox/43.0.3");
    httpGet.addHeader("Accept-Encoding", "gzip");
    CloseableHttpResponse imdbResponse = httpClient.execute(httpGet);
    HttpEntity httpEntity = imdbResponse.getEntity();

    response.setChunkedTransfer(true);
    response.setMimeType("image/jpeg");
    response.setData(httpEntity.getContent());
} catch (Exception e) {
    e.printStackTrace();
}
   }
OmdbAPI.java 文件源码 项目:vntscraper 阅读 13 收藏 0 点赞 0 评论 0
public OmdbResponse fetchResult() {
    OmdbResponse omdbResponse = null;
    try {
        CloseableHttpClient httpClient = HttpClientBuilder.create().setRedirectStrategy(new LaxRedirectStrategy()).build();

        List<NameValuePair> nameValuePairs = buildNameValuePair();

        String url = "http://www.omdbapi.com/?" + URLEncodedUtils.format(nameValuePairs, "UTF-8");
        log.debug("URL : " + url);
        HttpGet httpGet = new HttpGet(url);
        httpGet.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:43.0) Gecko/20100101 Firefox/43.0.3");
        httpGet.addHeader("Accept-Encoding", "gzip");
        CloseableHttpResponse imdbResponse = httpClient.execute(httpGet);

        HttpEntity httpEntity = imdbResponse.getEntity();

        String content = EntityUtils.toString(httpEntity);
        log.debug("content : " + content);
        //FIXME to use jackson objectmapper
        Gson gson = new GsonBuilder().serializeNulls().create();
        omdbResponse = gson.fromJson(content, OmdbResponse.class);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return omdbResponse;
}
KCauldronUpdater.java 文件源码 项目:KCauldron 阅读 17 收藏 0 点赞 0 评论 0
private static void download(String url, File destination)
        throws IOException {
    HttpUriRequest request = RequestBuilder
            .get()
            .setUri(url)
            .addParameter("hostname",
                    MinecraftServer.getServer().getHostname())
            .addParameter("port",
                    String.valueOf(MinecraftServer.getServer().getPort()))
            .build();
    CloseableHttpClient client = HttpClientBuilder.create()
            .setRedirectStrategy(new LaxRedirectStrategy())
            .setUserAgent("KCauldron Updater").build();

    HttpResponse response = client.execute(request);
    if (response.getStatusLine().getStatusCode() != 200) {
        client.close();
        throw new IllegalStateException("Could not download " + url);
    }
    InputStream is = response.getEntity().getContent();
    OutputStream os = new FileOutputStream(destination);
    IOUtils.copy(is, os);
    is.close();
    os.close();
    client.close();
}
LegacyMmsConnection.java 文件源码 项目:TextSecureSMP 阅读 18 收藏 0 点赞 0 评论 0
protected CloseableHttpClient constructHttpClient() throws IOException {
  RequestConfig config = RequestConfig.custom()
                                      .setConnectTimeout(20 * 1000)
                                      .setConnectionRequestTimeout(20 * 1000)
                                      .setSocketTimeout(20 * 1000)
                                      .setMaxRedirects(20)
                                      .build();

  URL                 mmsc          = new URL(apn.getMmsc());
  CredentialsProvider credsProvider = new BasicCredentialsProvider();

  if (apn.hasAuthentication()) {
    credsProvider.setCredentials(new AuthScope(mmsc.getHost(), mmsc.getPort() > -1 ? mmsc.getPort() : mmsc.getDefaultPort()),
                                 new UsernamePasswordCredentials(apn.getUsername(), apn.getPassword()));
  }

  return HttpClients.custom()
                    .setConnectionReuseStrategy(new NoConnectionReuseStrategyHC4())
                    .setRedirectStrategy(new LaxRedirectStrategy())
                    .setUserAgent(TextSecurePreferences.getMmsUserAgent(context, USER_AGENT))
                    .setConnectionManager(new BasicHttpClientConnectionManager())
                    .setDefaultRequestConfig(config)
                    .setDefaultCredentialsProvider(credsProvider)
                    .build();
}
LegacyMmsConnection.java 文件源码 项目:Silence 阅读 24 收藏 0 点赞 0 评论 0
protected CloseableHttpClient constructHttpClient() throws IOException {
  RequestConfig config = RequestConfig.custom()
                                      .setConnectTimeout(20 * 1000)
                                      .setConnectionRequestTimeout(20 * 1000)
                                      .setSocketTimeout(20 * 1000)
                                      .setMaxRedirects(20)
                                      .build();

  URL                 mmsc          = new URL(apn.getMmsc());
  CredentialsProvider credsProvider = new BasicCredentialsProvider();

  if (apn.hasAuthentication()) {
    credsProvider.setCredentials(new AuthScope(mmsc.getHost(), mmsc.getPort() > -1 ? mmsc.getPort() : mmsc.getDefaultPort()),
                                 new UsernamePasswordCredentials(apn.getUsername(), apn.getPassword()));
  }

  return HttpClients.custom()
                    .setConnectionReuseStrategy(new NoConnectionReuseStrategyHC4())
                    .setRedirectStrategy(new LaxRedirectStrategy())
                    .setUserAgent(SilencePreferences.getMmsUserAgent(context, USER_AGENT))
                    .setConnectionManager(new BasicHttpClientConnectionManager())
                    .setDefaultRequestConfig(config)
                    .setDefaultCredentialsProvider(credsProvider)
                    .build();
}
FormAuthenticationTest.java 文件源码 项目:elytron-web 阅读 17 收藏 0 点赞 0 评论 0
@Test
public void testSessionIdentityCache() throws Exception {
    HttpClient httpClient = HttpClientBuilder.create().setRedirectStrategy(new LaxRedirectStrategy()).build();
    HttpPost httpAuthenticate = new HttpPost(server.createUri("/j_security_check"));
    List<NameValuePair> parameters = new ArrayList<>(2);

    parameters.add(new BasicNameValuePair("j_username", "ladybird"));
    parameters.add(new BasicNameValuePair("j_password", "Coleoptera"));

    httpAuthenticate.setEntity(new UrlEncodedFormEntity(parameters));

    assertSuccessfulResponse(httpClient.execute(httpAuthenticate), "ladybird");

    for (int i = 0; i < 10; i++) {
        assertSuccessfulResponse(httpClient.execute(new HttpGet(server.createUri())), "ladybird");
    }

    assertEquals(1, realmIdentityInvocationCount.get());
}
AbstractApacheHttpClient.java 文件源码 项目:ksi-java-sdk 阅读 19 收藏 0 点赞 0 评论 0
/**
 * Creates asynchronous Apache HTTP client.
 *
 * @param settings
 *         settings to use to create client.
 * @param conf
 *         configuration related to async connection.
 * @return Instance of {@link CloseableHttpAsyncClient}.
 */
private CloseableHttpAsyncClient createClient(HttpSettings settings, ApacheHttpClientConfiguration conf) {
    IOReactorConfig ioReactor = IOReactorConfig.custom().setIoThreadCount(conf.getMaxThreadCount()).build();
    HttpAsyncClientBuilder httpClientBuilder = HttpAsyncClients.custom()
            .useSystemProperties()
                    // allow POST redirects
            .setRedirectStrategy(new LaxRedirectStrategy()).setMaxConnTotal(conf.getMaxTotalConnectionCount()).setMaxConnPerRoute(conf.getMaxRouteConnectionCount()).setDefaultIOReactorConfig(ioReactor)
            .setKeepAliveStrategy(new DefaultConnectionKeepAliveStrategy()).setDefaultRequestConfig(createDefaultRequestConfig(settings));
    if (settings.getProxyUrl() != null) {
        DefaultProxyRoutePlanner routePlanner = createProxyRoutePlanner(settings, httpClientBuilder);
        httpClientBuilder.setRoutePlanner(routePlanner);
    }
    CloseableHttpAsyncClient httpClient = httpClientBuilder.build();
    httpClient.start();
    return httpClient;
}
HTTPManager.java 文件源码 项目:mondo-hawk 阅读 20 收藏 0 点赞 0 评论 0
private CloseableHttpClient createClient() {
    final HttpClientBuilder builder = HttpClientBuilder.create();

    // Provide username and password if specified
    if (username != null) {
        final BasicCredentialsProvider credProvider = new BasicCredentialsProvider();
        credProvider.setCredentials(new AuthScope(new HttpHost(repositoryURL.getHost())),
                new UsernamePasswordCredentials(username, password));
        builder.setDefaultCredentialsProvider(credProvider);
    }

    // Follow redirects
    builder.setRedirectStrategy(new LaxRedirectStrategy());

    return builder.build();
}
HttpBuilder.java 文件源码 项目:scale.commons 阅读 23 收藏 0 点赞 0 评论 0
/**
 * Creates a new {@link HttpBuilder}. Without additional input, the builder
 * is set up to build {@link Http} instances with the following properties:
 * <ul>
 * <li>default connection timeout and socket timeout: 20 seconds</li>
 * <li>server authentication/verification (on SSL): none</li>
 * <li>client authentication: none</li>
 * <li>default request content type: application/json</li>
 * </ul>
 * All of these settings can be modified through the builder's methods.
 */
public HttpBuilder() {
    this.clientBuilder = HttpClients.custom();
    this.clientBuilder.setRedirectStrategy(new LaxRedirectStrategy());

    this.requestConfigBuilder = RequestConfig.copy(RequestConfig.DEFAULT);
    this.requestConfigBuilder.setConnectTimeout(DEFAULT_CONNECTION_TIMEOUT);
    this.requestConfigBuilder.setSocketTimeout(DEFAULT_SOCKET_TIMEOUT);

    this.sslContextBuilder = SslContextBuilder.newBuilder();
    verifyHostCert(false);
    verifyHostname(false);

    this.defaultHeaders = new HashMap<>();
    contentType(ContentType.APPLICATION_JSON);

    this.logger = Http.LOG;
}
ExtHttpClientStack.java 文件源码 项目:RESTImplementation 阅读 18 收藏 0 点赞 0 评论 0
public ExtHttpClientStack() throws VolleyError {
    try {
        SSLContext sslcontext = SSLContexts.custom()
                .loadTrustMaterial(null, new TrustStrategy() {
                    @Override
                    public boolean isTrusted(
                            final X509Certificate[] chain,
                            final String authType) throws CertificateException {
                        return true;
                    }
                }).build();

        client = HttpClients.custom()
                //.setUserAgent(USER_AGENT)
                .setRedirectStrategy(new LaxRedirectStrategy())
                .setSslcontext(sslcontext)
                .build();
    } catch (NoSuchAlgorithmException | KeyManagementException | KeyStoreException e) {
        throw new VolleyError("Cannot create HttpClient");
    }
}
MmsConnection.java 文件源码 项目:Securecom-Messaging 阅读 20 收藏 0 点赞 0 评论 0
protected CloseableHttpClient constructHttpClient()
    throws IOException {
  RequestConfig config = RequestConfig.custom()
                                      .setConnectTimeout(20 * 1000)
                                      .setConnectionRequestTimeout(20 * 1000)
                                      .setSocketTimeout(20 * 1000)
                                      .setMaxRedirects(20)
                                      .build();

  return HttpClients.custom()
                    .setConnectionReuseStrategy(new NoConnectionReuseStrategyHC4())
                    .setRedirectStrategy(new LaxRedirectStrategy())
                    .setUserAgent("Android-Mms/2.0")
                    .setConnectionManager(new BasicHttpClientConnectionManager())
                    .setDefaultRequestConfig(config)
                    .build();
}
UVersionRetriever.java 文件源码 项目:Uranium 阅读 14 收藏 0 点赞 0 评论 0
@Deprecated
private void check() {
    try {
        HttpUriRequest request = RequestBuilder
                .get()
                .setUri("https://api.prok.pw/repo/version/" + mGroup + "/"
                        + mName)
                .addParameter("version", Uranium.getCurrentVersion())
                .addParameter("hostname", sServer.getHostname())
                .addParameter("port", "" + sServer.getPort()).build();
        HttpResponse response = HttpClientBuilder.create()
                .setUserAgent("Uranium Version Retriever")
                .setRedirectStrategy(new LaxRedirectStrategy()).build()
                .execute(request);
        if (response.getStatusLine().getStatusCode() != 200) {
            uncaughtException(mThread, new IllegalStateException(
                    "Status code isn't OK"));
            return;
        }
        JSONObject json = (JSONObject) sParser.parse(new InputStreamReader(
                response.getEntity().getContent()));
        String version = (String) json.get("version");
        if (!mUpToDateSupport || Uranium.getCurrentVersion() == null
                || !version.equals(Uranium.getCurrentVersion())) {
            mCallback.newVersion(version);
        } else {
            mCallback.upToDate();
        }
    } catch (Exception e) {
        uncaughtException(null, e);
    }
}
ConnectionManager.java 文件源码 项目:aem-epic-tool 阅读 25 收藏 0 点赞 0 评论 0
public CloseableHttpClient getAuthenticatedClient(CredentialsProvider creds, HttpContext defaultContext) {
    CloseableHttpClient httpclient = HttpClients.custom()
            .setConnectionManager(connectionManager)
            .setConnectionManagerShared(true)
            .setRedirectStrategy(new LaxRedirectStrategy())
            .setDefaultCredentialsProvider(creds)
            .build();
    if (defaultContext != null) {
        this.defaultContext = defaultContext;
    }
    return httpclient;
}
HttpHelper.java 文件源码 项目:newblog 阅读 24 收藏 0 点赞 0 评论 0
/**
 * 描述:创建httpClient连接池,并初始化httpclient
 */
private void initHttpClient() throws ConfigurationException {
    Configuration configuration = new PropertiesConfiguration(CONFIG_FILE);
    //创建httpclient连接池
    PoolingHttpClientConnectionManager httpClientConnectionManager = new PoolingHttpClientConnectionManager();
    httpClientConnectionManager.setMaxTotal(configuration.getInt("http.max.total"));    //设置连接池线程最大数量
    httpClientConnectionManager.setDefaultMaxPerRoute(configuration.getInt("http.max.route"));    //设置单个路由最大的连接线程数量
    //创建http request的配置信息
    RequestConfig requestConfig = RequestConfig.custom()
            .setConnectionRequestTimeout(configuration.getInt("http.request.timeout"))
            .setSocketTimeout(configuration.getInt("http.socket.timeout"))
            .setCookieSpec(CookieSpecs.DEFAULT).build();
    //设置重定向策略
    LaxRedirectStrategy redirectStrategy = new LaxRedirectStrategy() {
        /**
         * false 禁止重定向  true 允许
         */
        @Override
        public boolean isRedirected(HttpRequest request,
                                    HttpResponse response, HttpContext context)
                throws ProtocolException {
            // TODO Auto-generated method stub
            return isRediect ? super.isRedirected(request, response, context) : isRediect;
        }
    };
    //初始化httpclient客户端
    httpClient = HttpClients.custom().setConnectionManager(httpClientConnectionManager)
            .setDefaultRequestConfig(requestConfig)
            //.setUserAgent(NewsConstant.USER_AGENT)
            .setRedirectStrategy(redirectStrategy)
            .build();
}
DatarouterHttpClientBuilder.java 文件源码 项目:datarouter 阅读 28 收藏 0 点赞 0 评论 0
public DatarouterHttpClientBuilder(){
    this.retryHandler = new DatarouterHttpRetryHandler();
    this.timeoutMs = DEFAULT_TIMEOUT_MS;
    this.maxTotalConnections = DEFAULT_MAX_TOTAL_CONNECTION;
    this.maxConnectionsPerRoute = DEFAULT_MAX_CONNECTION_PER_ROUTE;
    this.httpClientBuilder = HttpClientBuilder.create()
            .setRetryHandler(retryHandler)
            .setRedirectStrategy(new LaxRedirectStrategy());
}
HttpServiceImpl.java 文件源码 项目:Equella 阅读 21 收藏 0 点赞 0 评论 0
private DefaultHttpClient createClient(boolean https)
{
    final DefaultHttpClient client = (https ? new DefaultHttpClient(conMan) : new DefaultHttpClient());
    // Allows a slightly lenient cookie acceptance
    client.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY);
    // Allows follow of redirects on POST
    client.setRedirectStrategy(new LaxRedirectStrategy());
    return client;
}
LoginExecutor.java 文件源码 项目:security-karate 阅读 20 收藏 0 点赞 0 评论 0
/**
 * Perform a http login and return the acquired session ID.
 *
 * @param credentials the credentials to login with
 * @param csrfToken   the csrfToken form the login page
 *
 * @param forwardedForHeader
 * @return the sessionId if login was successful
 *
 * @throws IOException
 */
private Optional<String> login(Credentials credentials, String csrfToken, Header forwardedForHeader) throws IOException {
    Optional<String> sessionId;
    CloseableHttpClient httpclient = HttpClientBuilder.create()
            .setRedirectStrategy(new LaxRedirectStrategy())
            .build();

    try {
        HttpPost httpPost = new HttpPost(configuration.getLoginUrl());
        httpPost.setHeader(forwardedForHeader);

        List<NameValuePair> nvps = new ArrayList<>();
        nvps.add(new BasicNameValuePair("username", credentials.getUsername()));
        nvps.add(new BasicNameValuePair("password", credentials.getPassword()));
        nvps.add(new BasicNameValuePair("_csrf", csrfToken));

        String initialSession = getCurrentSession(context);

        httpPost.setEntity(new UrlEncodedFormEntity(nvps));
        CloseableHttpResponse response2 = httpclient.execute(httpPost, context);

        try {
            logger.debug(response2.getStatusLine().toString());
            sessionId = extractSessionId(context);
            if(initialSession != null && initialSession.equals(sessionId.orElse("nothing"))){
                return Optional.empty();
            }
        } finally {
            response2.close();
        }

    } finally {
        httpclient.close();
    }
    return sessionId;
}
HttpClientFactory.java 文件源码 项目:axon-eventstore 阅读 25 收藏 0 点赞 0 评论 0
private static CloseableHttpClient newClosableHttpClient(EventStoreSettings settings) {
   return HttpClientBuilder.create()

         .setDefaultRequestConfig(requestConfig(settings))
         .setDefaultCredentialsProvider(credentialsProvider(settings))
         .setRedirectStrategy(new LaxRedirectStrategy())
         .setRetryHandler(new StandardHttpRequestRetryHandler())
         .setKeepAliveStrategy(new de.qyotta.eventstore.utils.DefaultConnectionKeepAliveStrategy())
         .setConnectionManagerShared(true)

         .build();
}
ClientHttpRequestFactoryFactory.java 文件源码 项目:spring-vault 阅读 21 收藏 0 点赞 0 评论 0
static ClientHttpRequestFactory usingHttpComponents(ClientOptions options,
        SslConfiguration sslConfiguration) throws GeneralSecurityException,
        IOException {

    HttpClientBuilder httpClientBuilder = HttpClients.custom();

    httpClientBuilder.setRoutePlanner(new SystemDefaultRoutePlanner(
            DefaultSchemePortResolver.INSTANCE, ProxySelector.getDefault()));

    if (hasSslConfiguration(sslConfiguration)) {

        SSLContext sslContext = getSSLContext(sslConfiguration);
        SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(
                sslContext);
        httpClientBuilder.setSSLSocketFactory(sslSocketFactory);
        httpClientBuilder.setSSLContext(sslContext);
    }

    RequestConfig requestConfig = RequestConfig
            .custom()
            //
            .setConnectTimeout(
                    Math.toIntExact(options.getConnectionTimeout().toMillis())) //
            .setSocketTimeout(
                    Math.toIntExact(options.getReadTimeout().toMillis())) //
            .setAuthenticationEnabled(true) //
            .build();

    httpClientBuilder.setDefaultRequestConfig(requestConfig);

    // Support redirects
    httpClientBuilder.setRedirectStrategy(new LaxRedirectStrategy());

    return new HttpComponentsClientHttpRequestFactory(httpClientBuilder.build());
}
ZdsHttp.java 文件源码 项目:zest-writer 阅读 19 收藏 0 点赞 0 评论 0
public static String getZdsZipball(String id, String slug, String type, String destFolder) throws IOException{
        CloseableHttpClient httpclient = HttpClients.custom()
                .setRedirectStrategy(new LaxRedirectStrategy())
                .build();
        String urlForGet = "https://zestedesavoir.com/" + type + "/zip/"+ id + "/" + slug + ".zip";
        log.debug("Tentative de téléchargement du lien "+urlForGet);
        log.debug("Répertoire de téléchargement cible : "+destFolder);

        HttpGet get = new HttpGet(urlForGet);

        log.debug("Execution de la requete http");

        HttpResponse response = httpclient.execute(get);

        InputStream is = response.getEntity().getContent();
        String filePath = destFolder + File.separator + slug + ".zip";
        FileOutputStream fos = new FileOutputStream(new File(filePath));


        log.debug("Début du téléchargement");
        int inByte;
        while ((inByte = is.read()) != -1)
            fos.write(inByte);
        is.close();
        fos.close();
        log.debug("Archive téléchargée : "+filePath);
        return filePath;
}
TLSEvaluationStage.java 文件源码 项目:dcos-commons 阅读 39 收藏 0 点赞 0 评论 0
/**
 * Creates a new builder instance. Callers should avoid invoking this until/unless they have validated that TLS
 * functionality is needed.
 *
 * @throws IOException if the necessary clients could not be built, which may occur if the cluster doesn't
 *                     support TLS
 */
public Builder(String serviceName, SchedulerConfig schedulerConfig) throws IOException {
    this.serviceName = serviceName;
    this.namespace = schedulerConfig.getSecretsNamespace(serviceName);
    DcosHttpExecutor executor = new DcosHttpExecutor(new DcosHttpClientBuilder()
            .setTokenProvider(schedulerConfig.getDcosAuthTokenProvider())
            .setRedirectStrategy(new LaxRedirectStrategy() {
                protected boolean isRedirectable(String method) {
                    // Also treat PUT calls as redirectable
                    return method.equalsIgnoreCase(HttpPut.METHOD_NAME) || super.isRedirectable(method);
                }
            }));
    this.tlsArtifactsUpdater = new TLSArtifactsUpdater(
            serviceName, new SecretsClient(executor), new CertificateAuthorityClient(executor));
}
ConnectionHelper.java 文件源码 项目:hotel 阅读 28 收藏 0 点赞 0 评论 0
public void login(String host, int port, String protocol, String username, String password, Boolean useProxy) {
    target = new HttpHost(host, port, protocol);
    cookieStore = new BasicCookieStore();
    httpClientContext = HttpClientContext.create();
    httpClientContext.setCookieStore(cookieStore);
    if (useProxy) // to allow us debugging with fiddler
        requestConfig = RequestConfig.custom().setProxy(new HttpHost("127.0.0.1", 8888, "http")).build();
    else
        requestConfig = RequestConfig.custom().build();
    closeableHttpClient = HttpClients.custom().setRedirectStrategy(new LaxRedirectStrategy()).build();
    HttpUriRequest request = RequestBuilder.get().setUri("/").setConfig(requestConfig).build();
    try {
        CloseableHttpResponse response = closeableHttpClient.execute(target, request, httpClientContext);
        csrfToken = response.getFirstHeader("X-CSRF-TOKEN").getValue();
        EntityUtils.toString(response.getEntity());
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("username", username));
        params.add(new BasicNameValuePair("password", password));
        params.add(new BasicNameValuePair("remember-me", "true"));
        params.add(new BasicNameValuePair("_csrf", csrfToken));
        request = RequestBuilder.post().setUri("/login").setEntity(new UrlEncodedFormEntity(params))
                .setConfig(requestConfig).setHeader("X-CSRF-TOKEN", csrfToken).build();
        response = closeableHttpClient.execute(target, request, httpClientContext);
        csrfToken = response.getFirstHeader("X-CSRF-TOKEN").getValue();
        String source = EntityUtils.toString(response.getEntity());
        if (source.contains("<li><a href=\"/login\">"))
            Context.setMessage("Logowanie się nie powiodło", "red");
        else {
            logged = true;
            Context.setMessage("Zalogowano pomyślnie", "green");
        }
    } catch (Exception ex) {
        Context.setMessage("Wystąpił problem z połączeniem", "red");
    }
}


问题


面经


文章

微信
公众号

扫码关注公众号