C# WebRequestExtensions
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的更多相关文章
- WP8中使用async/await扩展HttpWebRequest
前文讲到WP8中使用Async执行HTTP请求,用了微软提供的扩展.下面提供了一种方法,自己实现HttpWebRequest的扩展. 随后就可以使用 await HttpWebRequest.GetR ...
随机推荐
- centos7命令
查看ip ip addr ip link 添加服务 systemctl enable nginx 添加防火墙端口 firewall-cmd --zone=public --add-port=80/tc ...
- C++从键盘输入文件结束符
当我们使用一个istream对象作为条件时,其效果是检测流的状态.如果流是有效的,即流未遇到错误,那么检测成功.当遇到文件结束符,或遇到一个无效输入时(例如需要将输入读到一个int变量 ...
- 【BZOJ 2243】染色 - 树链剖分+线段树
#include <cstdio> #include <cstring> #include <cstdlib> using namespace std; const ...
- 修改IIS文件上传大小限制
如果要上传的文件比较大,则需在IIS中修改文件上传的大小限制,否则无法上传(表现为程序不报错,而上传进度到100%后没有反应). 点击"管理"下的"配置编辑器" ...
- hdu 4548 筛法求素数 打表
题目:http://acm.hdu.edu.cn/showproblem.php?pid=4548 Problem Description 小明对数的研究比较热爱,一谈到数,脑子里就涌现出好多数的问题 ...
- 《Java程序设计》第十周学习总结
20145224 <Java程序设计>第十周学习总结 网络编程 ·网络编程就是在两个或两个以上的设备(例如计算机)之间传输数据.程序员所作的事情就是把数据发送到指定的位置,或者接收到指定的 ...
- 《Java程序设计》第六周学习总结
20145224 <Java程序设计>第六周学习总结 教材学习内容总结 第十章输入和输出 10.1.1 ·若要将数据从来源中取出,可以使用输入串流:若要将数据写入目的地,可以使用输出串流. ...
- QT-- MainWindow外的cpp文件调用ui
这几天在学习QT,想写一个类似VIM的小软件,刚开始不注重代码结构,全部实现都写在MainWindow文件中,导致MianWindow文件十分的长而且很难去阅读,就想着把函数按照功能分到不同的cpp文 ...
- uva------Help is needed for Dexter(11384)
Problem H Help is needed for Dexter Time Limit: 3 Second Dexter is tired of Dee Dee. So he decided t ...
- Text Justification [LeetCode]
Problem Description:http://oj.leetcode.com/problems/text-justification/ Note: Just be careful about ...