views:

814

answers:

3

My objective is to retry an asynchronous HttpWebRequest when it fails.

When I Abort() an HttpWebRequest, I cannot BeginGetResponse() again. So the only way to request again probably is recreate the HttpWebRequest object. It seems to require a lot of work since I would have to copy all properties from the old object. Is there any shortcut?

Note: I think that serialization it would solve my problem, but this class won't serialize, as discussed in a previous question.

Update Removed example source code because it was unnecessary

Current view on this matter There is no shortcut, the only way to redo the request is creating another HttpWebRequest object the same way you created the original one.

+2  A: 

Where are you trying to serialize the request? It might be a good idea to serialize the request before you try to make it, and then resend the request on failure (from the serialized instance).

Also, you might want to remove the proxy, serialize the instance, and then set the proxy back, since that seems to be where the problem lies in serializing the request.

casperOne
The full code doesn't contains the serialization because it didn't work, but if I include the serialization in the code I would certainly do as you told. The piece of code I wrote for serialization tests doesn't have any line about proxies, and I don't know how to remove it. Do you?
Jader Dias
removed the serialization sample code and replaced it with a link to another SO Question about serializing HttpWebRequest (http://stackoverflow.com/questions/351265/httpwebrequest-wont-serialize)
Jader Dias
+2  A: 

There is no shortcut, the only way to redo the request is creating another HttpWebRequest object the same way you created the original one.

Jader Dias
+9  A: 

It's not possible.

An HTTPWebRequest instance represents literally one request instance (although there are edge cases where it will retry internally), and is therefore not something which can logically be repeated. It is capable of returning exactly one response (request/response being a logical pair), and although you can inspect that response multiple times you'll get exactly the same object.

Basically, you're thinking an HTTPWebRequest instance is a sort of factory, but it's really concrete. You have to create another instance.

annakata