我花了3天的时间阅读了所有与这里和其他网站相关的文章,关于这个错误,没有成功!现在我需要帮助。
错误:
{StatusCode: 415, ReasonPhrase: 'Unsupported Media Type', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:{
...server's informations...
Strict-Transport-Security: max-age=2592000
X-Android-Received-Millis: 1551400026958
X-Android-Response-Source: NETWORK 415X-
Android-Selected-Protocol: http/1.1
X-Android-Sent-Millis: 1551400026857
X-Powered-By: ASP.NETContent-Length: 0}}
方法:问题出现在: request.PostAsync(URL,param).GetAwaiter().GetResult();
public static string RegisterPerson(Person p)
{
string msg = "";
string URL = URLbase + "person/register"; //--URL RIGHT, TESTING IN POSTMAN, INSERT DATA NORMALLY
FormUrlEncodedContent param = new FormUrlEncodedContent(new[] {
new KeyValuePair<string, string>("Name", p.Name),
new KeyValuePair<string, string>("Phone", p.Fone),
new KeyValuePair<string, string>("Birth", p.Birth),
});
HttpClient request = new HttpClient();
request.DefaultRequestHeaders.Accept.Clear();
request.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = request.PostAsync(URL, param).GetAwaiter().GetResult(); // <===ERROR HERE
switch (response.StatusCode)
{
case HttpStatusCode.OK:
msg= "SUCCESS";
break;
....
提前谢谢你!
发布于 2021-02-22 22:30:02
这是一个老问题,但是由于在代码示例中还没有被接受的答案,所以我在这里发布了一个答案。希望这个有代码示例的答案能帮助其他人,像我一样,在这个问题上绊倒了。我在应用程序/json、FormUrlEncodedContent、Content等之间挣扎了几分钟,然后终于开始工作了。在下面..。
选项1,使用PostAsync..。
var url = "https://....your url goes here...";
var param = new Dictionary<string, string>
{
{ "key_one", "value_1" },
{ "key_two", "value_2" }
// ... and so on
};
var content = new FormUrlEncodedContent(param);
var response = _httpClient.PostAsync(url, content);
var result = response.Result.Content.ReadAsStringAsync().Result;
if (!response.Result.IsSuccessStatusCode)
{
throw new Exception(result);
}
return "process result object into anything you need and then return it";
选项2,使用SendAsync..。
var url = "https://....your url goes here...";
var request = new HttpRequestMessage(HttpMethod.Post, new Uri(url));
var param = new Dictionary<string, string>
{
{ "key_one", "value_1" },
{ "key_two", "value_2" }
// ... and so on
};
var content = new FormUrlEncodedContent(param);
request.Content = content;
var response = _httpClient.SendAsync(request);
var result = response.Result.Content.ReadAsStringAsync().Result;
if (!response.Result.IsSuccessStatusCode)
{
throw new Exception(result);
}
return "process result object into anything you need and then return it";
如果有人要复制/粘贴这段代码,请注意,上面代码段中的_httpClient对象首先在IoC中初始化,然后注入构造函数。不管你需要什么,你都可以做。因为“如何使用IoC”不是这个问题的一部分,所以我将详细讨论这个问题。
发布于 2019-03-01 02:03:30
因此,首先回答这个问题,我敢打赌你需要设置一个“内容类型”标题,因为它是在抱怨提供了错误的媒体类型(你正在设置你愿意接受的内容,而不是你发送的内容)。此外,该服务是否期望应用程序/x-www-表单-urlencoded编码的内容(这就是您要发送的内容)?如果是这样的话,就把它作为内容类型,但是现在这样做已经不那么平常了。
这是经典的WebApi还是.net核心?如果是后者,请将您的方法更改为非静态的,注入IHttpClientFactory并使用它来构造您的客户端。好处是,您可以创建适合您在injection (Startup.cs)上的需要的客户端,并重复使用它们,并且在规模上避免套接字问题(请注意,这需要大量的负载,所以如果这不是您想要的,请不要担心)。然而,它确实使控制器代码变得更干净,所以从代码可读性的角度来看,这可能是值得的,而对我来说,这就是我通常所优化的。
https://stackoverflow.com/questions/54936636
复制相似问题