Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 状态代码:415,原因短语:';不支持的媒体类型';_C#_Web Services_Xamarin_Xamarin.forms_Httpclient - Fatal编程技术网

C# 状态代码:415,原因短语:';不支持的媒体类型';

C# 状态代码:415,原因短语:';不支持的媒体类型';,c#,web-services,xamarin,xamarin.forms,httpclient,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-

我花了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()

publicstaticstringregisterperson(个人p)
{
字符串msg=“”;
字符串URL=URLbase+“person/register”;//--URL-RIGHT,在邮递员中测试,正常插入数据
FormUrlEncodedContent参数=新FormUrlEncodedContent(新[]{
新的KeyValuePair(“名称”,p.Name),
新的KeyValuePair(“电话”,p.Fone),
新的KeyValuePair(“出生”,p.Birth),
});
HttpClient请求=新建HttpClient();
request.DefaultRequestHeaders.Accept.Clear();
request.DefaultRequestHeaders.Accept.Add(新的MediaTypeWithQualityHeaderValue(“应用程序/json”);

HttpResponseMessage response=request.PostAsync(URL,param).GetWaiter().GetResult();//所以要先回答这个问题,我打赌您需要设置一个“Content Type”标题,因为它抱怨提供了错误的媒体类型(您正在设置您愿意接受的内容,但不是您正在发送的内容)。服务是否期望应用程序/x-www-form-urlencoded内容(即您要发送的内容)?如果是,请将其作为内容类型提供,但现在这样做不太常见


这是经典的WebApi还是.net Core?我的问题是,如果它是后者,请将您的方法更改为非静态的,注入IHttpClientFactory并使用它来构建您的客户端。好处是您可以在注入(Startup.cs)时创建适合您需要的客户端,并重复使用它们,同时还可以避免大规模的套接字问题(请注意,这需要大量的负载,因此如果这不是您想要的,请不要担心)。但是,它确实有助于更干净的控制器代码,因此从代码可读性的角度来看,这可能是值得的,这对我来说通常是我优化的。因此,为了首先回答这个问题,我打赌您需要设置一个“内容类型”报头,因为它抱怨提供了错误的媒体类型(您正在设置您愿意接受的内容,而不是您正在发送的内容)。服务是否期望应用程序/x-www-form-urlencoded encoded content(即您正在发送的内容)?如果是,请将其作为内容类型提供,但现在这样做不太常见


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

这是一个老问题,但由于代码示例还没有被接受的答案,我在这里发布了一个答案。希望这个带有代码示例的答案对其他人有所帮助ho和我一样在这个问题上犯了错误。我在application/json、FormUrlEncodedContent、Content-Type header等之间挣扎了几分钟,最后终于让它工作了。如下所示

选项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";
var url=”https://....your 网址在这里;
var param=新字典
{
{“键1”,“值1”},
{“键2”,“值2”}
//……等等
};
var内容=新的FormUrlEncodedContent(参数);
var response=\u httpClient.PostAsync(url、内容);
var result=response.result.Content.ReadAsStringAsync().result;
如果(!response.Result.IsSuccessStatusCode)
{
抛出新异常(结果);
}
return“将处理结果对象转换为您需要的任何内容,然后返回它”;
选项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";
var url=”https://....your 网址在这里;
var request=newhttprequestmessage(HttpMethod.Post,新Uri(url));
var param=新字典
{
{“键1”,“值1”},
{“键2”,“值2”}
//……等等
};
var内容=新的FormUrlEncodedContent(参数);
request.Content=内容;
var response=\u httpClient.SendAsync(请求);
var result=response.result.Content.ReadAsStringAsync().result;
如果(!response.Result.IsSuccessStatusCode)
{
抛出新异常(结果);
}
return“将处理结果对象转换为您需要的任何内容,然后返回它”;

如果有人要复制/粘贴此代码,请注意,上面代码段中的\u httpClient对象首先在IoC中初始化,然后注入构造函数。您可以根据需要执行。因为“如何使用IoC”这不是这个问题的一部分,我将详细说明。

这是一个老问题,但由于代码示例中还没有公认的答案,我在这里发布了一个答案。希望这个带有代码示例的答案对其他像我一样偶然发现这个问题的人有所帮助。我在应用程序/j之间挣扎了几分钟son、FormUrlEncodedContent、内容类型标头等,然后最终使其正常工作

选项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";
var url=”https://....your 网址在这里;
var param=新字典
{
{“键1”,“值1”},
{“键2”,“值2”}
//……等等
};
var内容=新的FormUrlEncodedContent(参数);
var response=\u httpClient.PostAsync(url、内容);
var result=response.result.Content.ReadAsStringAsync().result;
如果(!response.Result.IsSuccessStatusCode)
{
抛出新异常(结果);
}
return“将处理结果对象转换为您需要的任何内容,然后返回它”;
选项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";
var url=”https://....your 网址在这里;
var request=newhttprequestmessage(HttpMethod.Post,新Uri(url));
var param=新字典
{
{“键1”,“值1”},
{“键2”,“值2”}
//……等等
};
var content=new FormUrlEncodedContent