c# - 必须使用适当的属性或方法来修改 'Accept' header 。参数名称: name

标签 c# .net http-headers webrequest

收到错误消息-必须使用适当的属性或方法修改'Accept' header 。
我在 postman 中得到响应,但没有在MVC Controller 中得到响应
我有一个带有以下headers.s的基本身份验证api,同时在请求中使用添加accept header 得到错误

Content-Type:  application/vnd.onem2m-res+json;ty=4;
Accept: application/vnd.onem2m-res+json;
Cache-Control: no-cache
X-M2M-RI: 9900001
Authorization: Basic QzdBQUNFO
X-M2M-Origin: C7AACE9-25

代码
WebRequest req = WebRequest.Create(@"url");
                req.Method = "GET";
                req.Headers["Authorization"] = "Basic " + "QzdBQUNFO";
                req.ContentType = "application/vnd.onem2m-res+json";
                req.Headers.Add("Accept", "application/vnd.onem2m-res+json;");
                req.Headers["Cache-Control"] = "no-cache";
                req.Headers["X-M2M-RI"] = "9900001";
                req.Headers["X-M2M-Origin"] = "C7AACE9-25";
                HttpWebResponse resp = req.GetResponse() as HttpWebResponse;
                var encoding = resp.CharacterSet == "" ? Encoding.UTF8 : Encoding.GetEncoding(resp.CharacterSet);

最佳答案

在某些情况下,您必须更改标题名称:

//req.Headers.Add("Accept", "application/vnd.onem2m-res+json;");
req.Headers.Add("20", "application/vnd.onem2m-res+json;");

以下是允许的网页标题列表:
CacheControl = 0
Date = 2
Connection = 1
Date = 2
KeepAlive = 3
Pragma = 4,
Trailer = 5
TransferEncoding = 6
 Upgrade = 7
Via = 8
Warning = 9
Allow = 10
ContentLength = 11
ContentType = 12
ContentEncoding = 13
ContentLanguage = 14
ContentLocation = 15
ContentMd5 = 16
 ContentRange = 17
Expires = 18
LastModified = 19
Accept = 20
AcceptCharset = 21
AcceptEncoding = 22
AcceptLanguage = 23
Authorization = 24
Cookie = 25
Expect = 26
From = 27
Host = 28
IfMatch = 29
 IfModifiedSince = 30
IfNoneMatch = 31
 IfRange = 32
IfUnmodifiedSince = 33
MaxForwards = 34
ProxyAuthorization = 35
Referer = 36
 Range = 37
Te = 38
Translate = 39
UserAgent = 40

希望它能对您有所帮助!

关于c# - 必须使用适当的属性或方法来修改 'Accept' header 。参数名称: name,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50423495/

相关文章:

c# - 如何判断 Type 是否是静态类?

http - 什么是 HTTP "Host" header ?

PHP session 添加 header Pragma : no-cache

get - NestJS 从 GridFS 返回一个文件

c# - 在 C# 中监视与互斥锁

c# - 更好地使用 C# 在所有文件中搜索字符串

c# - Retinex算法实现

c# - Json 架构日期验证

c# - 将图像添加到视频末尾

c# - 如何在 C# 中将工作周 double 转换为日期时间