期望:100-Continue - 如何编写模块和测试客户端

时间:2012-01-18 22:24:04

标签: c# asp.net .net asp.net-mvc

我正在尝试测试在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读取输出。据我所知,这仍然显示请求的完整有效负载。

现在我有几个问题。

  1. 服务器代码实际上是先返回500,还是在我收到BegineRequest事件之前已经返回了100?
  2. 有更好的方法来编写客户端吗?我不明白为什么在发出请求之前将完整的有效负载写入请求流。在调用客户端Request.GetResponse()方法之前,服务器模块不会获取BeginRequest事件。
  3. 有没有一种方法可以测试localhost上的实际流量?
  4. [更新]

    我找不到在本地测试的方法,所以我还使用了邻居员工桌面进行测试。我能够用这种方式用WireShark进行测试。

    据我所知,即使IHttpModule在文件完全上传之前立即返回错误,也无法让HttpWebClient向IIS发送PUT请求而不发送完整文件。

    我不知道问题是在客户端(HttpWebClient)还是服务器(IIS)中。我不知道是否使用原始套接字并手动实现HTTP协议会产生影响。

    如果有人对此有任何了解,请告诉我。

0 个答案:

没有答案
相关问题