出现服务器错误 500,“内部服务器错误”,但在 Postman 上运行良好,在 C# 中使用 httpclient

问题描述 投票:0回答:1

当我使用 Postman 时,一切似乎都很好,我得到了正确的响应,只有使用代码才会出现错误 500。 在在这里发布问题之前我经常检查。

 string host = "removed for privacy";
        string access_token = "removed for privecy"
        int profileId = 25; 
        string getTemplate = "/profiles/{0}/orders?order_status=new";
        string putTemplate = "/profiles/{0}/orders/{1}";
        string getPath;
        string putPath;

        HttpClient client = new HttpClient(new HttpClientHandler { AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate });
        
        public User()
        {
            client.BaseAddress = new Uri(host);

            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", access_token);

            getPath = string.Format(getTemplate, profileId);
            Uri.EscapeUriString(getPath);
        }

        public List<Order> GetNewOrders()
        {

            Console.WriteLine(client.BaseAddress + getPath);

            List<Order> orders = null;
            HttpResponseMessage response = client.GetAsync(client.BaseAddress + getPath).Result;
            Console.WriteLine(response);

            if (response.IsSuccessStatusCode)
            {

                orders = response.Content.ReadAsAsync<List<Order>>().Result;               
            }
            else
            {
                response.EnsureSuccessStatusCode();
            }
            return orders;
        }  

我仅在 Postman 中收到回复,使用相同的 url 和

access_token
。为了隐私我当时就删除了。你能解释一下它有什么问题以及缺失的部分在哪里吗?

c# rest desktop-application dotnet-httpclient
1个回答
0
投票

谢谢大家,我使用的是授权标头,不需要它,我的访问令牌直接传递到请求正文中,谢谢,问题现在已经解决了

© www.soinside.com 2019 - 2024. All rights reserved.