使用 Flurl 发出 PUT 请求时出现 NullReferenceException

NullReferenceException when make a PUT request using Flurl

我正在构造一个 Url 并使用 Flurl 发出一个 PUT 请求,如下所示:

using (var client = new Url("myurl"))
    .ConfigureClient(c => c.HttpClientFactory = new CompressHttpClientFactory())
    .WithCookie(new System.Net.Cookie("name", "value", "/", "domain"))
{
    var content = new StringContent("json here", Encoding.UTF8, "application/json");
    var response = await client.PutAsync(content)
}

但是我在调​​用 PutAsync:

时遇到异常

System.NullReferenceException occurred HResult=0x80004003 Message=Object reference not set to an instance of an object. Source=Flurl.Http StackTrace: at Flurl.Http.FlurlClient.ReadResponseCookies(HttpResponseMessage response) at Flurl.Http.FlurlClient.d__28.MoveNext() at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()

我应该使用 try / catch 并忽略异常吗?

我怀疑实际的 HTTP 调用未能获得响应,并且 Flurl 无论如何都在尝试读取 cookie,而不对响应进行空检查。这是known bug and is fixed/ready for the 2.0 release. 2.0 is in prerelease right now and I recommend upgrading. It's stable for production use, I'm just not guaranteeing quite yet that the changes一成不变的