如何在HttpRequestMessage中包含If

编程入门 行业动态 更新时间:2024-10-19 10:26:45
本文介绍了如何在HttpRequestMessage中包含If-None-Match标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个HttpRequestMessage,如下所示:

I have a HttpRequestMessage as follows:

string URI = "" + 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

本文发布于:2023-11-16 16:31:14,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1605964.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何在   HttpRequestMessage

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!