Loading

小心 HttpClient 中 FormUrlEncodeContent 的 bug

小心 HttpClient 中 FormUrlEncodeContent 的 bug

Intro

最近发现活动室预约项目里的上传图片有时候会有问题,周末找时间测试了一下,发现小图片的上传没问题,大图片上传会有问题,而且异常信息还很奇怪,System.UriFormatException: Invalid URI: The Uri string is too long 看这个错误的信息还以为是请求的 url 过长导致的,但是实际请求的 url 很短,诡异的异常信息

测试示例

为了方便大家了解和测试这个bug,我在 Github 上提供了一个示例 https://github.com/WeihanLi/SamplesInPractice/blob/master/HttpClientTest/FormUrlEncodeContentTest.cs

HttpClient 示例代码:

public class FormUrlEncodeContentTest
{
    private const string TestUrl = "https://cnblogs.com";

    public static async Task FormUrlEncodedContentLengthTest()
    {
        using (var httpClient = new HttpClient(new NoProxyHttpClientHandler()))
        {
            using (var response = await httpClient.PostAsync(TestUrl, new FormUrlEncodedContent(new Dictionary<string, string>()
            {
                {"bigContent", new string('a', 65535)},
            })))
            {
                Console.WriteLine($"response status code:{response.StatusCode}");
            }
        }
    }

    public static async Task ByteArrayContentLengthTest()
    {
        using (var httpClient = new HttpClient(new NoProxyHttpClientHandler()))
        {
            var postContent = $"bigContent={new string('a', 65535)}";
            using (var response = await httpClient.PostAsync(TestUrl, new ByteArrayContent(postContent.GetBytes())))
            {
                Console.WriteLine($"response status code:{response.StatusCode}");
            }
        }
    }

    public static async Task StringContentLengthTest()
    {
        using (var httpClient = new HttpClient(new NoProxyHttpClientHandler()))
        {
            var postContent = $"bigContent={new string('a', 65535)}";
            using (var response = await httpClient.PostAsync(TestUrl, new StringContent(postContent)))
            {
                Console.WriteLine($"response status code:{response.StatusCode}");
            }
        }
    }
}

测试代码:

InvokeHelper.OnInvokeException = Console.WriteLine;

await InvokeHelper.TryInvokeAsync(FormUrlEncodeContentTest.FormUrlEncodedContentLengthTest);
Console.WriteLine();
await InvokeHelper.TryInvokeAsync(FormUrlEncodeContentTest.StringContentLengthTest);
Console.WriteLine();
await InvokeHelper.TryInvokeAsync(FormUrlEncodeContentTest.ByteArrayContentLengthTest);

Console.WriteLine("Completed!");

输出结果如下:

揪出异常始末

上传图片的时候会调用一个码云的一个 POST 接口来保存上传的图片,参数是通过 form-data 的方式传递的,在 POST 的时候报异常了,异常信息很诡异,具体信息和上面的是一样的:

这个异常信息看上去像是 url 过长导致的,但是实际的 url 很短只有几百,而且从调用的堆栈上来看是 FormUrlEncodedContent 的 bug,然后根据异常堆栈信息去看了一下源码,部分源码如下:

首先看 FormUrlEncodedContent 做了什么:

然后再找上一层堆栈信息,Uri是一个分部类(partial),你如果直接在 Github 上 Find 的话会找到多个 Uri 相关的文件,最后在 UriExt 中找到了上面的 EscapeDataString 方法:

最后来看最上层的堆栈信息 UriHelper.EsacpeString 方法,找到异常抛出的地方

在 Uri 这个类中可以找到上面定义的 c_MaxUriBufferSize,它的值是 0xFFF0 转成十进制就是 65520

找到问题所在之后,就可以避免这个问题了,再遇到这个问题也就知道是怎么回事了,上面的问题就是 post 的数据太大了,超过了这个限制,所以引发的异常

More

既然知道这个是 FormUrlEncodedContent 的 bug,那么修复它就可以通过避免使用它,可以直接使用 ByteArray Content,或者不需要 Encode 处理直接用 StringContent 也是可以的

后来在 Github 搜 issue 的时候发现也有很多人遇到了这个问题,这个问题会在 net5 中得到修复,详见 PR https://github.com/dotnet/corefx/pull/41686

文中一些源码的链接在文章最后的 Reference 的部分可以找到

Reference

posted @ 2020-06-01 18:48  WeihanLi  阅读(1999)  评论(4编辑  收藏  举报