PostAsync返回未经授权

时间:2019-02-07 14:48:42

标签: c#

我遇到了一个奇怪的问题,我很确定自己的令牌(我使用Postman尝试了它。),但是它不适用于下面的C#代码:

Env:ASP.NET Core 2.2

try
            {
                string _uri = "https://example.com/test";

                var _baseAddress = new Uri(_uri);

                using (var _client = new HttpClient() { BaseAddress = _baseAddress  })
                {
                    var _pairs = new { };

                    _client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", _token);
                    _client.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json");
                    _client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));


                    var _response = _client.PostAsync("", _pairs.AsJson()).Result;
                    HttpStatusCode _statusCode = _response.StatusCode;
                }
            }
            catch (Exception _ex) { ClsMain.SaveErrorLog("Create:" + _ex.ToString()); }
            finally { }

我错过了什么?

请帮助

0 个答案:

没有答案