Apache HttpClient Digest authentication

前端 未结 4 1774
生来不讨喜
生来不讨喜 2020-11-28 13:28

Basically what I need to do is to perform digest authentication. First thing I tried is the official example available here. But when I try to execute it(with some small ch

4条回答
  •  野性不改
    2020-11-28 14:10

    This code snippet worked for me. You have to provide the realm which you can get by looking at the 401 response header you get from the host.

    val credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(AuthScope.ANY,
      new UsernamePasswordCredentials(user, password));
    val authCache = new BasicAuthCache();
    val digestScheme = new DigestScheme();
    
    digestScheme.overrideParamter("realm", "**Name of the Realm**");
    // Nonce value
    digestScheme.overrideParamter("nonce", "whatever");
    
    authCache.put(targetHost, digestScheme);
    
    context.setCredentialsProvider(credsProvider);
    context.setAuthCache(authCache);
    
    val httpget = new HttpGet(url);
    
    val response = httpClient.execute(targetHost, httpget, context);
    

提交回复
热议问题