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"?
views:
110answers:
3
+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
2009-12-28 06:33:09
So... you're saying I have to list them all out? That's exactly what I'm trying to avoid :)
Mark
2009-12-28 06:37:56
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
2009-12-28 07:57:19
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
2009-12-28 20:48:03
"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
2009-12-28 20:50:39
+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
2009-12-28 07:48:05
That's unfortunate... I think everything should have a `Clone()` method, but oh well.
Mark
2009-12-28 07:55:46
How would you clone the open network connection that's part of the HttpWebRequest?
John Saunders
2009-12-28 07:57:58
@John SaundersConnection property in HttpWebRequest is just a header, not a physical connection. And processing HttpWebResponse is another task here.
terR0Q
2009-12-28 08:06:55
@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
2009-12-29 03:32:12
+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
2009-12-28 08:00:13
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
2009-12-28 08:32:31