[英]Correct Basic Authentication in HttpClient 4.3.x
I currently use the following basic authentication implementation: 我目前使用以下基本身份验证实现:
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();
...
The code works fine however BasicScheme.authenticate
is deprecated. 该代码可以正常工作,但是不建议使用BasicScheme.authenticate
。
What is the correct way to implement the basic authentication for a request in HttpClient 4.3.x? 在HttpClient 4.3.x中为请求实现基本身份验证的正确方法是什么?
The following works for me - 以下对我有用的作品-
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();
Please let me know if it works fine for you. 如果适合您,请告诉我。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.