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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T03:22:27+00:00 2026-06-14T03:22:27+00:00

I have a problem with httpwebrequest exception. I use the following code to make

  • 0

I have a problem with httpwebrequest exception. I use the following code to make a request and catch the exception.

try
{    
    Uri url= new Uri("https://www.example.com");
    HttpWebRequest request2 =(HttpWebRequest)WebRequest.Create(url);

    request2.Timeout = 10000;

    HttpWebResponse response2 = (HttpWebResponse)request2.GetResponse();
    response2.Close();
}
catch (TimeoutException)
{
    listBox.Items.Insert(0, "Timeout");
}
catch (WebException ex)
{
    using (WebResponse response = ex.Response)
    {
        HttpWebResponse httpResponse = (HttpWebResponse)response;
        listBox.Items.Insert(0, "Status code(Benchmark):" + httpResponse.StatusCode);
    }
}
catch
{
    listBox.Items.Insert(0, "Failure");
}

At company network when I enter a non-existing url such as http://www.oiuahsdupiasduiuhid.com; it throws webexception . I got status code: not found or service unavailable. However If I try it at home, it doesn’t throw any exception. It waits around 1 second and then without any error stops working. I delete all exceptions to see what is happening but the problem is it doesn’t show any error.
Do you have any idea about what is the problem?

Or any recommendation, how can I handle this problem with a different way?

  • 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-14T03:22:28+00:00Added an answer on June 14, 2026 at 3:22 am

    Without knowing more about your application design, specifically exception handling further up the call stack, it is hard to say why it is behaving like it is when you are at home.

    But when I just tried your exact code, it did throw a WebException, however httpResponse.StatusCode throws a NullReferenceException because httpResponse is null. If you are potentially swallowing this exception further up the call stack, it could explain the situation you are seeing.

    httpResponse is going to be null in many WebException cases because your request did not receive any response, specifically in the timeout scenario.

    Before casting WebException.Response, you need to check the WebException.Status property. If that status suggests a response was received, then you can go check WebException.Response, otherwise it is just going to be null. Try something like:

    if(e.Status == WebExceptionStatus.ProtocolError) {
        listBox.Items.Insert("Status Code : {0}", 
           ((HttpWebResponse)e.Response).StatusCode);
    }
    else
    {
        listBox.Items.Insert("Status : {0}", ex.Status);
    }
    

    As a side note, your response2.Close(); is never going to be called when HttpWebResponse response2 = (HttpWebResponse)request2.GetResponse(); throws an exception, so you should be wrapping it in a using block:

    using(HttpWebResponse response2 = (HttpWebResponse)request2.GetResponse())
    {
        // do something with response
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have one problem with HttpWebRequest and Uri class of .NET framework (i use
I'm using this code, to make a request to a given URL: private static
I have problem with UIWebView delay when the load image from url. In my
i have a problem in certain company in germany. They use proxy in their
I have a strange problem: I am getting the html source from url using
we have an asp.net application, that makes heavy use of REST/json services through httpwebrequest
I have the following problem. I contact an address which I know employs a
Here is code I use to POST into RESTful web service. My problem is
I have a problem with WebRequest in Silverlight 4. In method ReadCallback at request.EndGetResponse(result)
I have a basic C# HttpWebRequest. My problem is the page that it is

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.