java - http 客户端 4.3 不发送凭据

标签 java https apache-httpclient-4.x

我正在尝试使用 apache http 客户端 4.3 发送获取请求(发送到使用自签名证书的客户端),但是我每次都收到错误“需要身份验证”。在网络浏览器中,它工作得很好,所以用户名/密码/url 是正确的。这不是使用http client 4.3传递用户名/密码的方式吗?

public static String sendJsonHttpGetRequest(
                String host,
                String path,
                String username,
                String password,
                int socketTimeout,
                int connectionTimeout,
                int connectionRequestTimeout
                ) throws Exception
    {
            String responseBody = null;
            CredentialsProvider credsProvider = new BasicCredentialsProvider();
            credsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));
            SSLContextBuilder builder = new SSLContextBuilder();
            builder.loadTrustMaterial(null, new TrustStrategy(){
                  @Override
                  public boolean isTrusted(java.security.cert.X509Certificate[] chain, String authType) 
                  throws java.security.cert.CertificateException
                  {
                      return true;
                  }
                });
            SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(builder.build());
            CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).setDefaultCredentialsProvider(credsProvider).build();
            URIBuilder uriB = new URIBuilder().setScheme("https").setHost(host).setPath(path);
            HttpGet _http = new HttpGet( uriB.build() );
            RequestConfig _requestConfig = RequestConfig.custom().
                      setSocketTimeout(socketTimeout).
                      setConnectTimeout(connectionTimeout).
                      setConnectionRequestTimeout(connectionRequestTimeout).build();
            _http.addHeader("Content-Type", "application/json");
            _http.addHeader("Accept","application/json, text/xml;q=9, /;q=8");
            _http.setConfig(_requestConfig);
            // ###########################
            ResponseHandler<String> response = new BasicResponseHandler();
            responseBody = httpclient.execute(_http, response);
            return responseBody;
    }

最佳答案

事实证明现在使用 http 4+ 你必须在两个位置提供它才能工作,

第二个是

authCache.put(host, basicAuth);
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));
HttpClientContext _context = HttpClientContext.create();
_context.setAuthCache(authCache);
_context.setCredentialsProvider(credentialsProvider);
responseBody = httpclient.execute(_http, response, _context);

关于java - http 客户端 4.3 不发送凭据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21647337/

相关文章:

apache - 将 SSL 证书从 CPanel 迁移到 DigitalOcean Apache 服务器

java - 如何使用 Bouncy CaSTLe 创建 CA 根证书?

https - jmeter中HTTPclient 3.1和HTTP client 4之间响应时间的差异

java - 是否可以针对不同的日志级别使用不同的模式布局并输出到 SYSTEM_OUT?

java - System.out.println() 更改单线程程序中变量的值

.htaccess - 将非www和非http重定向到https

java - WinRM(WsMan) 和 NTLM 身份验证

java - gmongo 的重复项也不异常(exception)

java.lang.ClassNotFoundException : com. example.Main 在 intelliJ 和 Maven 构建中

java - PoolingHttpClientConnectionManager 不释放连接