我想要什么: 使用抢先式基本身份验证发送 GET 请求。
请求看起来像这样:
<startURL>/app/process?job=doSomething¶m=value1,value2
而 startURL 始终是 https 链接,具体取决于环境。
看起来像这样: https://testABC.com https://prodABC.com
startURL 也被放置在属性文件中,就像不同的环境一样。
我调查的内容: http://www.baeldung.com/httpclient-4-basic-authentication
http://www.java-tips.org/other-api-tips/httpclient/how-to-use-basic-authentication.html
http://hc.apache.org/httpcomponents-client-ga/tutorial/html/authentication.html
全部包含一个
HttpHost targetHost = new HttpHost("hostname", portnumber, "scheme");
这就是我遇到的麻烦。此方法也是唯一允许您将方案指定为“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)
我找不到此方法返回基本身份验证标头的单个示例。它还需要一个上下文作为参数,而上面的片段没有。我真的不知道应该如何使用它。
所以,我具体想知道的是:
我只想设置一个包含完整链接的请求,其中包含所有内容,例如:
https://testABC.com/app/process?job=doSomething¶m=value1,value2
并将其作为进行抢先基本身份验证的请求的参数。
有没有什么办法可以做到这一点,而不需要挖掘已弃用的方法,它是什么样子的?
我遇到了和你一样的问题。
对我有用的是以下内容:
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);
我在以下位置找到了这个解决方案:HttpClientBuilder basic auth
最后我自己手动编写标题并用它发送内容:
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();
要删除已弃用的警告,请替换
httpGet.addHeader(BasicScheme.authenticate(
new UsernamePasswordCredentials("user", "password"),
"UTF-8", false));
与
httpGet.addHeader(new BasicScheme().authenticate(
new UsernamePasswordCredentials("user", "password"),
httpGet, new BasicHttpContext()));