System.Web.HttpRequestBase转HttpWebRequest
/// <summary>
/// Copies all headers and content (except the URL) from an incoming to an outgoing
/// request.
/// </summary>
/// <param name="source">The request to copy from</param>
/// <param name="destination">The request to copy to</param>
public static void CopyTo(this System.Web.HttpRequestBase source, HttpWebRequest destination)
{
Contract.Requires(source != null && destination != null); //注意:HttpWebRequire.Method默认为Get,
//在写入请求前必须把HttpWebRequire.Method设置为Post,
//否则在使用BeginGetRequireStream获取请求数据流的时候,系统就会发出“无法发送具有此谓词类型的内容正文”的异常。
destination.Method = source.HttpMethod; // Copy unrestricted headers (including cookies, if any)
foreach (var headerKey in source.Headers.AllKeys)
{
switch (headerKey)
{
case "Connection":
case "Content-Length":
case "Date":
case "Expect":
case "Host":
case "If-Modified-Since":
case "Range":
case "Transfer-Encoding":
case "Proxy-Connection":
// Let IIS handle these
break; case "Accept":
case "Content-Type":
case "Referer":
case "User-Agent":
// Restricted - copied below
break; default:
destination.Headers[headerKey] = source.Headers[headerKey];
break;
}
} // Copy restricted headers
if (!source.AcceptTypes.IsNullOrEmpty())
{
destination.Accept = string.Join(",", source.AcceptTypes);
}
destination.ContentType = source.ContentType;
if (source.UrlReferrer != null)
{
destination.Referer = source.UrlReferrer.AbsoluteUri;
}
destination.UserAgent = source.UserAgent;
destination.ContentLength = source.ContentLength;
destination.ContentType = source.ContentType;
destination.KeepAlive = source.Headers["Connection"] != "close";
DateTime ifModifiedSince;
if (DateTime.TryParse(source.Headers["If-Modified-Since"], out ifModifiedSince))
{
destination.IfModifiedSince = ifModifiedSince;
}
string transferEncoding = source.Headers["Transfer-Encoding"];
if (transferEncoding != null)
{
destination.SendChunked = true;
destination.TransferEncoding = transferEncoding;
} // Copy content (if content body is allowed)
if (source.HttpMethod != WebRequestMethods.Http.Get && source.HttpMethod != WebRequestMethods.Http.Head && source.ContentLength > )
{
var destinationStream = destination.GetRequestStream();
source.InputStream.FixedCopyTo(destinationStream, source.ContentLength);
destinationStream.Close();
}
}
System.Web.HttpRequestBase转HttpWebRequest的更多相关文章
- VB.NET中网络编程的另一种方案----system.net中的HttpWebRequest类的使用
VB.NET中网络编程的另一种方案---- system.net中的HttpWebRequest类的使用 在VB.net中进行网络编程,除了我之前写的随笔中的使用WinHttp组件进行编程,还有另一种 ...
- System.Web.Mvc.Controller.cs
ylbtech-System.Web.Mvc.Controller.cs 1.程序集 System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicK ...
- 错误 1 类型“System.Web.Mvc.ModelClientValidationRule”同时存在于“c:\Progra
问题如图: 解决办法: step1: 首先关闭你应用程序方案,在你保存项目的文件夹下找到ProjectName.csproj ProjectName是你实际的应用程序名称. step2: 用文字编辑 ...
- 未能加载文件或程序集“System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35”或它的某一个依赖项。系统找不到指定的文件
ASP.NET 运行时错误:针对类型System.Web.Mvc.PreApplicationStartCode的应用程序邓启动初始化方法Start 引发了异常,显示下列错误消息: 未能加载文件或程序 ...
- Could not load type 'System.Web.Mvc.ViewPage<dynamic>' in asp.net mvc2 after publishing the website
在WebConfig里 找到 <pages></pages> <pages pageParserFilterType="System.Web.Mvc.ViewT ...
- System.Web.Http.Cors配置跨域访问的两种方式
System.Web.Http.Cors配置跨域访问的两种方式 使用System.Web.Http.Cors配置跨域访问,众多大神已经发布了很多文章,我就不在详细描述了,作为小白我只说一下自己的使用心 ...
- System.Web.HttpRequestValidationException: A potentially dangerous Request.F
ASP.NET .0验证请求 System.Web.HttpRequestValidationException: A potentially dangerous Request.F System.W ...
- MVC-命名空间“System.Web.Mvc”中不存在类型或命名空间名称“Html”(是否缺少程序集引用?)
如上截图,明明引用了“System.web.mvc”,可是还出这样的错误. 解决方法: 1.右键引用的“System.Web.Mvc” 2.<复制本地>一样选择<True> 3 ...
- 如何解决System.Web.HttpRequestValidationException的异常
在.net framework 4.0版本以下, 只需要在web.config中进行如下配置: <configuration> <system.web> & ...
随机推荐
- phalcon: 表单
以实例为说明: controller <?php use \Phalcon\Forms\Form; use \Phalcon\Forms\Element\Text; use \Phalcon\F ...
- mismatch位置(MD tag)- sam/bam格式解读进阶
这算是第二讲了,前面一讲是:Edit Distance编辑距离(NM tag)- sam/bam格式解读进阶 MD是mismatch位置的字符串的表示形式,貌似在call SNP和indel的时候会用 ...
- Linux 打包和压缩 方法详解
一般基因组的数据都非常大,所以都会 打包 压缩 后进行传输,拿到数据后的第一步必然就是要 解包 和 解压缩. 基本常识 首先要弄清两个概念:打包 和 压缩. 打包 是指将一大堆文件或目录变成一个总的文 ...
- 基于TBDS的flume异常问题排查过程
版权声明:本文由王亮原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/214 来源:腾云阁 https://www.qclou ...
- 图片左右滚动的js代码
html代码 <div class="demo" id="demo" style="overflow:hidden; width:660px; ...
- vb 和vb.net的区别
vb 和vb.net的区别 源地址:http://blog.csdn.net/xjc1278003262/article/details/8805324 在随着VB.NET的 发展,大部分人都放弃使用 ...
- IE6 IE7 hasLayout bug之li间的3px垂直间距
1. li中仅包含a,span等内联(行内)元素2.触发条件: li元素的layout被触发(通常为设置了宽或高,设置overflow:hidden在IE7下同样触发layout),且a或span元素 ...
- (13)odoo翻译
-------------------更新时间:15:52 2016-09-28 星期三 增加模型名翻译17:26 2016-05-20 星期五17:58 2016-05-17 星期二12:14 20 ...
- GetMemory
在函数中动态申请内存(虚拟内存,堆),利用指针返回值指向申请的内存.
- 如何在linux系统下面编译C++(写给小白)(-1)
首先 , 对于redhat,openSuse来说 ,C/C++的编译器已经集成了 大多数应该使用的是Ubuntu ,Ubuntu只有gcc(一个编译C语言的编译器), 因此还需要使用命令apt-get ...