HttpClient处理Basic认证CODE

简介:
package  com.favccxx.favsoft.main;
 
import  java.net.URI;
import  org.apache.http.auth.AuthScope;
import  org.apache.http.auth.UsernamePasswordCredentials;
import  org.apache.http.client.CredentialsProvider;
import  org.apache.http.client.methods.CloseableHttpResponse;
import  org.apache.http.client.methods.HttpUriRequest;
import  org.apache.http.client.methods.RequestBuilder;
import  org.apache.http.impl.client.BasicCredentialsProvider;
import  org.apache.http.impl.client.CloseableHttpClient;
import  org.apache.http.impl.client.HttpClients;
import  org.apache.http.util.EntityUtils;
 
public  class  CreditTest {
 
     public  static  void  main(String[] args)  throws  Exception {
         CredentialsProvider credsProvider =  new  BasicCredentialsProvider();
         credsProvider.setCredentials( new  AuthScope( "10.0.0.100" 8080 ),
                 new  UsernamePasswordCredentials( "basicuser" "password" ));
         CloseableHttpClient httpclient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();
         HttpUriRequest login = RequestBuilder.post()
                 .setUri( new  URI(
                         "http://10.0.0.100:8080/hibaby/getBabiesByGender" ))
                 .addParameter( "appKey" "BEST" )
                 .addParameter( "gender" "boy" ).build();
         CloseableHttpResponse response = httpclient.execute(login);
         System.out.println( "响应状态:"  + response.getStatusLine());
         String result = EntityUtils.toString(response.getEntity(),  "UTF-8" );
         System.out.println( "Result: "  + result);
     }
}




本文转自 genuinecx 51CTO博客,原文链接:http://blog.51cto.com/favccxx/1878186,如需转载请自行联系原作者
目录
相关文章
|
30天前
|
算法 数据库 数据安全/隐私保护
摘要认证,使用HttpClient实现HTTP digest authentication
这篇文章提供了使用HttpClient实现HTTP摘要认证(digest authentication)的详细步骤和示例代码。
144 2
|
3月前
【Azure Developer】CURL 发送Oauth2 Token请求获取到 404 Not Found 问题
【Azure Developer】CURL 发送Oauth2 Token请求获取到 404 Not Found 问题
|
3月前
|
API 数据安全/隐私保护 网络架构
【Azure API 管理】解决调用REST API操作APIM(API Management)需要认证问题(Authentication failed, The 'Authorization' header is missing)
【Azure API 管理】解决调用REST API操作APIM(API Management)需要认证问题(Authentication failed, The 'Authorization' header is missing)
|
5月前
|
Kubernetes 容器 Perl
k8s部署seata 报错 没有提供足够的身份验证信息 [ http-nio-7091-exec-2] [ty.JwtAuthenticationEntryPoint] [ commence] [] : Responding with unauthorized error. Message - Full authentication is required to access this resource
Kubernetes pod 在16:12时出现两次错误,错误信息显示需要完整认证才能访问资源。尽管有此错误,但页面可正常访问。附有yaml配置文件的图片。
422 2
【异常解决】postman请求提示Full authentication is required to access this resource
【异常解决】postman请求提示Full authentication is required to access this resource
512 0
Android-async-http 添加token get方法报错 No valid URI scheme was provided
Android-async-http 添加token get方法报错 No valid URI scheme was provided
Tinyproxy支持权限校验Basic HTTP Authentication
Tinyproxy支持权限校验Basic HTTP Authentication
243 0
Basic Auth认证
Basic Auth认证
175 1