Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在ASP.NET MVC中通过GET请求发送内容类型标头?_C#_.net_Asp.net Mvc_Dotnet Httpclient - Fatal编程技术网

C# 如何在ASP.NET MVC中通过GET请求发送内容类型标头?

C# 如何在ASP.NET MVC中通过GET请求发送内容类型标头?,c#,.net,asp.net-mvc,dotnet-httpclient,C#,.net,Asp.net Mvc,Dotnet Httpclient,我在ASP.NET MVC项目中遇到了一个与HttpClient有关的阻塞问题,它完全拒绝允许内容类型标题。我知道从技术上讲它没有任何意义,但对于我正在调用的API,它是必需的 curl -X GET \ https://api.sample-service.com/v1/items \ -H 'content-type: application/json' \ -H 'secret-key: sk_test_12345' 它们需要这些标题,如果您不使用内容类型标题,它将返回Bad

我在ASP.NET MVC项目中遇到了一个与
HttpClient
有关的阻塞问题,它完全拒绝允许
内容类型
标题。我知道从技术上讲它没有任何意义,但对于我正在调用的API,它是必需的

curl -X GET \
  https://api.sample-service.com/v1/items \
  -H 'content-type: application/json' \
  -H 'secret-key: sk_test_12345'
它们需要这些标题,如果您不使用
内容类型
标题,它将返回
BadRequest
。我无法控制这一切

我在.NETCore2中成功地实现了这一点,但在MVC中它被完全否定了。下面是代码示例:

var client = new HttpClient
{
   BaseAddress = new Uri("https://api.sample-service.com/v1/")
};

client.DefaultRequestHeaders.Add("secret-key", "my-secret-key");

var content = new StreamContent(Stream.Null);
content.Headers.Add("Content-Type", "application/json");

var request = new HttpRequestMessage(HttpMethod.Get, "items")
{
   Content = content
};

var response = await client.SendAsync(request);
以上内容适用于.NETCore,但不适用于MVC。它抛出一个
ProtocolViolationException


在MVC中如何强制在GET请求中包含
内容类型
头?

内容类型在MVC中设置为HttpWebRequest的属性

var httpWebRequest = (HttpWebRequest)WebRequest.Create(URL);   //URL is a string with your url
httpWebRequest.ContentType = "application/json";
这就是我通常在MVC中处理Web请求的方式

string URL = ""; //your url
if (URL.Contains("https"))
            {
                ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
                ServicePointManager.ServerCertificateValidationCallback = (RemoteCertificateValidationCallback)Delegate.Combine(ServicePointManager.ServerCertificateValidationCallback, new RemoteCertificateValidationCallback((object s, X509Certificate ce, X509Chain ch, SslPolicyErrors tls) => true));
            }
CookieContainer cookieJar = new CookieContainer();
var httpWebRequest = (HttpWebRequest)WebRequest.Create(URL);   
string petition = ""; //leave empty for get requests  
string result = ""; //if the server answers it will do so here
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "GET";
httpWebRequest.Headers["Authorization"] = "passkeydlkefjswlkejcvekexample"; //this is how you add custom headers, you can change it to anything
var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream());


                streamWriter.Write(Petition);
                streamWriter.Flush();
                streamWriter.Close();


            var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
            var streamReader = new StreamReader(httpResponse.GetResponseStream());

            result = streamReader.ReadToEnd();

可能是@NightOwl888的复制品,谢谢,但我试过了。它仍然抛出
ProtocolViolationException
,并显示消息“无法发送此动词类型的内容正文”