views:

519

answers:

1

I am writing some C# 2.0 code which must do basic HTTP GETs and POSTs. I am using System.Net.HttpWebRequest to send both types of request and System.Net.HttpWebResponse to receive both. My code for GET looks like:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(String.Format("{0}?{1}",
    URLToHit,
    queryString));
request.Method = "GET";
request.Timeout = 1000; // set 1 sec. timeout
request.ProtocolVersion = HttpVersion.Version11; // use HTTP 1.1
try
{
    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
}
catch(WebException e)
{
    //if I do anything except swallow the exception here, I end up in some sort of endless loop in which the same WebException keeps being re-thrown by the GetResponse method. The exception is always right (ie: in cases when I'm not connected to a network, it gives a timed out error, etc...), but it should not be re-thrown!
}

And my POST code is very similar.

The last line works fine when URLToHit returns a HTTP status 200, but in any other circumstance (ie: non 200 HTTP status, no network connectivity, etc...), a System.Net.WebException is thrown (which is expected, according to MSDN docs). However, my code never makes progress past that line.

When I attempt to debug this, I find that I can't step over or continue past that last line. When I try to do so, the request is re-issued and the exception re-thrown.

Any ideas on what I can do to make the request only be issued once? I've never seen anything like this happen in any exception based code, and I'm out of ideas. Nothing like this happens in any other parts of my code, just the parts that deal with System.Net functionality and constructs.

Thanks!

(Update: added try/catch around the GetRequest method)

+1  A: 

There's no such thing as a "non-stop" exception in C#. To control what happens during an exception, you use a try-catch block.

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(String.Format("{0}?{1}",
    URLToHit,
    queryString));
request.Method = "GET";
request.Timeout = 1000; // set 1 sec. timeout
request.ProtocolVersion = HttpVersion.Version11; // use HTTP 1.1

try
{
    HttpWebResponse response = (HttpWebResponse)request.GetResponse();

    // Code here runs if GetResponse() was successful.
}
catch (WebException ex)
{
    // Code here runs if GetResponse() failed.
}

// Code here is always run unless another exception is thrown.

The reason there are no "non-stop exceptions" is because, if there is an exception, your code can't possibly do what you intended. For example, what would you expect the "response" variable to contain? What would you do with it? The try-catch block gives you full control over this.

Cybis
Understood. My exact problem is that this is very unexpected behavior. This exception seems to be thrown over and over again, even if I do catch it.
Aaron
I'm sorry if I misunderstood your question, but it seems that you're trying to step immediately past the failed "GetResponse()" call, as if you want either the exception to be ignored or not raised at all. Your sample code doesn't have a try-catch block. If it doesn't succeed, an exception is thrown
Cybis
No problem. I do not want the exception to be ignored. I want it to throw, clean up my current stack frame and bubble up to the caller. instead, it seems to re-execute code that caused the exception and, as a result, continually throw the same exception. Obviously, this is incorrect behavior.
Aaron
I see. There's nothing wrong with the code you posted though. The problem must lie elsewhere. Is this a multi-threaded application? Are there other places where a WebException can get thrown? If you put a breakpoint on the "GetResponse" call, can you single-step until that line is reached again?
Cybis
I use some asynchronous calls later in the app, but when this code is called, it's being called from the the main thread. in this code, the GetRequest is the first place that a WebException can be thrown.
Aaron
Also, if I put a breakpoint on the GetResponse call, I can't make any progress by stepping or continuing in the debugger. When I try to step or continue, the function seems to be executed again and the exception re-thrown. Also, I *can* swallow the exception and things work fine, but it's not ideal.
Aaron
I just can't seem to reproduce the problem you're having. I know what it's like when everything should work, but doesn't. I would create a fresh sample application starting with the snippet you posted, then gradually copy/paste relevant parts of your code into it until you finally reproduce the bug.
Cybis
Thanks, Cybis. I'll do that. I've got a workaround in place (swallowing the exception), which will have to do for now. I'm starting to think that this might be a bug in the CLR.
Aaron