【问题标题】:HttpResponseMessage System.InvalidOperationExceptionHttpResponseMessage System.InvalidOperationException
【发布时间】:2014-09-11 00:25:24
【问题描述】:

我有一个相当简单的问题,我认为它很容易解决。这只是在 Google 上尝试了不同的查询,但没有弹出任何内容,但这就是我在这里的原因。

这是错误: System.InvalidOperationException

这里的这段代码基本上会抛出这个错误

string test = response.Content.Headers.GetValues("Location").FirstOrDefault();

Fiddler 中的位置如下所示:

位置:https://www.redirecturlishere.com/blah

这是整个函数:

private async Task<string> GetRequest()
    {
        //HttpContent postContent = new StringContent(post, Encoding.ASCII, "application/x-www-form-urlencoded");

        using (HttpResponseMessage response = await httpClient.GetAsync(
            "http://www.cant-share-url.com"))
        {
            using (HttpContent content = response.Content)
            {
                string test = response.Content.Headers.GetValues("Location").FirstOrDefault();
            }
        }

        return "";

    }

有关错误的更多详细信息,“附加信息:错误的标头名称。确保请求标头与 HttpRequestMessage 一起使用,响应标头与 HttpResponseMessage 一起使用,内容标头与 HttpContent 对象一起使用。”

我认为没有太多要解释的了,所以我就这么说吧。

【问题讨论】:

  • 您是否在调试器中检查过response.Content.Headers.GetValues("Location") 返回的内容,如果它返回null,您将获得InvalidOperationException
  • @BenRobinson 真的吗?它不会抛出NullReferenceException,但会抛出InvalidOperationException

标签: c# http-headers httpclient invalidoperationexception httpcontent


【解决方案1】:

我已经基本解决了问题,标题没有存储在内容中,我很傻。

对于没有看到解决方案的任何人。 :)

string test = response.Headers.GetValues("Location").FirstOrDefault()

【讨论】:

    猜你喜欢
    • 2016-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-02
    • 2012-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多