接受'必须使用适当的属性或方法修改标头。参数名称:名称

时间:2018-05-19 08:52:23

标签: c# .net http-headers webrequest

收到错误消息 - “接受”'必须使用适当的属性或方法修改标头。 我在帖子中得到了回复但没有进入MVC控制器  我有一个基本的身份验证api与下面的标题。同时使用在请求中添加接受标头获取错误

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

3 个答案:

答案 0 :(得分:2)

WebRequest req = WebRequest.Create(@"url");
            req.Method = "GET";
            req.Headers["Authorization"] = "Basic " + "QzdBQUNFO";
            req.ContentType = "application/vnd.onem2m-res+json";
            req.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);

microsoft docs

答案 1 :(得分:0)

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

//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

希望它可以为您提供帮助!

答案 2 :(得分:0)

使用System.Net.HttpWebRequest对象,而不是使用System.Net.WebRequest对象。

WebRequest对象不支持Accept属性,但是HttpWebRequest对象支持。示例代码:

// Create a 'HttpWebRequest' object which support Accept property
HttpWebRequest req=(HttpWebRequest)WebRequest.Create(@"url");
                req.Method = "GET";
                req.Headers["Authorization"] = "Basic " + "QzdBQUNFO";
                req.ContentType = "application/vnd.onem2m-res+json";
                req.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";

Microsoft文档: https://docs.microsoft.com/en-us/dotnet/api/system.net.httpwebrequest.accept?view=netframework-4.6