【发布时间】: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