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. java 反射机制的实例

    [案例1]通过一个对象获得完整的包名和类名 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 package Reflect;   /**  * 通过一个对象获得完整的包名和类名 ...

  2. Lecture Notes: Macros

    原论文链接失效,特在这里保存一份 http://www.apl.jhu.edu/~hall/Lisp-Notes/Macros.html Lisp functions take Lisp values ...

  3. Spring AOP propagation七种属性值

    <!-- 配置事务通知 --> <tx:advice id="advice" transaction-manager="transactionManag ...

  4. 鼠标经过容器放大--css3

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  5. chrome浏览器下页面顶部出现一条空白解决

    最近遇到页面在chrome浏览器下,顶部会出现一条空白的问题.后来知道是bom头的问题. 1.什么是bom头? BOM签名的意思就是告诉编辑器当前文件采用何种编码,方便编辑器识别,但是BOM虽然在编辑 ...

  6. python 电影下载链接爬虫

    V1.0 功能:从比较知名的几个电影下载网站爬取下载链接,并自动打印出来: 代码: # -*- coding: utf8 -*- from bs4 import BeautifulSoup impor ...

  7. Qt之属性系统

    简述 Qt提供一个类似于其它编译器供应商提供的复杂属性系统(Property System).然而,作为一个编译器和平台无关的库,Qt不能够依赖于那些非标准的编译器特性,比如:__property或者 ...

  8. 笔记7:winfrom的一些知识点(一)

    一.MDI窗体 private void 添加窗体ToolStripMenuItem_Click(object sender, EventArgs e) { Form2 frm1 = new Form ...

  9. file类之目录

    可以解决的问题是:                有时需要列出目录下指定类型的文件,比如java,txt等扩展名的文件,可以使用File类的下述两个方法,列出指定类型的文件. /* file类实现两个 ...

  10. Subscribe的第四个参数用法

    看别人的代码真的是很好的学习过程啊 之前用Subscribe订阅的时候都是简单的用法形如: ros::Subscriber sub = node.subscribe<uhf_rfid_api:: ...