httpclient进行basic auth认证

时间:2023-03-08 17:13:33
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方法