如何使用ASP.Net MVC将原始文本转换为原始JSON格式?

时间:2019-05-22 01:13:44

标签: c# json asp.net-mvc asp.net-mvc-4

我在向API发送HTTP Request时遇到问题。

由于我发送的内容在Internal Server Error 500中,但应该在Text format中,因此我一直在使用API​​从下面的代码接收到JSON format。我也尝试过JsonConverter.SerializeObject来转换fileJson变量,但是它不起作用。

var client = new HttpClient();
                    var httpRequestMessage = new HttpRequestMessage
                    {
                        Method = HttpMethod.Post,
                        RequestUri = new Uri("https://some-url"),
                        Headers = {
                            { HttpRequestHeader.Authorization.ToString(), "Bearer " + token},
                            { HttpRequestHeader.Accept.ToString(), "application/json" },
                            { HttpRequestHeader.ContentType.ToString(), "application/json"},
                            { "client-id", "clientid"},
                            { "client-secret","clientsecret"},
                            { "partner-id","partnerid"},
                            { "X-Version", "1" }
                        },
                        Content = new StringContent(fileJson)
                    };

                    var response = client.SendAsync(httpRequestMessage).Result;

这是fileJson包含的字符串

{  
   "senderRefId":"TRANSFER0001",
   "tranRequestDate":"2018-08-10T08:40:45.897",
   "particulars":"particulars",
   "sender":{  
      "name":"name",
      "address":{  
         "line1":"line1",
         "line2":"line2",
         "city":"city",
         "province":"province",
         "zipCode":"zip",
         "country":"country"
      }
   }

我已经在Postman上对其进行了测试,当我的请求正文位于原始JSON format中时,它可以工作,而当它位于Text format中时,则出现错误。

0 个答案:

没有答案