Making a windows phone application and although I may easily pull from my Web Api I am having trouble posting to it. Whenever posting to the api I get the "Unsupported Media Type" error message and I'm not sure as to why it is happening considering the class I using as the base for my JSON post is the same as the one used in the api.

PostQuote (Post Method)

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

推荐答案

You should set the media type when creating StringContent

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

Json相关问答推荐

Vega Lite中的图例对齐

JSON API返回多个数组,需要帮助拼合数据以存储在SQL Server数据库表中

如何在Swift中使用JSON编码器的泛型

如何使用模式注册中心创建从主题中取消本地化的ks qlDB表?

对一些JSON模式验证的混淆

如何在Android中解析带有动态键和可变对象名称的改装JSON响应?

jq 对特定键进行过滤并将值整理到单个 csv 单元格中

在Databricks中如何将JSON文件作为字典读取

无法使用 vue.js 访问 JSON 数组的项目

如何在 Postman 中匹配 json 响应中的内容?并可视化

从 PowerShell 编辑 Windows 终端配置文件设置 JSON

如何在golang中获取 struct 的json字段名称?

在 C# 中解析 Json rest api 响应

如何使用 Jackson 重命名 JSON 序列化中的根键

使用 jq,将对象数组转换为具有命名键的对象

在视图中将 .Net 对象转换为 JSON 对象

如何向从文件中检索的 JSON 数据添加键值?

在 React 中访问子级的父级状态

java.lang.BootstrapMethodError:调用站点#4 bootstrap 方法的异常,初始化 retrofit 时

使用 axios 在 POST multipart/form-data 请求中发送文件和 json