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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T11:18:05+00:00 2026-05-20T11:18:05+00:00

I am trying to login to my web app using HttpWebRequest but I keep

  • 0

I am trying to login to my web app using HttpWebRequest but I keep getting the following error:

System.Net.WebException: The remote server returned an error: (401) Unauthorized.

Fiddler has the following output:

Result Protocol  Host           URL
200    HTTP      CONNECT        mysite.com:443
302    HTTPS     mysite.com     /auth
401    HTTP      mysite.com     /auth

This is what I’m doing:

// to ignore SSL certificate errors
public bool AcceptAllCertifications(object sender, System.Security.Cryptography.X509Certificates.X509Certificate certification, System.Security.Cryptography.X509Certificates.X509Chain chain, System.Net.Security.SslPolicyErrors sslPolicyErrors)
{
     return true;
}

try
{   
    // request
    Uri uri = new Uri("https://mysite.com/auth");
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri) as HttpWebRequest;
    request.Accept = "application/xml";

    // authentication
    string user = "user";
    string pwd = "secret";   
    string auth = "Basic " + Convert.ToBase64String(System.Text.Encoding.Default.GetBytes(user + ":" + pwd));
    request.Headers.Add("Authorization", auth);
    ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(AcceptAllCertifications);

    // response.
    HttpWebResponse response = (HttpWebResponse)request.GetResponse();

    // Display 
    Stream dataStream = response.GetResponseStream();
    StreamReader reader = new StreamReader(dataStream);
    string responseFromServer = reader.ReadToEnd();
    Console.WriteLine(responseFromServer);

    // Cleanup 
    reader.Close();
    dataStream.Close();
    response.Close();
}
catch (WebException webEx)
{
   Console.Write(webEx.ToString());
}

I am able to log in to the same site with no problem using ASIHTTPRequest in a Mac app like this:

NSURL *login_url = [NSURL URLWithString:@"https://mysite.com/auth"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:login_url];
[request setDelegate:self];
[request setUsername:name];
[request setPassword:pwd];  
[request setRequestMethod:@"GET"];
[request addRequestHeader:@"Accept" value:@"application/xml"]; 
[request startAsynchronous];    
  • 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-20T11:18:06+00:00Added an answer on May 20, 2026 at 11:18 am

    Try this:

    var uri = new Uri("https:/example.com/auth");
    var request = (HttpWebRequest)WebRequest.Create(uri);
    request.Accept = "application/xml";
    
    // authentication
    var cache = new CredentialCache();
    cache.Add(uri, "Basic", new NetworkCredential("user", "secret"));
    request.Credentials = cache;
    
    ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(AcceptAllCertifications);
    
    // response.
    var response = (HttpWebResponse)request.GetResponse();
    

    Note the use of the NetworkCredential class instead of rolling your own authentication headers.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I keep getting 401 when trying to login via Oauth with Twitter. I'm using
I'm trying to login with the following controller action, but my login attempt keeps
I am trying to add some rewrite rules using UrlRewriter.net to my ASP.NET web
In ASP.NET web app, I have login.aspx. I force that every user access through
I'm trying to trim JSP whitespace using the trimSpaces directive in my web.xml, but
I'm doing a ASP.NET WebForms App with FB login. I'm using the latest Facebook
My web service app on my Windows XP box is trying to log in
I'm trying to implement OpenId login for a web application. Whenever new user who
I am trying to Login or SugarCrm using REST. I have this code,, //Login
I am trying to login using the library Mechanize. This the output i get

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.