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 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?

Oktay
  • 127
  • 1
  • 2
  • 15
  • Have you used the debugger to determine what happens after `HttpWebResponse response2 = (HttpWebResponse)request2.GetResponse();` is called on a non-existent site when you are at home? – psubsee2003 Nov 08 '12 at 09:07
  • yes I tried use debugger at line HttpWebResponse response2 = (HttpWebResponse)request2.GetResponse(); didn't show anything.I tried at response2.close(); program stops (continue to run however doesn't continue to following commands) doesn't show anything. so even doesn't work until the line (response2.close();) so I can't see again what is happening with debugger. – Oktay Nov 08 '12 at 09:17
  • in addition at company when I enter an non-existing it doesn't close response as well. after HttpWebResponse response2 = (HttpWebResponse)request2.GetResponse(); line goes to webexception. interesting point is here why at home it doesn't go to webexception? – Oktay Nov 08 '12 at 09:23
  • That's what I'm trying to ask.... how do you know it doesn't? Have you set a breakpoint in the `WebException` catch block to see? – psubsee2003 Nov 08 '12 at 09:25
  • after you ask questions i try couple of things and I see that actually it works until webexception debugger shows the remote name couldn't be resolved:'www.oijsasad.com' but why it doesn't show anything after that and even I add listbox.items.add("sdsadsa"); directly after webexception it doesn't show it. It is getting more interesting. – Oktay Nov 08 '12 at 09:39
  • But does it ever get to the `WebException` block? From what I mentioned in my answer, your `WebException` may throw an exception that will be handled further up the call stack – psubsee2003 Nov 08 '12 at 09:48

2 Answers2

0

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
}
psubsee2003
  • 8,563
  • 8
  • 61
  • 79
0

Thanks to psubsee2003. I got my answer. here is my code which is working properly. I added following codes to webexception.

if (ex.Status == WebExceptionStatus.ProtocolError)

 {
   using (WebResponse response = ex.Response)

{
   HttpWebResponse httpResponse = (HttpWebResponse)response;
   listBox2.Items.Insert(0, "Status:" + httpResponse.StatusCode);

}
 }

else
{
   listBox2.Items.Insert(0, "Status: " + ex.Status);
 }
Oktay
  • 127
  • 1
  • 2
  • 15