StatusCode: 415, ReasonPhrase: '不支持的媒体类型'

Posted

技术标签:

【中文标题】StatusCode: 415, ReasonPhrase: \'不支持的媒体类型\'【英文标题】:StatusCode: 415, ReasonPhrase: 'Unsupported Media Type'StatusCode: 415, ReasonPhrase: '不支持的媒体类型' 【发布时间】:2019-03-01 01:18:51 【问题描述】:

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

提前谢谢你!

【问题讨论】:

这是一个服务器错误 - 对您连接的服务器一无所知,可能无法回答。 您正在分配 json 的 MediaTypeWithQualityHeaderValue,而不是发送 json 数据。还要使用await 而不是.GetAwaiter().GetResult(),另一个死锁正在等待发生... 如果您经常调用RegisterPersonHttpClient request = new HttpClient(); 是用尽所有可用系统套接字的可靠方法。 【参考方案1】:

这是一个老问题,但由于代码示例尚不存在可接受的答案,因此我在此处发布答案。希望这个带有代码示例的答案对像我一样偶然发现这个问题的其他人有所帮助。我在 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";

如果有人要复制/粘贴此代码,请注意,上面 sn-p 中的 _httpClient 对象首先在 IoC 中初始化,然后注入构造函数。你可以随心所欲。由于“如何使用 IoC”不是这个问题的一部分,所以我将详细介绍。

【讨论】:

【参考方案2】:

所以要首先回答这个问题,我敢打赌你需要设置一个“Content-Type”标题,因为它抱怨提供了错误的媒体类型(你设置的是你愿意接受的,而不是你想要的正在发送)。该服务是否还需要 application/x-www-form-urlencoded 编码内容(即您要发送的内容)?如果是这样,将其作为 Content-Type 提供,但现在不太常见。

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

【讨论】:

这是一个 XF 应用程序,类似于标签中的描述。一切都在招摇和邮递员中工作。问题出在我的代码中。

以上是关于StatusCode: 415, ReasonPhrase: '不支持的媒体类型'的主要内容,如果未能解决你的问题,请参考以下文章

从 Delphi 10.3 中的 TWebBrowser OnNavigateError 中提取 StatusCode

.NET 抛出 WebException 而不是设置 StatusCode

为啥设置 WebOperationContext.Current.OutgoingResponse.StatusCode = HttpStatusCode.Redirect 会导致 SecurityE

IIS7在设置Response.StatusCode时覆盖customErrors?

golang 获取statuscode

Grpc.Core.RpcException: 'Status (StatusCode = "Unavailable", Detail = "错误启动 gRPC 调用