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. ECMAScript 6入门 - let和const命令

    详细学习链接: http://es6.ruanyifeng.com/#docs/let let命令 基本用法 ES6新增了let命令,用来声明变量.它的用法类似于var,但是所声明的变量,只在let命 ...

  2. Java常见错误

    1.NullPointerExceptin 空指针异常 a.引用没有初始化就使用 b.引用置空了,仍然被使用 2.IndexOutofBoundsException 下标越界 a.数组下标小于0 或者 ...

  3. python进阶

    决定在python上有所突破 先看看知乎: 如何面试Python后端工程师? Python之美 - 知乎专栏 Python书籍推荐

  4. libCEF总结01下载、编译、入门

    目 录 第1章 下载    1 1.1 下载    1 1.2 合并    1 第2章 cmake    4 2.1 编译简介    4 2.2 下载cmake    4 2.3 运行cmake    ...

  5. jmeter 监听的介绍

    一个侦听器是一个组件,显示的结果 样本. 结果可以显示在一个树,表格,图表或简单地写入到日志中 文件. 查看的内容反应任何给定的采样器,添加的监听器” 视图 结果树 ”或“ 视图的结果表 一个测试计划 ...

  6. 文件I/O

    Linux顶层目录结构: /              根目录├── bin     存放用户二进制文件├── boot    存放内核引导配置文件├── dev     存放设备文件├── etc  ...

  7. mvc伪静态<四> 伪静态后静态页面或者引用的css和图片失效

    引用的css和图片失效的解决办法 把样式引用文件的相对路径改成绝对路径就可以了 比如原先的引用路径为:<link href="~/Content/css/style.css" ...

  8. webService—调用webService

    Web service是一个平台独立的,低耦合的,自包含的.基于可编程的web的应用程序,可使用开放的XML(标准通用标记语言下的一个子集)标准来描述.发布.发现.协调和配置这些应用程序,用于开发分布 ...

  9. SAP采购申请审批记录增强

    业务需要,开发就搞.... EBAN中增强结构:CI_EBANDB ANAME 类型 UNAME 用户名 ADATE 类型 AEDAT DATS 更改日期 ATIME 类型 UZEIT TIMS 时间 ...

  10. apache 修改最大连接数

    1.在哪里设置? 服务器的为FreeBSD 6.2 ,apache 2.24,使用默认配置(FreeBSD 默认不加载自定义MPM配置),默认最大连接数是250 在/usr/local/etc/apa ...