服务器需要使用第三方系统时需要登录验证

创建第三方登录认证和凭证信息

HttpHost targetHost = new HttpHost(host,new Integer(port).intValue(),"http");
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(new AuthScope(targetHost.getHostName(),targetHost.getPort()),new UsernamePasswordCredentials(user,password));

BasicSchema basicSchema = new BasicSchema();
AuthCache authCache = new BasicAuthCache();
authCache.put(targetHost,basicSchema);

HttpClientContext context = HttpClientContext.create();
context.setCredentialsProvider(credentialsProvider);
context.setAuthCache(authCache);
CloseableHttpClient httpClient = HttpClientBuilder.create().build();

发送请求时携带认证和凭证信息

HttpResponse response = httpClient.execute(httpGet, context);

 

posted @ 2024-01-30 14:57  一尺灯光  阅读(23)  评论(0)    收藏  举报