当前位置: 首页>>代码示例>>Java>>正文


Java HttpClientContext.setCredentialsProvider方法代码示例

本文整理汇总了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);
    }
}
 
开发者ID:aws,项目名称:aws-sdk-java-v2,代码行数:17,代码来源:ApacheUtils.java

示例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);
    }
}
 
开发者ID:IBM,项目名称:ibm-cos-sdk-java,代码行数:18,代码来源:ApacheUtils.java

示例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());
}
 
开发者ID:virjar,项目名称:vscrawler,代码行数:28,代码来源:VSCrawlerRoutePlanner.java

示例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());
}
 
开发者ID:MyPureCloud,项目名称:purecloud-iot,代码行数:21,代码来源:TestClientAuthenticationFakeNTLM.java

示例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());
        }
    }
 
开发者ID:brutusin,项目名称:Brutusin-RPC,代码行数:25,代码来源:HttpEndpoint.java

示例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());
}
 
开发者ID:MyPureCloud,项目名称:purecloud-iot,代码行数:24,代码来源:TestAuthenticationStrategy.java

示例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;
}
 
开发者ID:reflectoring,项目名称:infiniboard,代码行数:22,代码来源:UrlSourceJob.java

示例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;
}
 
开发者ID:egetman,项目名称:ibm-bpm-rest-client,代码行数:18,代码来源:KerberosBpmClient.java

示例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());
}
 
开发者ID:MyPureCloud,项目名称:purecloud-iot,代码行数:21,代码来源:TestClientAuthenticationFakeNTLM.java

示例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());
}
 
开发者ID:MyPureCloud,项目名称:purecloud-iot,代码行数:21,代码来源:TestClientAuthentication.java

示例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());
}
 
开发者ID:MyPureCloud,项目名称:purecloud-iot,代码行数:22,代码来源:TestClientAuthentication.java

示例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());
}
 
开发者ID:MyPureCloud,项目名称:purecloud-iot,代码行数:22,代码来源:TestClientAuthentication.java

示例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());
    }
}
 
开发者ID:MyPureCloud,项目名称:purecloud-iot,代码行数:21,代码来源:TestClientAuthentication.java

示例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());
}
 
开发者ID:MyPureCloud,项目名称:purecloud-iot,代码行数:24,代码来源:TestClientAuthentication.java

示例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;
    }
}
 
开发者ID:MyPureCloud,项目名称:purecloud-iot,代码行数:27,代码来源:TestClientAuthentication.java


注:本文中的org.apache.http.client.protocol.HttpClientContext.setCredentialsProvider方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。