【问题标题】:http client 4.3 not sending credentialshttp客户端4.3不发送凭据
【发布时间】:2014-02-08 14:40:02
【问题描述】:

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

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      我自己不使用这个库,但是你尝试过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 并设置超时,但这可能是一种选择。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-04-26
        • 1970-01-01
        • 2022-07-08
        相关资源
        最近更新 更多