【问题标题】:NullReferenceException when make a PUT request using Flurl使用 Flurl 发出 PUT 请求时出现 NullReferenceException
【发布时间】:2018-03-10 02:27:42
【问题描述】:

我正在构建一个 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 H结果=0x80004003 Message=对象引用未设置为对象的实例。 来源=Flurl.Http 堆栈跟踪: 在 Flurl.Http.FlurlClient.ReadResponseCookies(HttpResponseMessage 响应) 在 Flurl.Http.FlurlClient.d__28.MoveNext() 在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务) 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务) 在 System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()

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

【问题讨论】:

    标签: c# http flurl


    【解决方案1】:

    我怀疑实际的 HTTP 调用未能获得响应,并且 Flurl 无论如何都试图读取 cookie 而不对响应进行空值检查。这是一个known bug,已修复/准备好用于 2.0 版本。 2.0 现在在prerelease 中,我建议升级。它对于生产使用很稳定,我只是还不能保证changes 是一成不变的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-23
      相关资源
      最近更新 更多