views:

110

answers:

3

I want to set some properties of an HttpWebRequest and then download some files asynchronously. However, I need one copy of HttpWebRequest for each download, so how can I clone it so that I don't have to copy each property "by hand"?

+1  A: 

Create your own class to hold the properties you're interested in. That class can implement IClonable if necessary. In any case, use these properties to initialize each new instance of HttpWebRequest.

John Saunders
So... you're saying I have to list them all out? That's exactly what I'm trying to avoid :)
Mark
There's no such thing as magic. A class that is not clonable cannot be cloned. If it can't be serialized, then you can't serialize it. That's life.
John Saunders
No one said anything about magic. I was hoping maybe I could do some kind of introspection and nab the properties that way... who knows. I'm discovering all sorts of things about C# still...
Mark
"Reflection", not "introspection". You could use Reflection to capture all the public read/write properties of one instance, and to copy them to another instance.
John Saunders
+1  A: 

HttpWebRequest does not support serialization or cloning, thus you have to implement your own mechanism to save/copy it. You can use serialized (or clonable) derived class or some wrapper for this task.

terR0Q
That's unfortunate... I think everything should have a `Clone()` method, but oh well.
Mark
How would you clone the open network connection that's part of the HttpWebRequest?
John Saunders
@John SaundersConnection property in HttpWebRequest is just a header, not a physical connection. And processing HttpWebResponse is another task here.
terR0Q
@John: Oh, well I don't really care about the connection anyway. I just want the properties. Actually, I basically want to use it as a 'template'; I want to copy it before any connection is made.
Mark
+1  A: 

you need to be careful when creating a number of connection using HttpWebRequest. http://arnosoftwaredev.blogspot.com/2006/09/net-20-httpwebrequestkeepalive-and.html As it is not serializable you can create a wrapper class

Tinku
Hrm... well, I went through great pains to put a cap on the number of connections anyway. I only intend on using about 8.
Mark