发送httpcontent与PostAsync给出内部服务器错误

本文关键字:内部 服务器 错误 httpcontent PostAsync 发送 | 更新日期: 2023-09-27 18:02:32

发送数据作为PostAsync方法中的querystring,我使用以下方法。但我得到内部服务器错误。

 HttpResponseMessage response;
 string stringContent = "{ 'request_key': 'ABCD1234', 'request_code': 'CODE', 'request_type':'ID_type' }";
using(var client = new HttpClient()) {
                client.BaseAddress = new Uri(SubscriptionUtility.GetConfiguration("BaseURI"));
                client.DefaultRequestHeaders.Clear();
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(SubscriptionUtility.GetConfiguration("ContentType")));
                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", SubscriptionUtility.GetConfiguration("BasicAuthentication"));
                response = await client.PostAsync(SubscriptionUtility.GetConfiguration("SubscriptionAPI"), stringContent, new JsonMediaTypeFormatter());
                if(response.IsSuccessStatusCode) {
                    var dataObjects = JsonConvert.DeserializeObject<List<TestClass>>(response.Content.ReadAsStringAsync().Result);
                    //foreach(var d in dataObjects) {
                    //}
                }
            }

但是当我通过fiddler发送请求时,它工作得很好。这是我的小提琴请求

user - agent:提琴手

- type: application/json;utf - 8字符集=

主持人:testapi.com

font - family:宋体

授权:Basic 12fbe6e1f63d832aa33232323

Post数据:{"request_key":"这样","request_code":"代码",:"request_type ID_type"}

发送httpcontent与PostAsync给出内部服务器错误

我使用以下方法实现了所需的功能Post请求

using(var client = new HttpClient()) {

                client.BaseAddress = new Uri(SubscriptionUtility.GetConfiguration("BaseURI"));
                client.DefaultRequestHeaders.Clear();
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(SubscriptionUtility.GetConfiguration("ContentType")));
                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", SubscriptionUtility.GetConfiguration("BasicAuthentication"));
                var values = new Dictionary<string, string>
                    {
                       { "request_key", "ABCD1234" },
                       { "request_code", "CODE" },
                       { "request_type", "ID_type" }
                    };
                var content = new FormUrlEncodedContent(values);
                var response = await client.PostAsync(SubscriptionUtility.GetConfiguration("SubscriptionAPI"), content);
                var responseString = await response.Content.ReadAsStringAsync();