如何在HttpRequestMessage中包含If-None-Match标头 [英] How can I include If-None-Match header in HttpRequestMessage

查看:685
本文介绍了如何在HttpRequestMessage中包含If-None-Match标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个HttpRequestMessage,如下所示:

I have a HttpRequestMessage as follows:

string URI = "http://" + MyHostName.DisplayName.ToString() + "/datastore/";

HttpClient client = new HttpClient();
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, URI);
string Params = "";
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("text/plain"));
request.Headers.AcceptCharset.Add(new StringWithQualityHeaderValue("utf-8", 0.7));
request.Headers.AcceptLanguage.Add(new StringWithQualityHeaderValue("en-us", 0.5));
request.Content = new StreamContent(new MemoryStream(System.Text.Encoding.UTF8.GetBytes(Params)));
request.Content.Headers.Add("Content-Type", "application/x-www-form-urlencoded");

try
{
    var result = await client.SendAsync(request);
    var content = await result.Content.ReadAsStringAsync();
    return content.ToString();
}
catch (HttpRequestException)
{
    Connected = false;
    return "";
}

我想添加 If-None-Match 我的请求的标头,例如'8001'。但当我查找 HttpRequestHeaders.IfNoneMatch 你只能得到它而不是设置它。

I want to add a If-None-Match header to my request with a value like '8001' as an example. But when I lookup HttpRequestHeaders.IfNoneMatch you can only get it and not set it.

任何想法?

编辑1:添加了系统在Windows 10上运行UWP

Edit 1: Added that system is running UWP on Windows 10

推荐答案

解决方案似乎是一个自定义添加的If -None-Match标题

Solution seemed to be a custom addition of a If-None-Match header

供将来参考。有可能服务器不接受引号中的ETag,如8001。

For future reference. It may be possible that a server does not accept an ETag in quotes like "8001".

要解决此问题,您可以使用

To solve this you can use

            request.Headers.TryAddWithoutValidation("If-None-Match", "8001");

这将导致If-None-Match标记为8001

This will result in a If-None-Match tag of 8001

这篇关于如何在HttpRequestMessage中包含If-None-Match标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆