【问题标题】:getting server error 500, 'Internal Server Error', but works fine on Postman, using httpclient in c#获取服务器错误 500,“内部服务器错误”,但在 Postman 上工作正常,在 c# 中使用 httpclient
【发布时间】:2020-07-01 18:22:47
【问题描述】:

当我使用邮递员时,一切似乎都很好,我得到了正确的响应,只有使用代码才能得到错误 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;
        }
  

我只在邮递员中收到回复,使用相同的 url 和 access_token 我只是为了隐私而删除了,你能解释一下它有什么问题吗?缺少的部分在哪里!

【问题讨论】:

  • 我建议捕获 HTTP 流量并逐行比较它们。您的请求中可能缺少标头或其他标记。
  • 正如 John Wu 所推荐的。 Fiddler 是一个很好的捕获 http 流量的工具

标签: c# api rest httpclient desktop-application


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-11-01
    • 1970-01-01
    • 2015-05-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多