2

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
  • Follow up: did any of the answers helped you? If so, please, mark as correct Commented Jul 24, 2012 at 19:55

2 Answers 2

4

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

Sign up to request clarification or add additional context in comments.

8 Comments

The main issue with this is once it hits the line below it throws a WebException "Protocol error" not allowing me to get anything from it. "wrs = wrq.GetResponse();" leaving wrs null allowing me to get no information from it.
Sorry: you get the exception when you call wrq.GetResponse()?
Correct. "Protocol error" when accessing the page that requires credentials.
Is the page you're requesting for on a public website? I'd like to run some tests here
It is now. Thanks for looking into this :) Here is the address: webstrand.comoj.com/locked/safe.php
|
1

Might want to try

WebClient wc = new WebClient();
CredentialCache credCache = new CredentialCache();

If you can work with WebClient instead of WebRequest, you should it's a bit higher level, easier to handle headers etc.

Also, might want to check this thread: System.Net.WebClient fails weirdly

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.