Created
May 16, 2013 16:34
-
-
Save abombss/5593069 to your computer and use it in GitHub Desktop.
Inherit from HttpWebRequest... pure evil will ensue.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MyHttpWebRequest : HttpWebRequest | |
{ | |
private static readonly IFormatterConverter Converter = new FormatterConverter(); | |
private static readonly StreamingContext Context = new StreamingContext(StreamingContextStates.Clone); | |
public MyHttpWebRequest(string url) | |
: this(new Uri(url)) | |
{ | |
} | |
public MyHttpWebRequest(Uri uri) | |
: this(CreateHttpWebRequestSerializationInfo(uri), Context) | |
{ | |
} | |
protected MyHttpWebRequest(SerializationInfo serializationInfo, StreamingContext streamingContext) | |
: base(serializationInfo, streamingContext) | |
{ | |
} | |
private static SerializationInfo CreateHttpWebRequestSerializationInfo(Uri uri) | |
{ | |
var httpWebRequest = WebRequest.CreateDefault(uri) as ISerializable; | |
{ | |
var serializationInfo = new SerializationInfo(typeof(HttpWebRequest), Converter); | |
httpWebRequest.GetObjectData(serializationInfo, Context); | |
return serializationInfo; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This doesn't work out of the box. After instantiating the class you need a bit of reflection to set the _Url field. It looks like at some point they added _Url and serialized version only sets _OrigUrl.