List of usage examples for org.apache.http.client.protocol ClientContext AUTH_CACHE
String AUTH_CACHE
To view the source code for org.apache.http.client.protocol ClientContext AUTH_CACHE.
Click Source Link
From source file:org.slieer.http.auth.ClientPreemptiveBasicAuthentication.java
public static void main(String[] args) throws Exception { HttpHost targetHost = new HttpHost("localhost", 8080, "http"); DefaultHttpClient httpclient = new DefaultHttpClient(); try {//from ww w .j av a2s . co m httpclient.getCredentialsProvider().setCredentials( new AuthScope(targetHost.getHostName(), targetHost.getPort()), new UsernamePasswordCredentials("newuser", "tomcat")); // Create AuthCache instance AuthCache authCache = new BasicAuthCache(); // Generate BASIC scheme object and add it to the local // auth cache BasicScheme basicAuth = new BasicScheme(); authCache.put(targetHost, basicAuth); // Add AuthCache to the execution context BasicHttpContext localcontext = new BasicHttpContext(); localcontext.setAttribute(ClientContext.AUTH_CACHE, authCache); HttpGet httpget = new HttpGet("/simpleweb/protected"); System.out.println("executing request: " + httpget.getRequestLine()); System.out.println("to target: " + targetHost); for (int i = 0; i < 3; i++) { HttpResponse response = httpclient.execute(targetHost, httpget, localcontext); HttpEntity entity = response.getEntity(); System.out.println("----------------------------------------"); System.out.println(response.getStatusLine()); if (entity != null) { System.out.println("Response content length: " + entity.getContentLength()); } EntityUtils.consume(entity); } } finally { // When HttpClient instance is no longer needed, // shut down the connection manager to ensure // immediate deallocation of all system resources httpclient.getConnectionManager().shutdown(); } }
From source file:com.dlmu.heipacker.crawler.client.ClientPreemptiveBasicAuthentication.java
public static void main(String[] args) throws Exception { HttpHost targetHost = new HttpHost("localhost", 80, "http"); DefaultHttpClient httpclient = new DefaultHttpClient(); try {//from w w w . j av a2 s . c om httpclient.getCredentialsProvider().setCredentials( new AuthScope(targetHost.getHostName(), targetHost.getPort()), new UsernamePasswordCredentials("username", "password")); // Create AuthCache instance AuthCache authCache = new BasicAuthCache(); // Generate BASIC scheme object and add it to the local // auth cache BasicScheme basicAuth = new BasicScheme(); authCache.put(targetHost, basicAuth); // Add AuthCache to the execution context BasicHttpContext localcontext = new BasicHttpContext(); localcontext.setAttribute(ClientContext.AUTH_CACHE, authCache); HttpGet httpget = new HttpGet("/"); System.out.println("executing request: " + httpget.getRequestLine()); System.out.println("to target: " + targetHost); for (int i = 0; i < 3; i++) { HttpResponse response = httpclient.execute(targetHost, httpget, localcontext); HttpEntity entity = response.getEntity(); System.out.println("----------------------------------------"); System.out.println(response.getStatusLine()); if (entity != null) { System.out.println("Response content length: " + entity.getContentLength()); } EntityUtils.consume(entity); } } finally { // When HttpClient instance is no longer needed, // shut down the connection manager to ensure // immediate deallocation of all system resources httpclient.getConnectionManager().shutdown(); } }
From source file:com.hsbc.frc.SevenHero.ClientPreemptiveBasicAuthentication.java
public static void main(String[] args) throws Exception { HttpHost proxy = new HttpHost("133.13.162.149", 8080, "http"); HttpHost targetHost = new HttpHost("pt.3g.qq.com"); DefaultHttpClient httpclient = new DefaultHttpClient(); try {/* w w w . j a v a 2s . co m*/ httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy); httpclient.getCredentialsProvider().setCredentials( new AuthScope(targetHost.getHostName(), targetHost.getPort()), new UsernamePasswordCredentials("username", "password")); // Create AuthCache instance AuthCache authCache = new BasicAuthCache(); // Generate BASIC scheme object and add it to the local // auth cache BasicScheme basicAuth = new BasicScheme(); authCache.put(targetHost, basicAuth); // Add AuthCache to the execution context BasicHttpContext localcontext = new BasicHttpContext(); localcontext.setAttribute(ClientContext.AUTH_CACHE, authCache); HttpGet httpget = new HttpGet("/"); System.out.println("executing request: " + httpget.getRequestLine()); System.out.println("to target: " + targetHost); for (int i = 0; i < 3; i++) { HttpResponse response = httpclient.execute(targetHost, httpget, localcontext); HttpEntity entity = response.getEntity(); System.out.println("----------------------------------------"); System.out.println(response.getStatusLine()); if (entity != null) { System.out.println("Response content length: " + entity.getContentLength()); } EntityUtils.consume(entity); } } finally { // When HttpClient instance is no longer needed, // shut down the connection manager to ensure // immediate deallocation of all system resources httpclient.getConnectionManager().shutdown(); } }
From source file:mai.cn.film.auth.ClientAuthentication.java
public static void main(String[] args) throws Exception { HttpHost targetHost = new HttpHost("www.filmaffinity.com", 80, "http"); DefaultHttpClient httpclient = new DefaultHttpClient(); try {/*from ww w . j a v a 2s.c om*/ httpclient.getCredentialsProvider().setCredentials( new AuthScope(targetHost.getHostName(), targetHost.getPort()), new UsernamePasswordCredentials("picarus", "8C8PPkEc")); // Create AuthCache instance AuthCache authCache = new BasicAuthCache(); // Generate BASIC scheme object and add it to the local auth cache BasicScheme basicAuth = new BasicScheme(); authCache.put(targetHost, basicAuth); // Add AuthCache to the execution context BasicHttpContext localcontext = new BasicHttpContext(); localcontext.setAttribute(ClientContext.AUTH_CACHE, authCache); HttpGet httpget = new HttpGet("http://www.filmaffinity.com/en/myvotes.php"); System.out.println("executing request" + httpget.getRequestLine()); // Create a response handler ResponseHandler<String> responseHandler = new BasicResponseHandler(); HttpResponse response = httpclient.execute(httpget); String responseBody = responseHandler.handleResponse(response); int code = response.getStatusLine().getStatusCode(); HttpEntity entity = response.getEntity(); System.out.println("----------------------------------------"); System.out.println(responseBody); System.out.println("----------------------------------------"); System.out.println("----------------------------------------"); System.out.println("Status:" + code + " " + response.getStatusLine().getReasonPhrase()); if (entity != null) { System.out.println("Response content length: " + entity.getContentLength()); } EntityUtils.consume(entity); } finally { // When HttpClient instance is no longer needed, // shut down the connection manager to ensure // immediate deallocation of all system resources httpclient.getConnectionManager().shutdown(); } }
From source file:com.dlmu.heipacker.crawler.client.ClientPreemptiveDigestAuthentication.java
public static void main(String[] args) throws Exception { HttpHost targetHost = new HttpHost("localhost", 80, "http"); DefaultHttpClient httpclient = new DefaultHttpClient(); try {/* w w w . ja va 2 s .c om*/ httpclient.getCredentialsProvider().setCredentials( new AuthScope(targetHost.getHostName(), targetHost.getPort()), new UsernamePasswordCredentials("username", "password")); // Create AuthCache instance AuthCache authCache = new BasicAuthCache(); // Generate DIGEST scheme object, initialize it and add it to the local // auth cache DigestScheme digestAuth = new DigestScheme(); // Suppose we already know the realm name digestAuth.overrideParamter("realm", "some realm"); // Suppose we already know the expected nonce value digestAuth.overrideParamter("nonce", "whatever"); authCache.put(targetHost, digestAuth); // Add AuthCache to the execution context BasicHttpContext localcontext = new BasicHttpContext(); localcontext.setAttribute(ClientContext.AUTH_CACHE, authCache); HttpGet httpget = new HttpGet("/"); System.out.println("executing request: " + httpget.getRequestLine()); System.out.println("to target: " + targetHost); for (int i = 0; i < 3; i++) { HttpResponse response = httpclient.execute(targetHost, httpget, localcontext); HttpEntity entity = response.getEntity(); System.out.println("----------------------------------------"); System.out.println(response.getStatusLine()); if (entity != null) { System.out.println("Response content length: " + entity.getContentLength()); } EntityUtils.consume(entity); } } finally { // When HttpClient instance is no longer needed, // shut down the connection manager to ensure // immediate deallocation of all system resources httpclient.getConnectionManager().shutdown(); } }
From source file:org.eclipse.aether.transport.http.SharingHttpContext.java
SharingHttpContext(LocalState state) { this.state = state; authCache = new SharingAuthCache(state); super.setAttribute(ClientContext.AUTH_CACHE, authCache); }
From source file:projekat.rest_client.HttpComponentsClientHttpRequestFactoryBasicAuth.java
private HttpContext createHttpContext() { // Create AuthCache instance AuthCache authCache = new BasicAuthCache(); // Generate BASIC scheme object and add it to the local auth cache BasicScheme basicAuth = new BasicScheme(); authCache.put(host, basicAuth);/*from w w w .jav a 2s . c om*/ // Add AuthCache to the execution context BasicHttpContext localcontext = new BasicHttpContext(); localcontext.setAttribute(ClientContext.AUTH_CACHE, authCache); return localcontext; }
From source file:com.impetus.client.couchdb.CouchDBUtils.java
/** * Gets the context.//ww w. ja v a 2s .com * * @param httpHost * the http host * @return the context */ public static HttpContext getContext(HttpHost httpHost) { AuthCache authCache = new BasicAuthCache(); authCache.put(httpHost, new BasicScheme()); HttpContext context = new BasicHttpContext(); context.setAttribute(ClientContext.AUTH_CACHE, authCache); return context; }
From source file:org.piraso.client.net.HttpBasicAuthentication.java
public void authenticate() { validate();//from w ww. j av a2 s.co m UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(userName, password); httpClient.getCredentialsProvider() .setCredentials(new AuthScope(targetHost.getHostName(), targetHost.getPort()), credentials); // Create AuthCache instance AuthCache authCache = new BasicAuthCache(); // Generate BASIC scheme object and add it to the local auth cache BasicScheme basicAuth = new BasicScheme(); authCache.put(targetHost, basicAuth); context.setAttribute(ClientContext.AUTH_CACHE, authCache); }
From source file:com.github.restdriver.clientdriver.integration.BasicAuthTest.java
@Test public void basicAuthWorks() throws Exception { clientDriver.addExpectation(onRequestTo("/").withBasicAuth("Aladdin", "open sesame"), giveEmptyResponse().withStatus(418)).anyTimes(); DefaultHttpClient client = new DefaultHttpClient(); client.getCredentialsProvider().setCredentials(new AuthScope("localhost", AuthScope.ANY_PORT), new UsernamePasswordCredentials("Aladdin", "open sesame")); HttpHost host = new HttpHost("localhost", 12345); AuthCache authCache = new BasicAuthCache(); BasicScheme basicAuth = new BasicScheme(); authCache.put(host, basicAuth);// w ww. j av a 2s . c o m BasicHttpContext context = new BasicHttpContext(); context.setAttribute(ClientContext.AUTH_CACHE, authCache); List<String> authPrefs = new ArrayList<String>(); authPrefs.add(AuthPolicy.BASIC); client.getParams().setParameter(AuthPNames.PROXY_AUTH_PREF, authPrefs); HttpGet get = new HttpGet(clientDriver.getBaseUrl() + "/"); HttpResponse response = client.execute(host, get, context); assertThat(response.getStatusLine().getStatusCode(), is(418)); }