C# HTTPWebRequest获取错误远程服务器返回错误:(415)不支持的媒体类型

C# HTTPWebRequest获取错误远程服务器返回错误:(415)不支持的媒体类型,c#,json,rest,get,httpwebrequest,C#,Json,Rest,Get,Httpwebrequest,这段代码应该给我JsonString作为响应,但我得到了一个错误 System.dll中发生类型为“System.Net.WebException”的未处理异常 其他信息:远程服务器返回错误:(415)不支持的媒体类型 string Url = "https://abc.test.xyz.com/123456344589753/transactions"; HttpWebRequest request = (HttpWebRequest)WebRequest.Creat

这段代码应该给我JsonString作为响应,但我得到了一个错误

System.dll中发生类型为“System.Net.WebException”的未处理异常

其他信息:远程服务器返回错误:(415)不支持的媒体类型

string  Url = "https://abc.test.xyz.com/123456344589753/transactions";
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
            string certThumbPrint = "ce03c469fb6c6dewwwww21886b7c1d405d954hshgd";
            X509Certificate2 cert = FindCertificateByThumbprint(certThumbPrint);
            if (cert != null)
            {
                request.ClientCertificates.Add(cert);
            }
            request.Method = "GET";
            request.MediaType = "application/json";
            request.ContentType = "application/json; charset=utf-8";
            string text;
            var response = (HttpWebResponse)request.GetResponse();
            using (var sr = new StreamReader(response.GetResponseStream()))
            {
                text = sr.ReadToEnd();
            }
有关解决此错误的任何建议,请参见:

415不支持的媒体类型

415(不支持的媒体类型)状态代码表示 源服务器拒绝为请求提供服务,因为负载 此方法的目标资源不支持的格式。 格式问题可能是由于请求的指定内容造成的- 类型或内容编码,或作为检查数据的结果 直接的


GET请求没有有效负载,因此与您通信的服务器可能不喜欢您在请求中指定内容类型。

正是如此。解决方案是设置
Accept
属性:
request.Accept=“application/json”