Java httpClient 4.3.6具有完整URI和方案的基本身份验证

Java httpClient 4.3.6具有完整URI和方案的基本身份验证,java,authentication,basic-authentication,apache-httpclient-4.x,Java,Authentication,Basic Authentication,Apache Httpclient 4.x,我想要的是: 通过抢占式BASIC身份验证发送GET请求 请求如下所示: <startURL>/app/process?job=doSomething&param=value1,value2 这就是我遇到的问题。此方法也是唯一允许您将方案指定为https的方法。 一个问题是,我不知道端口号。我想我可能只需要为默认端口指定-1就可以了,这样它就可以工作了,但即使撇开这一点,我也没有主机名,只有上面提到的startURL。我真的不想每次都解析这个额外的属性,同时我也不想只为主机

我想要的是: 通过抢占式BASIC身份验证发送GET请求

请求如下所示:

<startURL>/app/process?job=doSomething&param=value1,value2
这就是我遇到的问题。此方法也是唯一允许您将方案指定为https的方法。 一个问题是,我不知道端口号。我想我可能只需要为默认端口指定-1就可以了,这样它就可以工作了,但即使撇开这一点,我也没有主机名,只有上面提到的startURL。我真的不想每次都解析这个额外的属性,同时我也不想只为主机名添加另一个属性

我翻了翻,找到了这个片段,看起来正是我想要的:

HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet("http://foo.com/bar");
httpGet.addHeader(BasicScheme.authenticate(
 new UsernamePasswordCredentials("user", "password"),
 "UTF-8", false));

HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity responseEntity = httpResponse.getEntity();

它给出了完整的请求URL,并简单地添加了基本头,不需要指定任何端口。仅此而已,自版本4.2以来,该版本现在已被弃用:

Deprecated. (4.2) Use ContextAwareAuthScheme.authenticate( Credentials, HttpRequest, org.apache.http.protocol.HttpContext)
我找不到该方法返回基本auth头的示例。它还需要一个上下文作为参数,而上面的snipped没有。我真的不知道这个应该怎么用

所以,我想具体知道:

我只想设置一个包含完整链接的请求,该链接包含所有内容,如:

https://testABC.com/app/process?job=doSomething&param=value1,value2
只需将其作为执行抢占式基本身份验证的请求的参数


有没有办法做到这一点而不去挖掘不推荐的方法以及它是什么样子的?

最后,我自己手动编写了标题并发送了一些东西:

    String header = "Basic ";       
    String headerValue = "username" + ":" + "password";
    String encodedHeaderValue = Base64.encodeBase64String(headerValue.getBytes());      
    String headerBasic =  header + encodedHeaderValue;

    Header authHeader = new BasicHeader("Authorization", headerBasic);
      ArrayList<Header> headers = new ArrayList<Header>();
      headers.add(authHeader);  

    ArrayList<Header> headers = getHttpHeaders();
    HttpClient client = HttpClients.custom().setDefaultHeaders(headers).build();

    HttpUriRequest request = RequestBuilder.get().setUri(uri).build();
    HttpResponse response = client.execute(request);

    int responseCode = response.getStatusLine().getStatusCode();

最后,我自己手动编写了标题,并发送了以下内容:

    String header = "Basic ";       
    String headerValue = "username" + ":" + "password";
    String encodedHeaderValue = Base64.encodeBase64String(headerValue.getBytes());      
    String headerBasic =  header + encodedHeaderValue;

    Header authHeader = new BasicHeader("Authorization", headerBasic);
      ArrayList<Header> headers = new ArrayList<Header>();
      headers.add(authHeader);  

    ArrayList<Header> headers = getHttpHeaders();
    HttpClient client = HttpClients.custom().setDefaultHeaders(headers).build();

    HttpUriRequest request = RequestBuilder.get().setUri(uri).build();
    HttpResponse response = client.execute(request);

    int responseCode = response.getStatusLine().getStatusCode();

我遇到了和你一样的问题

对我起作用的是:

UsernamePasswordCredentials creds = new UsernamePasswordCredentials("user", "12345");

HttpGet get = new HttpGet("https://foo.bar.com/rest");
HttpHost targetHost = new HttpHost("foo.bar.com", 443, "https");
  CredentialsProvider credsProvider = new BasicCredentialsProvider();
  credsProvider.setCredentials(
          new AuthScope(targetHost.getHostName(), targetHost.getPort()),
          creds);
  credsProvider.setCredentials(AuthScope.ANY,creds);

 // 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
  HttpClientContext context = HttpClientContext.create();
context.setCredentialsProvider(credsProvider);
context.setAuthCache(authCache);
HttpResponse response = client.execute(targetHost, get, context);

我在上找到了这个解决方案:

我遇到了与您相同的问题

对我起作用的是:

UsernamePasswordCredentials creds = new UsernamePasswordCredentials("user", "12345");

HttpGet get = new HttpGet("https://foo.bar.com/rest");
HttpHost targetHost = new HttpHost("foo.bar.com", 443, "https");
  CredentialsProvider credsProvider = new BasicCredentialsProvider();
  credsProvider.setCredentials(
          new AuthScope(targetHost.getHostName(), targetHost.getPort()),
          creds);
  credsProvider.setCredentials(AuthScope.ANY,creds);

 // 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
  HttpClientContext context = HttpClientContext.create();
context.setCredentialsProvider(credsProvider);
context.setAuthCache(authCache);
HttpResponse response = client.execute(targetHost, get, context);

我在上找到了这个解决方案:

不会有帮助,因为HttpHost构造函数需要指定的端口和主机名。但是如上所述,我只有startUrl,并且明确地希望避免解析它并将其分解为这三个参数值。在我的例子中,虽然我发现相同的授权头内容设置为Base64,但以您的方式创建头是行不通的。由于某些原因,使用httpGet.addHeaderBasicScheme.authenticate new UsernamePasswordCredentialsuser、password、UTF-8设置的Base64授权有效,而不是手动设置的导致401。无论如何,设置这个应该更简单,就像stackoverflow中的4.1.xTried serval版本一样,这篇文章是唯一适合我的文章。这不会有帮助,因为HttpHost构造函数需要指定的端口和主机名。但是如上所述,我只有startUrl,并且明确地希望避免解析它并将其分解为这三个参数值。在我的例子中,虽然我发现相同的授权头内容设置为Base64,但以您的方式创建头是行不通的。由于某些原因,使用httpGet.addHeaderBasicScheme.authenticate new UsernamePasswordCredentialsuser、password、UTF-8设置的Base64授权有效,而不是手动设置的导致401。无论如何,设置这个应该更简单,就像stackoverflow中的4.1.xTried serval版本一样,这篇文章是唯一适合我的。