0

Quite a simple problem I've got that would be easy to solve I reckon for the brains. It's just a tried a different queries on Google and nothing popped up but hey that's why I am here.

Here is the error: System.InvalidOperationException

Basically this error is thrown on this piece of code here

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

Location in Fiddler looks like this:

Location: https://www.redirecturlishere.com/blah

Here is the entire function:

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 "";

    }

More details on the error, "Additional information: Misused header name. Make sure request headers are used with HttpRequestMessage, response headers with HttpResponseMessage, and content headers with HttpContent objects."

I don't think there is much else to explain so I'll leave it at that.

Aidan
  • 317
  • 1
  • 3
  • 17
  • Have you checked in a debugger what `response.Content.Headers.GetValues("Location")` returns, if it returns null you will get an `InvalidOperationException` – Ben Robinson Sep 10 '14 at 16:25
  • @BenRobinson really? It won't throw `NullReferenceException` but will throw `InvalidOperationException` ? – Michael Sep 10 '14 at 16:32

1 Answers1

0

I have solved the problem basically the header was not stored in the content, silly me.

And for anyone who doesn't see the solution. :)

string test = response.Headers.GetValues("Location").FirstOrDefault()
Aidan
  • 317
  • 1
  • 3
  • 17