/// <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的更多相关文章

  1. VB.NET中网络编程的另一种方案----system.net中的HttpWebRequest类的使用

    VB.NET中网络编程的另一种方案---- system.net中的HttpWebRequest类的使用 在VB.net中进行网络编程,除了我之前写的随笔中的使用WinHttp组件进行编程,还有另一种 ...

  2. System.Web.Mvc.Controller.cs

    ylbtech-System.Web.Mvc.Controller.cs 1.程序集 System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicK ...

  3. 错误 1 类型“System.Web.Mvc.ModelClientValidationRule”同时存在于“c:\Progra

    问题如图: 解决办法: step1: 首先关闭你应用程序方案,在你保存项目的文件夹下找到ProjectName.csproj  ProjectName是你实际的应用程序名称. step2: 用文字编辑 ...

  4. 未能加载文件或程序集“System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35”或它的某一个依赖项。系统找不到指定的文件

    ASP.NET 运行时错误:针对类型System.Web.Mvc.PreApplicationStartCode的应用程序邓启动初始化方法Start 引发了异常,显示下列错误消息: 未能加载文件或程序 ...

  5. 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 ...

  6. System.Web.Http.Cors配置跨域访问的两种方式

    System.Web.Http.Cors配置跨域访问的两种方式 使用System.Web.Http.Cors配置跨域访问,众多大神已经发布了很多文章,我就不在详细描述了,作为小白我只说一下自己的使用心 ...

  7. System.Web.HttpRequestValidationException: A potentially dangerous Request.F

    ASP.NET .0验证请求 System.Web.HttpRequestValidationException: A potentially dangerous Request.F System.W ...

  8. MVC-命名空间“System.Web.Mvc”中不存在类型或命名空间名称“Html”(是否缺少程序集引用?)

    如上截图,明明引用了“System.web.mvc”,可是还出这样的错误. 解决方法: 1.右键引用的“System.Web.Mvc” 2.<复制本地>一样选择<True> 3 ...

  9. 如何解决System.Web.HttpRequestValidationException的异常

    在.net framework 4.0版本以下, 只需要在web.config中进行如下配置: <configuration>    <system.web>        & ...

随机推荐

  1. maven和jdk版本不匹配

    解决方法:http://blog.csdn.net/mafan121/article/details/51944346

  2. 编译maxscale

    编译maxscale,需要依赖mariadb版本的MySQL.有自己的版本就是任性啊

  3. CentOS报错:Could not retrieve mirrorlist http://mirrorlist.centos.org/?release=7&arch=x86_64&repo=os&infra=stock32 error was 14: curl#6 - "Could not resolve host: mirrorlist.centos.org; Unknown error"

    今天安装完带图形界面的CentOS 7后,在Terminal中运行yum安装命令时报了以下错误: Could not retrieve mirrorlist http://mirrorlist.cen ...

  4. Qt之QRoundProgressBar(圆形进度条)

    简述 QRoundProgressBar类能够实现一个圆形进度条,继承自QWidget,并且有和QProgressBar类似的API接口. 简述 详细说明 风格 颜色 字体 共有函数 共有槽函数 详细 ...

  5. 更准确的mysql全文索引

    MYSQL自带的全文索引在查找数据的时候,有非常多的限制,如字符少于3个不能搜索,常用字不能搜索 但mysql 的like进行查询的时候又非常的慢 但你需要用到比较准确的查询的时候,要么不用mysql ...

  6. Same Tree [LeetCode]

    Problem Description: http://oj.leetcode.com/problems/same-tree/ class Solution { public: bool isSame ...

  7. Rudolph javascript 监听简单对象属性的变化 -- 回调函数的应用

    http://www.oschina.net/code/snippet_1590754_46481 //简单对象的属性的变化监控 //通过setAttr改变属性的值 var o = { 'a':2, ...

  8. 对石家庄铁道大学网站的UI分析

    学校网站的首页面的色调用的比较好看,用深蓝色体现了严谨治学的风范.然后就是网站的首页有 1,学校概况:学校简介 现任领导 历任领导 校史沿革 2,组织机构: 机构设置 院系设置 管理机构 直属单位 其 ...

  9. Java开发 Eclipse使用技巧(转)

    1.如何设置默认的代码目录为src,默认的输出目录为bin? window->Preferences->java->Build Path中,右侧选择Folders就可以 2.如何为快 ...

  10. SQL语句技巧(上个样式太差了)

      以下并非本人整理,但是看后感觉相当不错,特此分享. 1.应用程序中,保证在实现功能的基础上,尽量减少对数据库的访问次数:通过搜索参数,尽量减少对表的访问行数,最小化结果集,从而减轻网络负担:能够分 ...