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. OpenCV3编程入门笔记(6)自我验证各函数程序代码

    // asw.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <opencv2/opencv.hpp> usi ...

  2. 【BZOJ 2818】Gcd - 筛法求素数&phi()

    题目描述 给定整数,求且为素数的数对有多少对. 分析 首先筛出所有的素数. 我们考虑枚举素数p,统计满足的个数,等价于统计的个数,即统计以内满足互质的有序数对个数. 不难发现,也就是说,我们只要预处理 ...

  3. jmeter 监听的介绍

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

  4. java操作数据库出现(][SQLServer 2000 Driver for JDBC]Error establishing socket.)的问题所在即解决办法

    在进行jdbc直接操作数据库时    : 我们需要对该工程进行一次导包(引入"msbase.jar" "mssqlserver.jar" "msuti ...

  5. LDM和STM指令

    LDM批量加载/STM批量存储指令可以实现一组寄存器和一块连续的内存单元之间传输数据. 允许一条指令传送16个寄存器的任意子集和所有寄存器,指令格式如下: LDM{cond}  mode  Rn{!} ...

  6. Best Time to Buy and Sell Stock II [LeetCode]

    Problem Description: http://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/ Basic idea: ...

  7. 你不知道的JavaScript--数组进阶全掌握

    转载 http://blog.csdn.net/i10630226/article/details/49764537 在程序语言中数组的重要性不言而喻,JavaScript中数组也是最常使用的对象之一 ...

  8. Firefox火狐Flash插件卡死问题完美解决方法(转载)

    http://www.ihacksoft.com/firefox-flash-protectedmode.html 其实这个问题以前就出现过,而最近该问题又出现在最新的 Windows 8.1 系统中 ...

  9. MVC之视图的布局

      1. RenderBody  布局在Razor引擎中没有了“母版页”,取而代之的是叫做“布局”的页面(_Layout.cshtml)放在了共享视图文件夹中.在这个页面中,会看到标签里有这样一条语句 ...

  10. FG函数模块

    ALDB:                             ABAP/4上的逻辑数据库                                   选择屏幕的修改更新 BUBA_4: ...