代码之家  ›  专栏  ›  技术社区  ›  Diego Venâncio

状态代码:415,原因短语:“不支持的媒体类型”

  •  0
  • Diego Venâncio  · 技术社区  · 6 年前

    我花了三天时间阅读了所有与此相关的文章和其他网站上的文章,关于这个错误,但没有成功!现在我需要帮助。

    错误:

    {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}}
    

    方法: 问题出现在: 要求PostAsync(URL,参数)。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;
    ....
    

    提前谢谢!

    0 回复  |  直到 6 年前
        1
  •  0
  •   fluidguid    4 年前

    这是一个老生常谈的问题,但由于代码示例中还没有公认的答案,我在这里发布了一个答案。希望这个带有代码示例的答案能对其他像我一样偶然发现这个问题的人有所帮助。我在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";
    

    选项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";
    

    如果有人要复制/粘贴此代码,请注意,上面代码段中的\u httpClient对象首先在IoC中初始化,然后注入构造函数。你可以做任何你需要的事。由于“如何使用国际奥委会”不是这个问题的一部分,我将详细讨论这个问题。

        2
  •  0
  •   Mirko    6 年前

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

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