如何在ASP.NET MVC中发送带有GET请求的Content-Type标头? [英] How can I send Content-Type header with a GET request in ASP.NET MVC?
问题描述
在ASP.NET MVC项目中, HttpClient
遇到阻塞问题,该项目完全拒绝允许 Content-Type
标头。我知道从技术上讲它没有任何意义,但是对于我正在调用的API,这是必需的。
I'm having a blocking issue with HttpClient
in an ASP.NET MVC project where it completely refuses to allow the Content-Type
header. I know that technically it has no meaning, but for the API I'm calling, it's required.
curl -X GET \
https://api.sample-service.com/v1/items \
-H 'content-type: application/json' \
-H 'secret-key: sk_test_12345'
它们需要这些标头,如果您保留 Content-type
标头中,它将返回 BadRequest
。我对此无能为力。
They require these headers and if you leave the Content-type
header out, it will return a BadRequest
. I do not have control over this.
我设法使它在.NET Core 2中工作,但在MVC中完全否认了这一点。以下是代码示例:
I managed to get this working in .NET Core 2, but it's completely denied in MVC. Here's a sample of the code:
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);
以上内容在.Net Core中有效,但在MVC中无效。它将引发 ProtocolViolationException
。
The above works in .Net Core but not MVC. It throws a ProtocolViolationException
.
我该如何在MVC中将 Content-Type
标头强制包含在GET请求中?
What can I do in MVC to force-include the Content-Type
header in GET requests?
推荐答案
内容类型在MVC中设置为HttpWebRequest的属性
content-type is set as an attribute of HttpWebRequest in MVC
var httpWebRequest = (HttpWebRequest)WebRequest.Create(URL); //URL is a string with your url
httpWebRequest.ContentType = "application/json";
这是我通常在MVC中执行Web请求的方式
This is how I usually do Web requests in MVC
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();
这篇关于如何在ASP.NET MVC中发送带有GET请求的Content-Type标头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!