httpclient进行basic auth认证

Posted 乔叶叶

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了httpclient进行basic auth认证相关的知识,希望对你有一定的参考价值。

private HttpClientContext context = HttpClientContext.create();

public void addUserOAuth(String username,String password){

        CredentialsProvider  credsProvider = new BasicCredentialsProvider();
        org.apache.http.auth.Credentials credentials = new org.apache.http.auth.UsernamePasswordCredentials(username,password);
        credsProvider.setCredentials(org.apache.http.auth.AuthScope.ANY,credentials);
        this.context.setCredentialsProvider(credsProvider);
    }

 

然后在调用httpclient.post或者get方法前,调用addUserOAuth方法

以上是关于httpclient进行basic auth认证的主要内容,如果未能解决你的问题,请参考以下文章

Httpclient处理摘要认证

Java HttpClient Basic Credential 认证

HTTP 基本认证(basic auth)和摘要认证(digest auth)区别

HTTP 基本认证(basic auth)和摘要认证(digest auth)区别

nginx 实现基于basic用户认证机制

elasticsearch httpclient认证机制