本文整理匯總了Java中org.apache.http.client.protocol.HttpClientContext.setCredentialsProvider方法的典型用法代碼示例。如果您正苦於以下問題:Java HttpClientContext.setCredentialsProvider方法的具體用法?Java HttpClientContext.setCredentialsProvider怎麽用?Java HttpClientContext.setCredentialsProvider使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.apache.http.client.protocol.HttpClientContext
的用法示例。
在下文中一共展示了HttpClientContext.setCredentialsProvider方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: addPreemptiveAuthenticationProxy
import org.apache.http.client.protocol.HttpClientContext; //導入方法依賴的package包/類
private static void addPreemptiveAuthenticationProxy(HttpClientContext clientContext,
ProxyConfiguration proxyConfiguration) {
if (proxyConfiguration.preemptiveBasicAuthenticationEnabled()) {
HttpHost targetHost = new HttpHost(proxyConfiguration.endpoint().getHost(), proxyConfiguration.endpoint().getPort());
final CredentialsProvider credsProvider = newProxyCredentialsProvider(proxyConfiguration);
// 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);
clientContext.setCredentialsProvider(credsProvider);
clientContext.setAuthCache(authCache);
}
}
示例2: addPreemptiveAuthenticationProxy
import org.apache.http.client.protocol.HttpClientContext; //導入方法依賴的package包/類
private static void addPreemptiveAuthenticationProxy(HttpClientContext clientContext,
HttpClientSettings settings) {
if (settings.isPreemptiveBasicProxyAuth()) {
HttpHost targetHost = new HttpHost(settings.getProxyHost(), settings
.getProxyPort());
final CredentialsProvider credsProvider = newProxyCredentialsProvider(settings);
// 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);
clientContext.setCredentialsProvider(credsProvider);
clientContext.setAuthCache(authCache);
}
}
示例3: determineProxy
import org.apache.http.client.protocol.HttpClientContext; //導入方法依賴的package包/類
@Override
protected HttpHost determineProxy(HttpHost host, HttpRequest request, HttpContext context) throws HttpException {
HttpClientContext httpClientContext = HttpClientContext.adapt(context);
Proxy proxy = proxyPlanner.determineProxy(host, request, context, ipPool, crawlerSession);
if (proxy == null) {
return null;
}
if (log.isDebugEnabled()) {
log.debug("{} 當前使用IP為:{}:{}", host.getHostName(), proxy.getIp(), proxy.getPort());
}
context.setAttribute(VSCRAWLER_AVPROXY_KEY, proxy);
if (proxy.getAuthenticationHeaders() != null) {
for (Header header : proxy.getAuthenticationHeaders()) {
request.addHeader(header);
}
}
if (StringUtils.isNotEmpty(proxy.getUsername()) && StringUtils.isNotEmpty(proxy.getPassword())) {
BasicCredentialsProvider credsProvider1 = new BasicCredentialsProvider();
httpClientContext.setCredentialsProvider(credsProvider1);
credsProvider1.setCredentials(AuthScope.ANY,
new UsernamePasswordCredentials(proxy.getUsername(), proxy.getPassword()));
}
return new HttpHost(proxy.getIp(), proxy.getPort());
}
示例4: testNTLMType2NonUnicodeMessageOnlyAuthenticationFailure
import org.apache.http.client.protocol.HttpClientContext; //導入方法依賴的package包/類
@Test
public void testNTLMType2NonUnicodeMessageOnlyAuthenticationFailure() throws Exception {
this.serverBootstrap.registerHandler("*", new NtlmType2MessageOnlyResponseHandler("TlRMTVNTUAACAA" +
"AABgAGADgAAAAyggLiASNFZ4mrze8AAAAAAAAAAAAAAAAAAAAABgBwFwAAAA9T" +
"ZXJ2ZXI="));
final HttpHost target = start();
final HttpClientContext context = HttpClientContext.create();
final BasicCredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(AuthScope.ANY,
new NTCredentials("test", "test", null, null));
context.setCredentialsProvider(credsProvider);
final HttpGet httpget = new HttpGet("/");
final HttpResponse response = this.httpclient.execute(target, httpget, context);
EntityUtils.consume(response.getEntity());
Assert.assertEquals(HttpStatus.SC_UNAUTHORIZED,
response.getStatusLine().getStatusCode());
}
示例5: main
import org.apache.http.client.protocol.HttpClientContext; //導入方法依賴的package包/類
public static void main(String[] args) throws Exception {
HttpClientContextFactory ctxFact = new HttpClientContextFactory() {
public HttpClientContext create() {
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(
new AuthScope("localhost", 8080, AuthScope.ANY_REALM, "basic"),
new UsernamePasswordCredentials("user", "password"));
HttpClientContext context = HttpClientContext.create();
context.setCredentialsProvider(credsProvider);
return context;
}
};
HttpEndpoint endpoint = new HttpEndpoint(new URI("http://localhost:8080/rpc/http"), ctxFact);
HttpResponse resp = endpoint.exec("rpc.http.version", null, null);
if (resp.isIsBinary()) {
System.out.println("binary");
System.out.println(resp.getInputStream().getName());
} else {
System.out.println(resp.getRpcResponse().getResult());
}
}
示例6: testNoCredentials
import org.apache.http.client.protocol.HttpClientContext; //導入方法依賴的package包/類
@Test
public void testNoCredentials() throws Exception {
final TargetAuthenticationStrategy authStrategy = new TargetAuthenticationStrategy();
final HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_UNAUTHORIZED, "UNAUTHORIZED");
final HttpHost authhost = new HttpHost("locahost", 80);
final HttpClientContext context = HttpClientContext.create();
final Map<String, Header> challenges = new HashMap<String, Header>();
challenges.put("basic", new BasicHeader(AUTH.WWW_AUTH, "Basic realm=\"realm1\""));
challenges.put("digest", new BasicHeader(AUTH.WWW_AUTH, "Digest realm=\"realm2\", nonce=\"1234\""));
final Registry<AuthSchemeProvider> authSchemeRegistry = RegistryBuilder.<AuthSchemeProvider>create()
.register("basic", new BasicSchemeFactory())
.register("digest", new DigestSchemeFactory()).build();
context.setAuthSchemeRegistry(authSchemeRegistry);
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
context.setCredentialsProvider(credentialsProvider);
final Queue<AuthOption> options = authStrategy.select(challenges, authhost, response, context);
Assert.assertNotNull(options);
Assert.assertEquals(0, options.size());
}
示例7: configureContext
import org.apache.http.client.protocol.HttpClientContext; //導入方法依賴的package包/類
private HttpClientContext configureContext(CredentialsProvider credentialsProvider, String url) {
if (credentialsProvider != null) {
try {
URL targetUrl = new URL(url);
HttpHost targetHost =
new HttpHost(targetUrl.getHost(), targetUrl.getPort(), targetUrl.getProtocol());
AuthCache authCache = new BasicAuthCache();
authCache.put(targetHost, new BasicScheme());
final HttpClientContext context = HttpClientContext.create();
context.setCredentialsProvider(credentialsProvider);
context.setAuthCache(authCache);
return context;
} catch (MalformedURLException e) {
LOG.error("Cannot parse URL '{}'", url, e);
}
}
return null;
}
示例8: getHttpContext
import org.apache.http.client.protocol.HttpClientContext; //導入方法依賴的package包/類
private HttpClientContext getHttpContext() {
HttpClientContext context = HttpClientContext.create();
BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
Credentials credentials = new Credentials() {
public String getPassword() {
return null;
}
public Principal getUserPrincipal() {
return null;
}
};
credentialsProvider.setCredentials(new AuthScope(null, -1, null), credentials);
context.setCredentialsProvider(credentialsProvider);
return context;
}
示例9: testNTLMType2MessageOnlyAuthenticationFailure
import org.apache.http.client.protocol.HttpClientContext; //導入方法依賴的package包/類
@Test
public void testNTLMType2MessageOnlyAuthenticationFailure() throws Exception {
this.serverBootstrap.registerHandler("*", new NtlmType2MessageOnlyResponseHandler("TlRMTVNTUAACAA" +
"AADAAMADgAAAAzggLiASNFZ4mrze8AAAAAAAAAAAAAAAAAAAAABgBwFwAAAA9T" +
"AGUAcgB2AGUAcgA="));
final HttpHost target = start();
final HttpClientContext context = HttpClientContext.create();
final BasicCredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(AuthScope.ANY,
new NTCredentials("test", "test", null, null));
context.setCredentialsProvider(credsProvider);
final HttpGet httpget = new HttpGet("/");
final HttpResponse response = this.httpclient.execute(target, httpget, context);
EntityUtils.consume(response.getEntity());
Assert.assertEquals(HttpStatus.SC_UNAUTHORIZED,
response.getStatusLine().getStatusCode());
}
示例10: testBasicAuthenticationNoCreds
import org.apache.http.client.protocol.HttpClientContext; //導入方法依賴的package包/類
@Test
public void testBasicAuthenticationNoCreds() throws Exception {
this.serverBootstrap.registerHandler("*", new AuthHandler());
final HttpHost target = start();
final HttpClientContext context = HttpClientContext.create();
final TestCredentialsProvider credsProvider = new TestCredentialsProvider(null);
context.setCredentialsProvider(credsProvider);
final HttpGet httpget = new HttpGet("/");
final HttpResponse response = this.httpclient.execute(target, httpget, context);
final HttpEntity entity = response.getEntity();
Assert.assertEquals(HttpStatus.SC_UNAUTHORIZED, response.getStatusLine().getStatusCode());
Assert.assertNotNull(entity);
EntityUtils.consume(entity);
final AuthScope authscope = credsProvider.getAuthScope();
Assert.assertNotNull(authscope);
Assert.assertEquals("test realm", authscope.getRealm());
}
示例11: testBasicAuthenticationFailure
import org.apache.http.client.protocol.HttpClientContext; //導入方法依賴的package包/類
@Test
public void testBasicAuthenticationFailure() throws Exception {
this.serverBootstrap.registerHandler("*", new AuthHandler());
final HttpHost target = start();
final HttpClientContext context = HttpClientContext.create();
final TestCredentialsProvider credsProvider = new TestCredentialsProvider(
new UsernamePasswordCredentials("test", "all-wrong"));
context.setCredentialsProvider(credsProvider);
final HttpGet httpget = new HttpGet("/");
final HttpResponse response = this.httpclient.execute(target, httpget, context);
final HttpEntity entity = response.getEntity();
Assert.assertEquals(HttpStatus.SC_UNAUTHORIZED, response.getStatusLine().getStatusCode());
Assert.assertNotNull(entity);
EntityUtils.consume(entity);
final AuthScope authscope = credsProvider.getAuthScope();
Assert.assertNotNull(authscope);
Assert.assertEquals("test realm", authscope.getRealm());
}
示例12: testBasicAuthenticationSuccess
import org.apache.http.client.protocol.HttpClientContext; //導入方法依賴的package包/類
@Test
public void testBasicAuthenticationSuccess() throws Exception {
this.serverBootstrap.registerHandler("*", new AuthHandler());
final HttpGet httpget = new HttpGet("/");
final HttpClientContext context = HttpClientContext.create();
final TestCredentialsProvider credsProvider = new TestCredentialsProvider(
new UsernamePasswordCredentials("test", "test"));
context.setCredentialsProvider(credsProvider);
final HttpHost target = start();
final HttpResponse response = this.httpclient.execute(target, httpget, context);
final HttpEntity entity = response.getEntity();
Assert.assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode());
Assert.assertNotNull(entity);
EntityUtils.consume(entity);
final AuthScope authscope = credsProvider.getAuthScope();
Assert.assertNotNull(authscope);
Assert.assertEquals("test realm", authscope.getRealm());
}
示例13: testConnectionCloseAfterAuthenticationSuccess
import org.apache.http.client.protocol.HttpClientContext; //導入方法依賴的package包/類
@Test
public void testConnectionCloseAfterAuthenticationSuccess() throws Exception {
this.serverBootstrap.registerHandler("*", new ClosingAuthHandler());
final HttpHost target = start();
final HttpClientContext context = HttpClientContext.create();
final BasicCredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(AuthScope.ANY,
new UsernamePasswordCredentials("test", "test"));
context.setCredentialsProvider(credsProvider);
for (int i = 0; i < 2; i++) {
final HttpGet httpget = new HttpGet("/");
final HttpResponse response = this.httpclient.execute(target, httpget, context);
EntityUtils.consume(response.getEntity());
Assert.assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode());
}
}
示例14: testBasicAuthenticationSuccessOnRepeatablePost
import org.apache.http.client.protocol.HttpClientContext; //導入方法依賴的package包/類
@Test
public void testBasicAuthenticationSuccessOnRepeatablePost() throws Exception {
this.serverBootstrap.registerHandler("*", new AuthHandler());
final HttpHost target = start();
final HttpPost httppost = new HttpPost("/");
httppost.setEntity(new StringEntity("some important stuff", Consts.ASCII));
final HttpClientContext context = HttpClientContext.create();
final TestCredentialsProvider credsProvider = new TestCredentialsProvider(
new UsernamePasswordCredentials("test", "test"));
context.setCredentialsProvider(credsProvider);
final HttpResponse response = this.httpclient.execute(target, httppost, context);
final HttpEntity entity = response.getEntity();
Assert.assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode());
Assert.assertNotNull(entity);
EntityUtils.consume(entity);
final AuthScope authscope = credsProvider.getAuthScope();
Assert.assertNotNull(authscope);
Assert.assertEquals("test realm", authscope.getRealm());
}
示例15: testBasicAuthenticationFailureOnNonRepeatablePost
import org.apache.http.client.protocol.HttpClientContext; //導入方法依賴的package包/類
@Test(expected=ClientProtocolException.class)
public void testBasicAuthenticationFailureOnNonRepeatablePost() throws Exception {
this.serverBootstrap.registerHandler("*", new AuthHandler());
final HttpHost target = start();
final HttpPost httppost = new HttpPost("/");
httppost.setEntity(new InputStreamEntity(
new ByteArrayInputStream(
new byte[] { 0,1,2,3,4,5,6,7,8,9 }), -1));
final HttpClientContext context = HttpClientContext.create();
final TestCredentialsProvider credsProvider = new TestCredentialsProvider(
new UsernamePasswordCredentials("test", "test"));
context.setCredentialsProvider(credsProvider);
try {
this.httpclient.execute(target, httppost, context);
Assert.fail("ClientProtocolException should have been thrown");
} catch (final ClientProtocolException ex) {
final Throwable cause = ex.getCause();
Assert.assertNotNull(cause);
Assert.assertTrue(cause instanceof NonRepeatableRequestException);
throw ex;
}
}