Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8598729
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T01:13:26+00:00 2026-06-12T01:13:26+00:00

I am tring to perform Digest Authentication using the HttpClient library, but I keep

  • 0

I am tring to perform Digest Authentication using the HttpClient library, but I keep getting: HTTP/1.1 401 Unauthorized.

When I try the request from Firefox it works fine and I get a response correctly, so I know the server authentication is working fine.

Update: moved Working code to answer.

enter image description here

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T01:13:28+00:00Added an answer on June 12, 2026 at 1:13 am

    The following code worked for me

    import java.util.Random;
    
    import org.apache.http.HttpEntity;
    import org.apache.http.HttpHost;
    import org.apache.http.HttpResponse;
    import org.apache.http.auth.AuthScope;
    import org.apache.http.auth.UsernamePasswordCredentials;
    import org.apache.http.client.AuthCache;
    import org.apache.http.client.ResponseHandler;
    import org.apache.http.client.methods.HttpGet;
    import org.apache.http.impl.auth.DigestScheme;
    import org.apache.http.impl.client.BasicResponseHandler;
    import org.apache.http.impl.client.DefaultHttpClient;
    import org.apache.http.client.protocol.ClientContext;
    import org.apache.http.impl.client.BasicAuthCache;
    import org.apache.http.protocol.BasicHttpContext;
    import org.apache.http.util.EntityUtils;
    
    /**
     * A simple example that uses HttpClient to execute an HTTP request against a
     * target site that requires user authentication.
     */
    public class RestClient {
    
    public static void main(String args[]) throws Exception {
    
        HttpHost targetHost = new HttpHost("localhost", 8001, "http");
        DefaultHttpClient httpclient = new DefaultHttpClient();
    
        final String userName = "admin";
        final String password = "password";
    
        httpclient.getCredentialsProvider().setCredentials(
                new AuthScope("localhost", 8001), 
                new UsernamePasswordCredentials(userName, password));   
    
    
        // Create AuthCache instance
        AuthCache authCache = new BasicAuthCache();
        // Generate DIGEST scheme object, initialize it and add it to the local
        // auth cache
        DigestScheme digestAuth = new DigestScheme();
        // Suppose we already know the realm name
        digestAuth.overrideParamter("realm", "some realm");
        // Suppose we already know the expected nonce value
        digestAuth.overrideParamter("nonce", "whatever");
        authCache.put(targetHost, digestAuth);
    
        // Add AuthCache to the execution context
        BasicHttpContext localcontext = new BasicHttpContext();
        localcontext.setAttribute(ClientContext.AUTH_CACHE, authCache);
    
        HttpGet httpget = new HttpGet("http://localhost:8001/rest/test");
    
        try {
            HttpResponse response = httpclient.execute(targetHost, httpget, localcontext);
            HttpEntity entity = response.getEntity();
    
            System.out.println("----------------------------------------");
            System.out.println(response.getStatusLine());
            if (entity != null) {
                System.out.println("Response content length: " + entity.getContentLength());
            }
            EntityUtils.consume(entity);
        } finally {
            httpclient.getConnectionManager().shutdown();
        }
    }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to perform grayscale transformation on frames on a video ,but keep getting Assertation
i'm tring to start a new activity from the main one but i'm getting
I am using Jack as JavaScript mocking library. http://github.com/keronsen/jack . I am also using
I'm trying perform a relatively basic TCP socket send/receive operation on Windows CE using
I am trying to perform a search for documents within a SharePoint site, but
Trying to perform a nested object form. The page loads with no errors, but
I am trying to perform a Calculus Cross Product calculation using Linq and trying
Am trying to perform a select into outfile, am having following errors My request:
I'm trying to perform a simple animation using Cocoa. One button causes an image
I'm trying to perform git push heroku master But the PC i am working

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.