Skip to content

Instantly share code, notes, and snippets.

@abombss
Created May 16, 2013 16:34
Show Gist options
  • Save abombss/5593069 to your computer and use it in GitHub Desktop.
Save abombss/5593069 to your computer and use it in GitHub Desktop.
Inherit from HttpWebRequest... pure evil will ensue.
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;
}
}
}
@abombss
Copy link
Author

abombss commented May 22, 2013

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment