private Answer<HttpResponse> createOkResponseWithCookie() { return new Answer<HttpResponse>() { @Override public HttpResponse answer(InvocationOnMock invocation) throws Throwable { HttpContext context = (HttpContext) invocation.getArguments()[1]; if (context.getAttribute(ClientContext.COOKIE_STORE) != null) { BasicCookieStore cookieStore = (BasicCookieStore) context.getAttribute(ClientContext.COOKIE_STORE); BasicClientCookie cookie = new BasicClientCookie("cookie", "meLikeCookie"); cookieStore.addCookie(cookie); } return OK_200_RESPONSE; } }; }
public QMailHttpClient getHttpClient() throws MessagingException { if (httpClient == null) { httpClient = httpClientFactory.create(); // Disable automatic redirects on the http client. httpClient.getParams().setBooleanParameter("http.protocol.handle-redirects", false); // Setup a cookie store for forms-based authentication. httpContext = new BasicHttpContext(); authCookies = new BasicCookieStore(); httpContext.setAttribute(ClientContext.COOKIE_STORE, authCookies); SchemeRegistry reg = httpClient.getConnectionManager().getSchemeRegistry(); try { Scheme s = new Scheme("https", new WebDavSocketFactory(hostname, 443), 443); reg.register(s); } catch (NoSuchAlgorithmException nsa) { Timber.e(nsa, "NoSuchAlgorithmException in getHttpClient"); throw new MessagingException("NoSuchAlgorithmException in getHttpClient: ", nsa); } catch (KeyManagementException kme) { Timber.e(kme, "KeyManagementException in getHttpClient"); throw new MessagingException("KeyManagementException in getHttpClient: ", kme); } } return httpClient; }
protected HttpContext createHttpContext() { HttpContext context = new BasicHttpContext(); context.setAttribute( ClientContext.SCHEME_REGISTRY, getConnectionManager().getSchemeRegistry()); context.setAttribute( ClientContext.AUTHSCHEME_REGISTRY, getAuthSchemes()); context.setAttribute( ClientContext.COOKIESPEC_REGISTRY, getCookieSpecs()); context.setAttribute( ClientContext.COOKIE_STORE, getCookieStore()); context.setAttribute( ClientContext.CREDS_PROVIDER, getCredentialsProvider()); return context; }
public void authSucceeded( final HttpHost authhost, final AuthScheme authScheme, final HttpContext context) { if (authhost == null) { throw new IllegalArgumentException("Host may not be null"); } if (authScheme == null) { throw new IllegalArgumentException("Auth scheme may not be null"); } if (context == null) { throw new IllegalArgumentException("HTTP context may not be null"); } if (isCachable(authScheme)) { AuthCache authCache = (AuthCache) context.getAttribute(ClientContext.AUTH_CACHE); if (authCache == null) { authCache = new BasicAuthCache(); context.setAttribute(ClientContext.AUTH_CACHE, authCache); } if (this.log.isDebugEnabled()) { this.log.debug("Caching '" + authScheme.getSchemeName() + "' auth scheme for " + authhost); } authCache.put(authhost, authScheme); } }
public void authFailed( final HttpHost authhost, final AuthScheme authScheme, final HttpContext context) { if (authhost == null) { throw new IllegalArgumentException("Host may not be null"); } if (context == null) { throw new IllegalArgumentException("HTTP context may not be null"); } AuthCache authCache = (AuthCache) context.getAttribute(ClientContext.AUTH_CACHE); if (authCache != null) { if (this.log.isDebugEnabled()) { this.log.debug("Clearing cached auth scheme for " + authhost); } authCache.remove(authhost); } }
private byte[] downloadHTTPfile_post(String formToDownloadLocation, List<NameValuePair> params) throws IOException, NullPointerException, URISyntaxException { BasicHttpContext localContext = new BasicHttpContext(); LOG.info("Mimic WebDriver cookie state: " + this.mimicWebDriverCookieState); if (this.mimicWebDriverCookieState) { localContext.setAttribute(ClientContext.COOKIE_STORE, mimicCookieState(this.driver.manage().getCookies())); } HttpPost httppost = new HttpPost(formToDownloadLocation); HttpParams httpRequestParameters = httppost.getParams(); httpRequestParameters.setParameter(ClientPNames.HANDLE_REDIRECTS, this.followRedirects); httppost.setParams(httpRequestParameters); httppost.setEntity(new UrlEncodedFormEntity(params, "UTF-8")); LOG.info("Sending POST request for: " + httppost.getURI()); @SuppressWarnings("resource") HttpResponse response = new DefaultHttpClient().execute(httppost, localContext); this.httpStatusOfLastDownloadAttempt = response.getStatusLine().getStatusCode(); LOG.info("HTTP GET request status: " + this.httpStatusOfLastDownloadAttempt); byte[] file = IOUtils.toByteArray(response.getEntity().getContent()); response.getEntity().getContent().close(); return file; }
/** * Perform an HTTP Status check and return the response code * * @return * @throws IOException */ @SuppressWarnings("resource") public int getHTTPStatusCode() throws IOException { HttpClient client = new DefaultHttpClient(); BasicHttpContext localContext = new BasicHttpContext(); LOG.info("Mimic WebDriver cookie state: " + this.mimicWebDriverCookieState); if (this.mimicWebDriverCookieState) { localContext.setAttribute(ClientContext.COOKIE_STORE, mimicCookieState(this.driver.manage().getCookies())); } HttpRequestBase requestMethod = this.httpRequestMethod.getRequestMethod(); requestMethod.setURI(this.linkToCheck); HttpParams httpRequestParameters = requestMethod.getParams(); httpRequestParameters.setParameter(ClientPNames.HANDLE_REDIRECTS, this.followRedirects); requestMethod.setParams(httpRequestParameters); LOG.info("Sending " + requestMethod.getMethod() + " request for: " + requestMethod.getURI()); HttpResponse response = client.execute(requestMethod, localContext); LOG.info("HTTP " + requestMethod.getMethod() + " request status: " + response.getStatusLine().getStatusCode()); return response.getStatusLine().getStatusCode(); }
private synchronized void checkLocalContext() { if (null != sdkProtocolAdatperCustProvider && null != target) { // Create AuthCache instance AuthCache authCache = new BasicAuthCache(); // Generate DIGEST scheme object, initialize it and add it to the local auth cache String authType = (String) ThreadLocalHolder.get().getEntities().get("AuthType"); if ("Basic".equals(authType)) { LOGGER.debug("authentication type: basic"); } else { DigestScheme digestAuth = new DigestScheme(); digestAuth.overrideParamter("nc", String.valueOf(serverNounceCount++)); digestAuth.overrideParamter("cnonce", UUID.randomUUID().toString().replaceAll("-", "")); digestAuth.overrideParamter("qop", "auth"); authCache.put(target, digestAuth); } // Add AuthCache to the execution context localContext = new BasicHttpContext(); localContext.setAttribute(ClientContext.AUTH_CACHE, authCache); } }
private synchronized void checkLocalContext() { if (null != sdkProtocolAdatperCustProvider && null != target) { // Create AuthCache instance AuthCache authCache = new BasicAuthCache(); // Generate DIGEST scheme object, initialize it and add it to the local auth cache String authType = (String)ThreadLocalHolder.get().getEntities().get("AuthType"); if ("Basic".equals(authType)) { LOGGER.debug("authentication type: basic"); } else { DigestScheme digestAuth = new DigestScheme(); digestAuth.overrideParamter("nc", String.valueOf(serverNounceCount++)); digestAuth.overrideParamter("cnonce", UUID.randomUUID().toString().replaceAll("-", "")); digestAuth.overrideParamter("qop", "auth"); authCache.put(target, digestAuth); } // Add AuthCache to the execution context localContext = new BasicHttpContext(); localContext.setAttribute(ClientContext.AUTH_CACHE, authCache); } }
public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException { AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE); // If no auth scheme avaialble yet, try to initialize it preemptively if (authState.getAuthScheme() == null) { AuthScheme authScheme = (AuthScheme) context.getAttribute("preemptive-auth"); CredentialsProvider credsProvider = (CredentialsProvider) context.getAttribute(ClientContext.CREDS_PROVIDER); HttpHost targetHost = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST); if (authScheme != null) { Credentials creds = credsProvider.getCredentials(new AuthScope(targetHost.getHostName(), targetHost.getPort())); if (creds == null) { throw new HttpException("No credentials for preemptive authentication"); } authState.setAuthScheme(authScheme); authState.setCredentials(creds); } } }
public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException { // // get the authentication state for the request // AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE); // // if no auth scheme avaialble yet, try to initialize it preemptively // if (authState.getAuthScheme() == null) { // check if credentials are set if (this.credentials == null) { throw new HttpException("No credentials for preemptive authentication"); } // add the credentials to the auth state authState.setAuthScheme(this.basicAuthScheme); authState.setCredentials(this.credentials); // HttpHost targetHost = (HttpHost)context.getAttribute(ExecutionContext.HTTP_TARGET_HOST); //CredentialsProvider credsProvider = (CredentialsProvider)context.getAttribute(ClientContext.CREDS_PROVIDER); } }
public WebDavHttpClient getHttpClient() throws MessagingException { if (mHttpClient == null) { mHttpClient = mHttpClientFactory.create(); // Disable automatic redirects on the http client. mHttpClient.getParams().setBooleanParameter("http.protocol.handle-redirects", false); // Setup a cookie store for forms-based authentication. mContext = new BasicHttpContext(); mAuthCookies = new BasicCookieStore(); mContext.setAttribute(ClientContext.COOKIE_STORE, mAuthCookies); SchemeRegistry reg = mHttpClient.getConnectionManager().getSchemeRegistry(); try { Scheme s = new Scheme("https", new WebDavSocketFactory(mHost, 443), 443); reg.register(s); } catch (NoSuchAlgorithmException nsa) { Log.e(LOG_TAG, "NoSuchAlgorithmException in getHttpClient: " + nsa); throw new MessagingException("NoSuchAlgorithmException in getHttpClient: " + nsa); } catch (KeyManagementException kme) { Log.e(LOG_TAG, "KeyManagementException in getHttpClient: " + kme); throw new MessagingException("KeyManagementException in getHttpClient: " + kme); } } return mHttpClient; }
public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException { AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE); // If no auth scheme has been initialized yet if (authState.getAuthScheme() == null) { CredentialsProvider credentialsProvider = (CredentialsProvider) context.getAttribute(ClientContext.CREDS_PROVIDER); HttpHost targetHost = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST); AuthScope authScope = new AuthScope(targetHost.getHostName(), targetHost.getPort()); // Obtain credentials matching the target host Credentials credentials = credentialsProvider.getCredentials(authScope); // If found, generate BasicScheme preemptively if (credentials != null) { authState.setAuthScheme(new DiadocAuthScheme()); authState.setCredentials(credentials); } } }
@Override public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException { AuthState authState = (AuthState)context.getAttribute(ClientContext.TARGET_AUTH_STATE); CredentialsProvider credsProvider = (CredentialsProvider)context .getAttribute(ClientContext.CREDS_PROVIDER); HttpHost targetHost = (HttpHost)context.getAttribute(ExecutionContext.HTTP_TARGET_HOST); // If not auth scheme has been initialized yet if (authState.getAuthScheme() == null) { AuthScope authScope = new AuthScope(targetHost.getHostName(), targetHost.getPort()); // Obtain credentials matching the target host org.apache.http.auth.Credentials creds = credsProvider.getCredentials(authScope); // If found, generate BasicScheme preemptively if (creds != null) { authState.setAuthScheme(new BasicScheme()); authState.setCredentials(creds); } } }
@Override public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException { AuthState authState = (AuthState)context.getAttribute(ClientContext.TARGET_AUTH_STATE); CredentialsProvider credsProvider = (CredentialsProvider)context .getAttribute(ClientContext.CREDS_PROVIDER); HttpHost targetHost = (HttpHost)context.getAttribute(ExecutionContext.HTTP_TARGET_HOST); // If not auth scheme has been initialized yet if (authState.getAuthScheme() == null) { AuthScope authScope = new AuthScope(targetHost.getHostName(), targetHost.getPort()); org.apache.http.auth.Credentials creds = credsProvider.getCredentials(authScope); if (creds != null) { authState.setAuthScheme(new BasicScheme()); authState.setCredentials(creds); } } }
@Override public void authSucceeded( final HttpHost authhost, final AuthScheme authScheme, final HttpContext context) { AuthCache authCache = (AuthCache) context.getAttribute(ClientContext.AUTH_CACHE); if (isCachable(authScheme)) { if (authCache == null) { authCache = new BasicAuthCache(); context.setAttribute(ClientContext.AUTH_CACHE, authCache); } if (this.log.isDebugEnabled()) { this.log.debug("Caching '" + authScheme.getSchemeName() + "' auth scheme for " + authhost); } authCache.put(authhost, authScheme); } }
protected HttpContext createHttpContext() { final HttpContext context = new BasicHttpContext(); context.setAttribute( ClientContext.SCHEME_REGISTRY, getConnectionManager().getSchemeRegistry()); context.setAttribute( ClientContext.AUTHSCHEME_REGISTRY, getAuthSchemes()); context.setAttribute( ClientContext.COOKIESPEC_REGISTRY, getCookieSpecs()); context.setAttribute( ClientContext.COOKIE_STORE, getCookieStore()); context.setAttribute( ClientContext.CREDS_PROVIDER, getCredentialsProvider()); return context; }
private AndroidHttpClient(ClientConnectionManager ccm, HttpParams params) { this.delegate = new DefaultHttpClient(ccm, params) { @Override protected BasicHttpProcessor createHttpProcessor() { // Add interceptor to prevent making requests from main thread. BasicHttpProcessor processor = super.createHttpProcessor(); processor.addRequestInterceptor(sThreadCheckInterceptor); return processor; } @Override protected HttpContext createHttpContext() { // Same as DefaultHttpClient.createHttpContext() minus the // cookie store. HttpContext context = new BasicHttpContext(); context.setAttribute(ClientContext.AUTHSCHEME_REGISTRY, getAuthSchemes()); context.setAttribute(ClientContext.COOKIESPEC_REGISTRY, getCookieSpecs()); context.setAttribute(ClientContext.CREDS_PROVIDER, getCredentialsProvider()); return context; } }; }
public WebDavHttpClient getHttpClient() throws MessagingException { if (mHttpClient == null) { mHttpClient = new WebDavHttpClient(); // Disable automatic redirects on the http client. mHttpClient.getParams().setBooleanParameter("http.protocol.handle-redirects", false); // Setup a cookie store for forms-based authentication. mContext = new BasicHttpContext(); mAuthCookies = new BasicCookieStore(); mContext.setAttribute(ClientContext.COOKIE_STORE, mAuthCookies); SchemeRegistry reg = mHttpClient.getConnectionManager().getSchemeRegistry(); try { Scheme s = new Scheme("https", new WebDavSocketFactory(mHost, 443), 443); reg.register(s); } catch (NoSuchAlgorithmException nsa) { Log.e(K9.LOG_TAG, "NoSuchAlgorithmException in getHttpClient: " + nsa); throw new MessagingException("NoSuchAlgorithmException in getHttpClient: " + nsa); } catch (KeyManagementException kme) { Log.e(K9.LOG_TAG, "KeyManagementException in getHttpClient: " + kme); throw new MessagingException("KeyManagementException in getHttpClient: " + kme); } } return mHttpClient; }
public CookieRestTemplate() { httpContext = new BasicHttpContext(); cookieStore = new BasicCookieStore(); httpContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore); HttpParams params = new BasicHttpParams(); HttpClientParams.setRedirecting(params, false); //REGISTERS SCHEMES FOR BOTH HTTP AND HTTPS SchemeRegistry registry = new SchemeRegistry(); registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); final SSLSocketFactory sslSocketFactory = SSLSocketFactory.getSocketFactory(); sslSocketFactory.setHostnameVerifier(SSLSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER); registry.register(new Scheme("https", sslSocketFactory, 443)); ClientConnectionManager conman = new ThreadSafeClientConnManager(params, registry); httpClient = new DefaultHttpClient(conman, params); HttpComponentsClientHttpRequestFactory hcchrf = new StatefulHttpComponentsClientHttpRequestFactory(httpClient, httpContext); hcchrf.setConnectTimeout(30 * 1000); setRequestFactory(hcchrf); }
String get_url_contents( String url , List<NameValuePair> params , CookieStore cookieStore ) { try { HttpClient client = MySSLSocketFactory.createMyHttpClient(); client.getParams().setParameter(CoreProtocolPNames.USER_AGENT, USER_AGENT); HttpResponse response = null; if( cookieStore == null ) response = client.execute( new HttpGet( params == null || params.size() == 0 ? url : url + "?" + URLEncodedUtils.format(params, "utf-8") ) ); else { HttpContext mHttpContext = new BasicHttpContext(); mHttpContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore); response = client.execute( new HttpGet( params == null || params.size() == 0 ? url : url + "?" + URLEncodedUtils.format(params, "utf-8") ) , mHttpContext ); } HttpEntity result = response.getEntity(); if( result != null ) { InputStream mInputStream = result.getContent(); String out = getStringFromInputStream(mInputStream); mInputStream.close(); return out; } } catch (Exception e) { e.printStackTrace(); } return ""; }
public static BasicHttpContext setCredentials(HttpClientBuilder builder, HttpHost httpHost, String username,String password, boolean preAuth) { // set Username and Password if(!StringUtil.isEmpty(username,true)) { if(password==null)password=""; CredentialsProvider cp = new BasicCredentialsProvider(); builder.setDefaultCredentialsProvider(cp); cp.setCredentials( new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT), new UsernamePasswordCredentials(username,password)); BasicHttpContext httpContext = new BasicHttpContext(); if(preAuth) { AuthCache authCache = new BasicAuthCache(); authCache.put(httpHost, new BasicScheme()); httpContext.setAttribute(ClientContext.AUTH_CACHE, authCache); } return httpContext; } return null; }
@Override public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException { AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE); // If no auth scheme available yet, try to initialize it preemptively if (authState.getAuthScheme() == null) { AuthScheme authScheme = (AuthScheme) context.getAttribute("preemptive-auth"); CredentialsProvider credsProvider = (CredentialsProvider) context.getAttribute(ClientContext.CREDS_PROVIDER); HttpHost targetHost = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST); if (authScheme != null) { Credentials creds = credsProvider.getCredentials(new AuthScope(targetHost.getHostName(), targetHost.getPort())); if (creds != null) { authState.setAuthScheme(authScheme); authState.setCredentials(creds); } } } }
public void authSucceeded( final HttpHost authhost, final AuthScheme authScheme, final HttpContext context) { AuthCache authCache = (AuthCache) context.getAttribute(ClientContext.AUTH_CACHE); if (isCachable(authScheme)) { if (authCache == null) { authCache = new BasicAuthCache(); context.setAttribute(ClientContext.AUTH_CACHE, authCache); } if (this.log.isDebugEnabled()) { this.log.debug("Caching '" + authScheme.getSchemeName() + "' auth scheme for " + authhost); } authCache.put(authhost, authScheme); } }
public void authFailed( final HttpHost authhost, final AuthScheme authScheme, final HttpContext context) { AuthCache authCache = (AuthCache) context.getAttribute(ClientContext.AUTH_CACHE); if (authCache == null) { return; } if (this.log.isDebugEnabled()) { this.log.debug("Removing from cache '" + authScheme.getSchemeName() + "' auth scheme for " + authhost); } authCache.remove(authhost); }
public Object getUserToken(final HttpContext context) { Principal userPrincipal = null; AuthState targetAuthState = (AuthState) context.getAttribute( ClientContext.TARGET_AUTH_STATE); if (targetAuthState != null) { userPrincipal = getAuthPrincipal(targetAuthState); if (userPrincipal == null) { AuthState proxyAuthState = (AuthState) context.getAttribute( ClientContext.PROXY_AUTH_STATE); userPrincipal = getAuthPrincipal(proxyAuthState); } } if (userPrincipal == null) { HttpRoutedConnection conn = (HttpRoutedConnection) context.getAttribute( ExecutionContext.HTTP_CONNECTION); if (conn.isOpen()) { SSLSession sslsession = conn.getSSLSession(); if (sslsession != null) { userPrincipal = sslsession.getLocalPrincipal(); } } } return userPrincipal; }
private AndroidHttpClient(ClientConnectionManager ccm, HttpParams params) { this.delegate = new DefaultHttpClient(ccm, params) { @Override protected BasicHttpProcessor createHttpProcessor() { // Add interceptor to prevent making requests from main thread. BasicHttpProcessor processor = super.createHttpProcessor(); processor.addRequestInterceptor(sThreadCheckInterceptor); processor.addRequestInterceptor(new CurlLogger()); return processor; } @Override protected HttpContext createHttpContext() { // Same as DefaultHttpClient.createHttpContext() minus the // cookie store. HttpContext context = new BasicHttpContext(); context.setAttribute( ClientContext.AUTHSCHEME_REGISTRY, getAuthSchemes()); context.setAttribute( ClientContext.COOKIESPEC_REGISTRY, getCookieSpecs()); context.setAttribute( ClientContext.CREDS_PROVIDER, getCredentialsProvider()); return context; } }; }
public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException { AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE); CredentialsProvider credsProvider = (CredentialsProvider) context.getAttribute( ClientContext.CREDS_PROVIDER); HttpHost targetHost = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST); if (authState.getAuthScheme() == null) { AuthScope authScope = new AuthScope(targetHost.getHostName(), targetHost.getPort()); Credentials creds = credsProvider.getCredentials(authScope); if (creds != null) { authState.setAuthScheme(new BasicScheme()); authState.setCredentials(creds); } } }
private byte[] downloadHTTPfile(String fileToDownloadLocation) throws IOException, NullPointerException, URISyntaxException { URL fileToDownload = new URL(fileToDownloadLocation); File downloadedFile = new File(this.localDownloadPath + fileToDownload.getFile().replaceFirst("/|\\\\", "")); if (downloadedFile.canWrite() == false) downloadedFile.setWritable(true); @SuppressWarnings("resource") HttpClient client = new DefaultHttpClient(); BasicHttpContext localContext = new BasicHttpContext(); LOG.info("Mimic WebDriver cookie state: " + this.mimicWebDriverCookieState); if (this.mimicWebDriverCookieState) { localContext.setAttribute(ClientContext.COOKIE_STORE, mimicCookieState(this.driver.manage().getCookies())); } HttpGet httpget = new HttpGet(fileToDownload.toURI()); HttpParams httpRequestParameters = httpget.getParams(); httpRequestParameters.setParameter(ClientPNames.HANDLE_REDIRECTS, this.followRedirects); httpget.setParams(httpRequestParameters); LOG.info("Sending GET request for: " + httpget.getURI()); HttpResponse response = client.execute(httpget, localContext); this.httpStatusOfLastDownloadAttempt = response.getStatusLine().getStatusCode(); LOG.info("HTTP GET request status: " + this.httpStatusOfLastDownloadAttempt); LOG.info("Downloading file: " + downloadedFile.getName()); byte[] file = IOUtils.toByteArray(response.getEntity().getContent()); response.getEntity().getContent().close(); return file; }
public byte[] postDownloaderWithRedirects(String formToDownloadLocation, List<NameValuePair> params) throws IOException, NullPointerException, URISyntaxException { HttpClient client = new DefaultHttpClient(); BasicHttpContext localContext = new BasicHttpContext(); LOG.info("Mimic WebDriver cookie state: " + this.mimicWebDriverCookieState); if (this.mimicWebDriverCookieState) { localContext.setAttribute(ClientContext.COOKIE_STORE, mimicCookieState(this.driver.manage().getCookies())); } HttpResponse response = realizaPeticion(null, client, localContext, formToDownloadLocation, params, null); boolean segueixRedirs = true; while (response.getStatusLine().getStatusCode()==302 && segueixRedirs) { try { String URLaux = formToDownloadLocation.substring(0, formToDownloadLocation.lastIndexOf("/")+1); String nuevaURL = URLaux + response.getHeaders("RedirectTo")[0].getValue(); response = realizaPeticion(response, client, localContext, nuevaURL, params, response.getParams()); }catch (Exception ex) { segueixRedirs = false; } } this.httpStatusOfLastDownloadAttempt = response.getStatusLine().getStatusCode(); LOG.info("HTTP GET request status: " + this.httpStatusOfLastDownloadAttempt); byte[] file = IOUtils.toByteArray(response.getEntity().getContent()); response.getEntity().getContent().close(); return file; }
private void buildBasicHttpContext() { AuthCache authCache = new BasicAuthCache(); DigestScheme digestScheme = new DigestScheme(); digestScheme.overrideParamter("nc", String.valueOf(serverNounceCount++)); digestScheme.overrideParamter("cnonce", UUID.randomUUID().toString().replaceAll("-", "")); digestScheme.overrideParamter("qop", "auth"); authCache.put(target, digestScheme); localContext = new BasicHttpContext(); localContext.setAttribute(ClientContext.AUTH_CACHE, authCache); }