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

  • Home
  • SEARCH
  • 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 8931021
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T09:01:04+00:00 2026-06-15T09:01:04+00:00

I am trying to obtain the headers of a webpage. The code block below

  • 0

I am trying to obtain the headers of a webpage. The code block below does the job.
However, when a page is 401 denied, I can’t get any headers at all. Is there a way?

Thanks

public Int32 CheckURLStatus(string uri, out HttpWebResponse _response)
    {
        //Get response header
        _response = null;
        ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
        HttpWebRequest _request = (HttpWebRequest)WebRequest.Create(uri);
        _request.Method = "GET";
        _request.AllowAutoRedirect = true;
        _request.Accept = "*/*";
        _request.MaximumAutomaticRedirections = 4;
        _request.UseDefaultCredentials = true;
        _request.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
        Int32 statusCode = 404;
        try
        {
            _response = (HttpWebResponse)_request.GetResponse();
            statusCode = Convert.ToInt32(_response.StatusCode);
        }
        catch (Exception ex)
        {
            _response = null;
        }
        return statusCode;
    }
  • 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-15T09:01:05+00:00Added an answer on June 15, 2026 at 9:01 am

    In case of any StatusCode that is not 200 the GetResponse() method throws a WebException. To get the status code you have to handle this Exception and get the StatusCode from the Response that is in the Exception.

    I´ve changed your code to get the response code even in the case of WebException:

    public Int32 CheckURLStatus(string uri, out HttpWebResponse _response)
        {
            //Get response header
            _response = null;
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
            HttpWebRequest _request = (HttpWebRequest)WebRequest.Create(uri);
            _request.Method = "GET";
            _request.AllowAutoRedirect = true;
            _request.Accept = "*/*";
            _request.MaximumAutomaticRedirections = 4;
            _request.UseDefaultCredentials = true;
            _request.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
            Int32 statusCode = 404;
            try
            {
                _response = (HttpWebResponse)_request.GetResponse();
                statusCode = Convert.ToInt32(_response.StatusCode);
            }
            catch (WebException ex)
            {
                statusCode = Convert.ToInt32(((HttpWebResponse)ex.Response).StatusCode);
                _response = null;
            }
            return statusCode;
        }
    

    Also you can check the ex.Response.Headers and you will have the HTTP headers of the response.

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

Sidebar

Related Questions

I'm trying to log into a site and obtain the code back, but i'm
I am trying to obtain some information from a webpage that requires login. I
I'm trying to obtain the keywords from an HTML page that I'm scraping with
As stated, I'm trying to obtain column headers consisting of an icon and wrappable
I am trying to obtain the url from the innerHTML of a web page
I am trying to obtain embeded code through extracting variables from Youtube video URLs.
I am trying to obtain an element width using the following code just before
I'm trying to obtain the machine precision on gmp variables. To that end, I
I'm trying to obtain a smooth lighting effect on a solid with openGLES 2.0.
I am trying to obtain the value part of this syntax: [name:value] I got

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.