10

Using Microsoft Message Analyzer, I can see that post data using the HttpClient is being sent in two tcp packets. One for the header, then one for the post data. This data could easily fit into one packet, however it is being split into two. I have explicitly turned on nagling and expect 100 continue off using the ServicePointManager, though, it doesn't seem to help.

        ServicePointManager.Expect100Continue = false;
        ServicePointManager.UseNagleAlgorithm = true;

Microsoft Message Analyzer 5023 (.Net) shows 2 packets are sent to destination, 8170 (Postman) shows 1 packet being sent. Tests were done with the same payload.

Below is some sample code used to generate the request in .net

    public void TestRequest()
    {
        var uri = new Uri("http://www.webscantest.com/");
        ServicePointManager.Expect100Continue = false;
        ServicePointManager.UseNagleAlgorithm = true;
        var p = ServicePointManager.FindServicePoint(uri);
        p.Expect100Continue = false;
        p.UseNagleAlgorithm = true;
        HttpClient client = new HttpClient();
        client.DefaultRequestHeaders.Add("Connection", "close");

        var values = new Dictionary<string, string>
        {
            { "thing1", "hello" },
            { "thing2", "world" }
        };

        var content = new FormUrlEncodedContent(values);

        var response = client.PostAsync("http://www.webscantest.com/", content, CancellationToken.None).Result;
    }

Is there a way to force the payload into a single packet?

Using .Net Framework 4.7

related question here

4

1 回答 1

2

所以看了dotnet core源码后(其他.net版本只能假设相同),在WinHttpHandler中可以看到Request HeaderRequest Body是在不同的点发送的。

请求标头与Interop.WinHttp.WinHttpSendRequest一起发送。然后根据 WinHttp 文档,带有Interop.WinHttp.WinHttpWriteData的请求正文将“等到 WinHttpSendRequest 完成后再调用此函数”

我认为这个问题可以解决,如果请求正文是使用 WinHttpSendRequest 发送的,它当前将正文设置为IntPtr.Zero

此处请求标头

在此处请求正文

于 2018-04-12T05:02:44.460 回答