我正在尝试测试在IHttpModule中处理“expect:100-continue”请求的行为。
我想要做的是在客户端使用标头expect: 100-continue
创建请求并将其发送到服务器。服务器将立即返回500.理论上,如果服务器返回500而不是100,则永远不应发送有效负载(文件)。
我没有看到预期的行为。这就是我正在做的......
这是服务器代码(http模块):
using System;
using System.Web;
namespace WebSite
{
public class Expect100ContinueModule : IHttpModule
{
private HttpApplication httpApplication;
public void Init(HttpApplication context)
{
httpApplication = context;
context.BeginRequest += ContextBeginRequest;
}
private void ContextBeginRequest(object sender, EventArgs e)
{
var request = httpApplication.Context.Request;
var response = httpApplication.Context.Response;
if(!request.Url.AbsolutePath.StartsWith("/Upload"))
{
return;
}
response.StatusCode = 500;
response.End();
}
public void Dispose()
{
}
}
}
我在IIS 7中使用集成管道运行它。
以下是客户端代码:
using System.IO;
using System.Net;
namespace ConsoleClient
{
class Program
{
static void Main(string[] args)
{
var request = (HttpWebRequest)WebRequest.Create("http://localhost:83/Upload/");
request.ServicePoint.Expect100Continue = true;
request.Method = "POST";
request.ContentType = "application/octet-stream";
var buffer = File.ReadAllBytes("Test - Copy.txt");
var text = File.ReadAllText("Test - Copy.txt");
using (var requestStream = request.GetRequestStream())
{
requestStream.Write(buffer, 0, buffer.Length);
requestStream.Flush();
}
var response = (HttpWebResponse)request.GetResponse();
var x = "";
}
}
}
从我所看到的情况来看,即使返回500,请求也会包含文件内容。
我遇到的一个问题是,Fiddler自动处理expect:100-continue缓冲请求,返回100,然后继续完整请求。
然后我尝试了WireShark。为了实现这一点,我必须使用RawCap
捕获流量并使用WireShark读取输出。据我所知,这仍然显示请求的完整有效负载。
现在我有几个问题。
[更新]
我找不到在本地测试的方法,所以我还使用了邻居员工桌面进行测试。我能够用这种方式用WireShark进行测试。
据我所知,即使IHttpModule在文件完全上传之前立即返回错误,也无法让HttpWebClient向IIS发送PUT请求而不发送完整文件。
我不知道问题是在客户端(HttpWebClient)还是服务器(IIS)中。我不知道是否使用原始套接字并手动实现HTTP协议会产生影响。
如果有人对此有任何了解,请告诉我。