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 6609795
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:45:36+00:00 2026-05-25T19:45:36+00:00

I have searched a LOT for this and could not find a decent solution.

  • 0

I have searched a LOT for this and could not find a decent solution. The one using credentials provider is bad as it make double the amount of calls opposed to what is required i.e. it fires the request , gets a 401 and only then fires the request with the web auth credentials.

Anyone who has used android’s httpclient library to do http post requests to a URL behind web auth successfully??

  • 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-05-25T19:45:37+00:00Added an answer on May 25, 2026 at 7:45 pm

    For HttpClient 4.0.x you use a HttpRequestInterceptor to enable preemptive authentication – since the AndroidHttpClient class doesn’t expose the addRequestInterceptor(..) method you’re probably going to have to use the DefaultHttpClient class.

    This example will spam user1/user1 to any server interested. Adjust the AuthScope if you care even one bit about security.

    DefaultHttpClient client = new DefaultHttpClient();
    client.getCredentialsProvider().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("user1", "user1"));
    client.addRequestInterceptor(new HttpRequestInterceptor() {
        public void process(HttpRequest request, HttpContext context) throws HttpException, IOException {
            AuthState state = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE);
            if (state.getAuthScheme() == null) {
                BasicScheme scheme = new BasicScheme();
                CredentialsProvider credentialsProvider = (CredentialsProvider) context.getAttribute(ClientContext.CREDS_PROVIDER);
                Credentials credentials = credentialsProvider.getCredentials(AuthScope.ANY);
                if (credentials == null) {
                    throw new HttpException();
                }
                state.setAuthScope(AuthScope.ANY);
                state.setAuthScheme(scheme);
                state.setCredentials(credentials);
            }
        }
    }, 0); // 0 = first, and you really want to be first.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have searched around a lot for a solution, but could not find any.
I have searched a lot but I could not find any good solution to
i have searched a lot but could not find the exact way to do
I have searched this site extensively but cannot find a solution. Here is the
I wish I would find an answer for this. I have searched and searched
I have searched a lot about UTF8 decoding, but not found the answer yet.
I have searched all around to find a solution to my problem, but i
I have searched a lot about the difference between Website and WebApplication but didn't
I have searched around, and it seems that this is a limitation in MS
I have searched the Web for this, but with no luck. Any idea? Thanks.

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.