https://gist.github.com/abombss/2720757

public static class WebRequestExtensions
{
public static HttpWebRequest CloneRequest(this HttpWebRequest originalRequest, Uri newUri)
{
return CloneHttpWebRequest(originalRequest, newUri);
} public static WebRequest CloneRequest(this WebRequest originalRequest, Uri newUri)
{
var httpWebRequest = originalRequest as HttpWebRequest;
if (httpWebRequest != null) return CloneHttpWebRequest(httpWebRequest, newUri);
return CloneWebRequest(originalRequest, newUri);
} private static HttpWebRequest CloneHttpWebRequest(HttpWebRequest old, Uri newUri)
{
var @new = (HttpWebRequest) WebRequest.Create(newUri);
CopyWebRequestProperties(old, @new);
CopyHttpWebRequestProperties(old, @new);
CopyHttpWebRequestHeaders(old, @new);
return @new;
} private static WebRequest CloneWebRequest(WebRequest old, Uri newUri)
{
var @new = WebRequest.Create(newUri);
CopyWebRequestProperties(old, @new);
CopyWebRequestHeaders(old, @new);
return @new;
} private static void CopyWebRequestProperties(WebRequest old, WebRequest @new)
{
@new.AuthenticationLevel = old.AuthenticationLevel;
@new.CachePolicy = old.CachePolicy;
@new.ConnectionGroupName = old.ConnectionGroupName;
@new.ContentType = old.ContentType;
@new.Credentials = old.Credentials;
@new.ImpersonationLevel = old.ImpersonationLevel;
@new.Method = old.Method;
@new.PreAuthenticate = old.PreAuthenticate;
@new.Proxy = old.Proxy;
@new.Timeout = old.Timeout;
@new.UseDefaultCredentials = old.UseDefaultCredentials; if (old.ContentLength > ) @new.ContentLength = old.ContentLength;
} private static void CopyWebRequestHeaders(WebRequest old, WebRequest @new)
{
string[] allKeys = old.Headers.AllKeys;
foreach (var key in allKeys)
{
@new.Headers[key] = old.Headers[key];
}
} private static void CopyHttpWebRequestProperties(HttpWebRequest old, HttpWebRequest @new)
{
@new.Accept = old.Accept;
@new.AllowAutoRedirect = old.AllowAutoRedirect;
@new.AllowWriteStreamBuffering = old.AllowWriteStreamBuffering;
@new.AutomaticDecompression = old.AutomaticDecompression;
@new.ClientCertificates = old.ClientCertificates;
@new.SendChunked = old.SendChunked;
@new.TransferEncoding = old.TransferEncoding;
@new.Connection = old.Connection;
@new.ContentType = old.ContentType;
@new.ContinueDelegate = old.ContinueDelegate;
@new.CookieContainer = old.CookieContainer;
@new.Date = old.Date;
@new.Expect = old.Expect;
@new.Host = old.Host;
@new.IfModifiedSince = old.IfModifiedSince;
@new.KeepAlive = old.KeepAlive;
@new.MaximumAutomaticRedirections = old.MaximumAutomaticRedirections;
@new.MaximumResponseHeadersLength = old.MaximumResponseHeadersLength;
@new.MediaType = old.MediaType;
@new.Pipelined = old.Pipelined;
@new.ProtocolVersion = old.ProtocolVersion;
@new.ReadWriteTimeout = old.ReadWriteTimeout;
@new.Referer = old.Referer;
@new.Timeout = old.Timeout;
@new.UnsafeAuthenticatedConnectionSharing = old.UnsafeAuthenticatedConnectionSharing;
@new.UserAgent = old.UserAgent;
} private static void CopyHttpWebRequestHeaders(HttpWebRequest old, HttpWebRequest @new)
{
var allKeys = old.Headers.AllKeys;
foreach (var key in allKeys)
{
switch (key.ToLower(CultureInfo.InvariantCulture))
{
// Skip all these reserved headers because we have to set them through properties
case "accept":
case "connection":
case "content-length":
case "content-type":
case "date":
case "expect":
case "host":
case "if-modified-since":
case "range":
case "referer":
case "transfer-encoding":
case "user-agent":
case "proxy-connection":
break;
default:
@new.Headers[key] = old.Headers[key];
break;
}
}
}
}

C# WebRequestExtensions的更多相关文章

  1. WP8中使用async/await扩展HttpWebRequest

    前文讲到WP8中使用Async执行HTTP请求,用了微软提供的扩展.下面提供了一种方法,自己实现HttpWebRequest的扩展. 随后就可以使用 await HttpWebRequest.GetR ...

随机推荐

  1. javascript AOP实现

    参考:http://www.cnblogs.com/rubylouvre/archive/2009/08/08/1541578.html function Person(){ this.say = f ...

  2. phalcon: 过滤(Phalcon\Filter())

    过滤,就是清除不需要的数据,留下想要的数据. 其调用方法如下,一: $filter = new \Phalcon\Filter(); $filter->sanitize("some(o ...

  3. 浅谈全区全服架构的SNS游戏后台

    版权声明:本文由梁本志原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/198 来源:腾云阁 https://www.qclo ...

  4. MHA的几种死法-叶良辰

    master不通      找到最新slave           找到有所有其他slave缺失relay log的 slave      选主           配置文件指定           ...

  5. MySQL锁监视器

    还在为看不懂何登成的加锁处理分析文章感到羞愧吗? 还在因为何大师的笔误,陷入深深的迷茫吗? 只要你拥有大于5.6.16版本的MySQL,锁监视器你值得拥有! 快速入门 开启 set GLOBAL in ...

  6. 三种语言(c++、as、lua)中函数的差异性

    对于不同的语言, 尤其是静态语言和动态语言, 对于函数的定义(即如何看待一个函数)和处理截然不同.具体来说可以分为两类: 1.将函数视为第一类型值, 即函数和其他的对象一样, 都是语言中一个普通的对象 ...

  7. win8style布局

    <?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android=" ...

  8. 制作Aspose CHM文档的过程记录

    欢迎和大家交流技术相关问题: 邮箱: jiangxinnju@163.com 博客园地址: http://www.cnblogs.com/jiangxinnju GitHub地址: https://g ...

  9. linux中无 conio.h的解决办法

    conio.h不是C标准库中的头文件,在ISO和POSIX标准中均没有定义.conio是Console Input/Output(控制台输入输出)的简写,其中定义了通过控制台进行数据输入和数据输出的函 ...

  10. HTML4 和 HTML5 的10个关键区别

    HTML5是HTML标准的下一个版本.越来越多的程序员开始HTML5来构建网站.如果你同时使用HTML4和HTML5的话 ,你会发现用HTML5从头构建,比从HTML4迁移到HTML5要方便很多.虽然 ...