[英]Correct Basic Authentication in HttpClient 4.3.x
我目前使用以下基本身份验证实现:
HttpClient httpClient = HttpClientBuilder.create().build();
HttpGet httpGet = new HttpGet(uri);
// Set the basic authentication
httpGet.addHeader(BasicScheme.authenticate(
new UsernamePasswordCredentials("user", "pass"),
"UTF-8", false));
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity entity = httpResponse.getEntity();
...
该代码可以正常工作,但是不建议使用BasicScheme.authenticate
。
在HttpClient 4.3.x中为请求实现基本身份验证的正确方法是什么?
以下对我有用的作品-
HttpClient httpClient = HttpClientBuilder.create().build();
HttpGet httpGet = new HttpGet(uri);
// Set the basic authentication
String encoding = Base64Encoder.encode("user:pass");
httpGet.setHeader("Authorization", "Basic" + encoding);
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity entity = httpResponse.getEntity();
如果适合您,请告诉我。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.