[英]HttpClient {StatusCode: 415, ReasonPhrase: 'Unsupported Media Type'
[英]StatusCode: 415, ReasonPhrase: 'Unsupported Media Type'
我花了 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;
....
先感謝您!
所以首先回答這個問題,我敢打賭你需要設置一個“Content-Type”標題,因為它抱怨提供了錯誤的媒體類型(你正在設置你願意接受的內容,但不是你發送的內容) . 該服務是否還需要 application/x-www-form-urlencoded 編碼的內容(這就是您發送的內容)? 如果是這樣,請將其作為 Content-Type 提供,但現在這樣做並不常見。
這是經典的 WebApi 還是 .net Core? 我問好像是后者,將您的方法更改為非靜態,注入 IHttpClientFactory 並使用它來構建您的客戶端。 好處是您可以在注入時創建適合您需求的客戶端 (Startup.cs) 並重新使用它們,還可以避免大規模的套接字問題(請注意,這需要大量負載,所以如果這不是您想要的別擔心)。 然而,它確實使控制器代碼更清晰,所以從代碼可讀性的角度來看,這可能是值得的,對我來說,這通常是我優化的。
這是一個老問題,但由於代碼示例中尚不存在可接受的答案,因此我將在此處發布答案。 希望這個帶有代碼示例的答案對像我一樣偶然發現這個問題的其他人有所幫助。 我在 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”不是這個問題的一部分,我將詳細介紹。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.