C# 无法获取传输模式为流传输的HTTP标头

C# 无法获取传输模式为流传输的HTTP标头,c#,wcf,iis-7.5,multipartform-data,C#,Wcf,Iis 7.5,Multipartform Data,我正在使用IIS 7.5和.NET 4.5开发一个WCF REST Web服务 接口文件具有以下声明: [OperationContract(Action = "PutDataFile", IsOneWay = true)] [WebInvoke(Method = "POST", UriTemplate = "file")] void PutDataFile(Stream fileContents); 代码如下: public void PutDataFile(Stream fileConte

我正在使用IIS 7.5和.NET 4.5开发一个WCF REST Web服务

接口文件具有以下声明:

[OperationContract(Action = "PutDataFile", IsOneWay = true)]
[WebInvoke(Method = "POST", UriTemplate = "file")]
void PutDataFile(Stream fileContents);
代码如下:

public void PutDataFile(Stream fileContents)
当我从客户端执行多部分POST时,流文件内容如下:

------------------------------b64bd2de2e40
Content-Disposition: form-data; name="UploadTest.txt"; filename="UploadTest.txt"
Content-Type: application/octet-stream

...

------------------------------b64bd2de2e40--
但是,如果在服务器端执行以下操作:

String contentType1 = WebOperationContext.Current.IncomingRequest.ContentType;
String contentType2 = WebOperationContext.Current.IncomingRequest.Headers[HttpResponseHeader.ContentType];
WebHeaderCollection headers = WebOperationContext.Current.IncomingRequest.Headers;

所有这些变量都是空的。我查看了传输中使用WireShark和HTTP请求头的流量,特别是内容类型头。为什么那些头变量是空的?提前谢谢

可能是因为这是单向操作。你能试着用这个来读标题吗?var contentType=HttpContext.Current.Request.contentType;var headers=HttpContext.Current.Request.headers;确保您的web.config.com中启用了aspnetCompatibility模式,该模式可以正常工作。谢谢这就是我最后使用的:String contentType1=System.Web.HttpContext.Current.Request.ContentType;字符串contentType2=System.Web.HttpContext.Current.Request.Headers[“Content Type”];System.Collections.Specialized.NameValueCollection headers=System.Web.HttpContext.Current.Request.headers;