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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T19:50:22+00:00 2026-06-08T19:50:22+00:00

Does anyone know how to check if a webpage is asking for HTTP Authentication

  • 0

Does anyone know how to check if a webpage is asking for HTTP Authentication via C# using the WebRequest class? I’m not asking how to post Credentials to the page, just how to check if the page is asking for Authentication.

Current Snippet to get HTML:

WebRequest wrq = WebRequest.Create(address);
wrs = wrq.GetResponse();
Uri uri = wrs.ResponseUri;
StreamReader strdr = new StreamReader(wrs.GetResponseStream());
string html = strdr.ReadToEnd();
wrs.Close();
strdr.Close();
return html;

PHP Server side source:

<?php
if (!isset($_SERVER['PHP_AUTH_USER'])) {
    header('WWW-Authenticate: Basic realm="Secure Sign-in"');
    header('HTTP/1.0 401 Unauthorized');
    echo 'Text to send if user hits Cancel button';
    exit;
} else {
    echo "<p>Hello {$_SERVER['PHP_AUTH_USER']}.</p>";
    echo "<p>You entered {$_SERVER['PHP_AUTH_PW']} as your password.</p>";
}
?>
  • 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-08T19:50:24+00:00Added an answer on June 8, 2026 at 7:50 pm

    WebRequest.GetResponse returns an object of type HttpWebResponse. Just cast it and you can retrieve StatusCode.

    However, .Net will give you an exception if it receives a response of status 4xx or 5xx (thanks for your feedback).
    There is a little workaround, check it out:

        HttpWebRequest wrq = (HttpWebRequest)WebRequest.Create(@"http://webstrand.comoj.com/locked/safe.php");
        HttpWebResponse wrs = null;
    
        try
        {
            wrs = (HttpWebResponse)wrq.GetResponse();
        }
        catch (System.Net.WebException protocolError)
        {
            if (((HttpWebResponse)protocolError.Response).StatusCode == HttpStatusCode.Unauthorized)
            {
                //do something
            }
        }
        catch (System.Exception generalError)
        {
            //run to the hills
        }
    
        if (wrs.StatusCode == HttpStatusCode.OK)
        {
            Uri uri = wrs.ResponseUri;
            StreamReader strdr = new StreamReader(wrs.GetResponseStream());
    
            string html = strdr.ReadToEnd();
            wrs.Close();
            strdr.Close();
        }
    

    Hope this helps.

    Regards

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

Sidebar

Related Questions

Does anyone know how to check in the code to Visual svn server using
Does anyone know of a utility that can check whether a HTTP request/response is
Does anyone know how to check if a key exists using Membase Client (Enyim)?
Does anyone know how to check whether certain sheets exist or not in an
Does anyone know how to check if a Youtube video is embeddable using the
does anyone know how to check if a file or directory is either a
Does anyone know how to check if the called ui (custom) has also options
Does anyone know of a C# spell check library that would underline misspelled works
Does anyone know if there's a way to check the number of messages in
Does anyone know how to get the list of items and check/uncheck items in

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.