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(); }
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(); }
@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(); }
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(); }
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; }
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(); }
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(); }
@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(); } }
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; }
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(); }
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(); }
@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()); }
/** * 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; }
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(); }
/** * 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; }
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"); } }
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(); }
@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); } }
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; }
/** * 描述:创建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(); }
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()); }
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; }
/** * 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; }
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(); }
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()); }
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; }
/** * 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)); }
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"); } }
/** * get resource body from OAuth2 resource response using HTTP GET method * * @param uri * resource URI * @param parameters * resource parameters * @return resource body * @throws OAuthSystemException * If an OAuth system exception occurs * @throws OAuthProblemException * If an OAuth problem exception occurs */ public String get(String uri, Map<String, String> parameters) throws OAuthSystemException, OAuthProblemException { CloseableHttpClient client = HttpClients.custom().setRedirectStrategy(new LaxRedirectStrategy()) .build(); try { return get(client, uri, parameters); } finally { try { client.close(); } catch (IOException e) { // this can be safely ignored } } }
/** * get resource body from OAuth2 resource response using HTTP POST method with * UTF-8 encoded parameters * * @param uri * resource URI * @param parameters * resource parameters * @return resource body * @throws OAuthSystemException * If an OAuth system exception occurs * @throws OAuthProblemException * If an OAuth problem exception occurs */ public String post(String uri, Map<String, String> parameters) throws OAuthSystemException, OAuthProblemException { CloseableHttpClient client = HttpClients.custom().setRedirectStrategy(new LaxRedirectStrategy()) .build(); try { return post(client, uri, parameters); } finally { try { client.close(); } catch (IOException e) { // this can be safely ignored } } }
private CloseableHttpClient createHttpClient(ProxySettings proxySettings) { HttpClientBuilder httpClientBuilder = HttpClientBuilder.create() /* set user agent string */ .setUserAgent(USER_AGENT) /* redirect on POSTs, too */ .setRedirectStrategy(new LaxRedirectStrategy()) /* increase max total connections from 20 to 200 */ .setMaxConnTotal(200) /* increate max connections per route from 2 to 200 (we only have one route) */ .setMaxConnPerRoute(200) /* set retry handler with maximum 3 retries and requestSentRetryEnabled=true so that even POST requests are retried (default is false) */ .setRetryHandler(new DefaultHttpRequestRetryHandler(3, true)) .setDefaultCookieStore(cookieStore) .setDefaultRequestConfig(RequestConfig.custom() /* timeout in milliseconds until a connection is established, in ms */ .setConnectTimeout(SOCKET_TIMEOUT * 1000) /* socket timeout in milliseconds, which is the timeout for waiting for data */ .setSocketTimeout(SOCKET_TIMEOUT * 1000) /* don't use "Expect: 100-continue" because some proxies don't understand */ .setExpectContinueEnabled(false) /* need to relax default cookie policy to avoid problem with cookies with invalid expiry dates */ .setCookieSpec(CookieSpecs.BROWSER_COMPATIBILITY) .build()); if (proxySettings != null) { httpClientBuilder.setProxy(new HttpHost(proxySettings.getHost(), proxySettings.getPort())); } return httpClientBuilder.build(); }
/** * @param mProxyHost * @return */ public static synchronized HttpClient creteHttpClient(CookieStore mCookieStore, HttpHost... mProxyHost) { if (null == customerHttpClient) { HttpClientBuilder customBuilder = HttpClients.custom(); PoolingHttpClientConnectionManager manager = new PoolingHttpClientConnectionManager(); manager.setMaxTotal(10); manager.setDefaultMaxPerRoute(10); customBuilder.setConnectionManager(manager); LaxRedirectStrategy redirectStrategy = new LaxRedirectStrategy(); customBuilder.setRedirectStrategy(redirectStrategy); customBuilder.setDefaultCookieStore(mCookieStore); RequestConfig requestConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.BEST_MATCH).build(); customBuilder.setDefaultRequestConfig(requestConfig); for (HttpHost mHost : mProxyHost) { customBuilder.setProxy(mHost); } customerHttpClient = customBuilder.build(); // HttpClientParams.setCookiePolicy(customerHttpClient.getParams(), // CookiePolicy.BROWSER_COMPATIBILITY); } return customerHttpClient; }
private void check() { try { HttpUriRequest request = RequestBuilder .get() .setUri("http://th.tcpr.ca/thermos/version") .addParameter("version", Thermos.getCurrentVersion()).build(); HttpResponse response = HttpClientBuilder.create() .setUserAgent("Thermos 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 || Thermos.getCurrentVersion() == null || !version.equals(Thermos.getCurrentVersion())) { mCallback.newVersion(version); } else { mCallback.upToDate(); } } catch (Exception e) { uncaughtException(null, e); } }
private void check() { try { HttpUriRequest request = RequestBuilder .get() .setUri("https://api.prok.pw/repo/version/" + mGroup + "/" + mName) .addParameter("version", KCauldron.getCurrentVersion()) .addParameter("hostname", sServer.getHostname()) .addParameter("port", "" + sServer.getPort()).build(); HttpResponse response = HttpClientBuilder.create() .setUserAgent("KCauldron 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 || KCauldron.getCurrentVersion() == null || !version.equals(KCauldron.getCurrentVersion())) { mCallback.newVersion(version); } else { mCallback.upToDate(); } } catch (Exception e) { uncaughtException(null, e); } }
private HttpResponse getHTTPResponse() throws IOException, NullPointerException { if (fileURI == null) { throw new NullPointerException("No file URI specified"); } HttpClientBuilder clientBuilder = HttpClientBuilder.create(); if (followRedirects) { clientBuilder.setRedirectStrategy(new LaxRedirectStrategy()); } HttpClient client = clientBuilder.build(); BasicHttpContext localContext = new BasicHttpContext(); // Clear down the local cookie store every time to make sure we don't have any left over // cookies influencing the test localContext.setAttribute(HttpClientContext.COOKIE_STORE, null); LOG.info("Mimic WebDriver cookie state: " + mimicWebDriverCookieState); if (mimicWebDriverCookieState) { localContext.setAttribute(HttpClientContext.COOKIE_STORE, mimicCookieState(driver.manage().getCookies())); } HttpRequestBase requestMethod = httpRequestMethod.getRequestMethod(); requestMethod.setURI(fileURI); // TODO if post send map of variables, also need to add a post map setter LOG.info("Sending " + httpRequestMethod.toString() + " request for: " + fileURI); return client.execute(requestMethod, localContext); }
@Test public void testFormSuccessfulAuthentication() 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"); assertSuccessfulResponse(httpClient.execute(httpAuthenticate), "ladybird"); }
/** * Create the httpClient * * @return */ public static DefaultHttpClient createHttpClient() { DefaultHttpClient httpClient = new DefaultHttpClient(); httpClient.setRedirectStrategy(new LaxRedirectStrategy()); return httpClient; }