簡體   English   中英

在HttpClient 4.3.x中更正基本身份驗證

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM