【问题标题】:Unsupported Media Type error when posting to Web API发布到 Web API 时出现不支持的媒体类型错误
【发布时间】:2015-10-10 04:04:14
【问题描述】:

制作一个 windows phone 应用程序,虽然我可以轻松地从我的 Web Api 中提取,但我在发布到它时遇到了麻烦。每当发布到 api 时,我都会收到“不支持的媒体类型”错误消息,考虑到我用作 JSON 帖子基础的类与 api 中使用的类相同,我不确定为什么会发生这种情况。

PostQuote(发布方法)

private async void PostQuote(object sender, RoutedEventArgs e)
        {
            Quotes postquote = new Quotes(){
                QuoteId = currentcount,
                QuoteText = Quote_Text.Text,
                QuoteAuthor = Quote_Author.Text,
                TopicId = 1019
            };
            string json = JsonConvert.SerializeObject(postquote);
            if (Quote_Text.Text != "" && Quote_Author.Text != ""){

                using (HttpClient hc = new HttpClient())
                {
                    hc.BaseAddress = new Uri("http://rippahquotes.azurewebsites.net/api/QuotesApi");
                    hc.DefaultRequestHeaders.Accept.Clear();
                    hc.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
                    HttpResponseMessage response = await hc.PostAsync(hc.BaseAddress, new StringContent(json));
                    if (response.IsSuccessStatusCode)
                    {
                        Frame.Navigate(typeof(MainPage));
                    }
                    else
                    {
                        Quote_Text.Text = response.StatusCode.ToString();
                        //Returning Unsupported Media Type//
                    }
                }
            }
        }

引用和主题(模型)

public class Quotes
    {
        public int QuoteId { get; set; }
        public int TopicId { get; set; }
        public string QuoteText { get; set; }
        public string QuoteAuthor { get; set; }
        public Topic Topic { get; set; }
        public string QuoteEffect { get; set; }
    }
    //Topic Model//
    public class Topic
    {
        public int TopicId { get; set; }
        public string TopicName { get; set; }
        public string TopicDescription { get; set; }
        public int TopicAmount { get; set; }
    }

【问题讨论】:

    标签: c# .net json asp.net-web-api windows-phone


    【解决方案1】:

    你应该在创建 StringContent 时设置媒体类型

    new StringContent(json, Encoding.UTF32, "application/json");
    

    【讨论】:

    • 不知何故它不适用于 Encoding.UTF32 。 Encoding.UTF8 确实有效。有什么解释吗?
    • 没有错误,这些值没有被解析到模型中(它们保持为空)
    • 我需要更多关于您的服务器代码的详细信息。这可能是一些服务器配置。
    • 如果您使用 C# / HttpClient,this 是问题所在(与此处提到的其他问题完全无关)。
    • 这对我不起作用,有人有更完整的例子吗?
    【解决方案2】:

    我在使用快速而肮脏的反向代理时发现了这个问题。我需要表单数据而不是 JSON。

    这对我有用。

    string formData = "Data=SomeQueryString&Foo=Bar";
    var result = webClient.PostAsync("http://XXX/api/XXX", 
            new StringContent(formData, Encoding.UTF8, "application/x-www-form-urlencoded")).Result;
    

    【讨论】:

      【解决方案3】:

      要修复不受支持的媒体类型,我必须使用 HttpRequestMessage 并添加标头以接受带有 MediaTypeWithQualityHeaderValue 的 json,如下所示。

              var httpRequestMessage = new HttpRequestMessage
              {
                  Content = new StringContent(json, Encoding.UTF8, "application/json")
              };
      
              httpRequestMessage.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));           
      
              var httpResponse = await _client.PostAsync("/contacts", httpRequestMessage.Content);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-03-16
        • 2017-09-23
        • 2017-10-20
        • 1970-01-01
        • 2023-03-08
        • 1970-01-01
        • 2014-11-14
        • 1970-01-01
        相关资源
        最近更新 更多