StatusCode:415,ReasonPhrase:“不受支持的媒体类型”

时间:2019-03-01 01:18:52

标签: c# web-services xamarin xamarin.forms httpclient

我花了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;
....

提前谢谢!

2 个答案:

答案 0 :(得分:0)

因此,首先回答这个问题,我敢打赌,您需要设置一个“ Content-Type”标头,因为它抱怨提供了错误的媒体类型(您正在设置要接受的内容,而不是要设置的内容)正在发送)。该服务还需要应用程序/ x-www-form-urlencoded编码的内容(即您要发送的内容)吗?如果是这样,则将其提供为Content-Type,但如今却越来越少。

这是经典的WebApi还是.net Core?我问好像是后者,将您的方法更改为非静态的,注入IHttpClientFactory并使用它来构造您的客户端。好处是您可以创建适合您的注入需求的客户端(Startup.cs),然后重新使用它们,并避免大规模的套接字问题(请注意,这需要大量的负载,因此如果这不是您的本意)不用担心)。但是,它确实可以使控制器代码更简洁,因此从代码可读性的角度来看,这也许是值得的,而对我而言,这通常是我优化的目标。

答案 1 :(得分:0)

这是一个老问题,但由于代码示例中还没有公认的答案,我在这里发布了一个答案。希望这个带有代码示例的答案对像我一样偶然发现这个问题的其他人有所帮助。我在 application/json、FormUrlEncodedContent、Content-Type 标头等之间挣扎了几分钟,然后终于让它工作了。如下...

选项 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”不是这个问题的一部分,我将详细介绍。

相关问题