【发布时间】:2020-11-14 07:07:49
【问题描述】:
我想从 Web API 获取令牌宽度窗口控制台,并且有我的代码,但我发生了错误:
发送请求时出错,
我使用 Visual Studio 2013,.Net FrameWork 4.5,
private static async Task<string> GetAccessToken()
{
string baseUrl = "http://my.strategy.maskan";
string _ClientId = "abc";
string _ClientSecret = "password123";
string _Scope = "api1";
using (var client = new HttpClient())
{
client.BaseAddress = new Uri(baseUrl);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
List<KeyValuePair<string, string>> postData = new List<KeyValuePair<string, string>>();
postData.Add(new KeyValuePair<string, string>("ClientId", _ClientId));
postData.Add(new KeyValuePair<string, string>("ClientSecret", _ClientSecret));
postData.Add(new KeyValuePair<string, string>("Scope", _Scope));
FormUrlEncodedContent content = new FormUrlEncodedContent(postData);
HttpResponseMessage response = new HttpResponseMessage();
try
{
response = await client.PostAsync("IDENTITYSERVER", content);
}
catch (HttpRequestException ex)
{
Console.WriteLine(ex.Message);
}
string jsonString = await response.Content.ReadAsStringAsync();
return jsonString;
}
}
response = await client.PostAsync("IDENTITYSERVER", content); 中产生异常。
如何从 httpClient.PostAsync 修复此异常。
【问题讨论】:
-
您需要提供更多信息。
HttpRequestException ex中的内部异常与ex.StatusCode一样有帮助 -
你需要看InnerException
-
在发送请求时发生错误之后还有其他内容:那是什么?连接被远程端点关闭?发布完整的异常。 -- 这段代码在哪里运行,什么系统?
-
我解决了这个问题,我在我组织的错误 LAN 网络中。所以我在正确的域服务器中执行它。
标签: c# console client token webapi