http客户端4.3不发送凭据

时间:2014-02-08 14:40:02

标签: java https apache-httpclient-4.x

我正在尝试使用apache http client 4.3(使用自签名证书的客户端)发送get请求,但是我每次都会收到错误“Requires Authentication”。在网络浏览器中它工作得很好,所以用户名/密码/网址是正确的。这不是使用http客户端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;
    }

2 个答案:

答案 0 :(得分:3)

现在使用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);

答案 1 :(得分:0)

我自己不使用这个库,但你试过HttpClient类吗?

HttpClient client = new HttpClient();
client.getParams().setAuthenticationPreemptive(true);
client.getState().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));
GetMethod method = new GetMethod(uri);
client.executeMethod(method);

你仍然需要构建uri并设置超时,但它可能是一个选项。