Httpclient()发送异步POST请求时的异常问题
使用Httpclient()发送POST 请求以获得Token,部分程序如下:
在调试过程中,发现程序运行到
var response = await client.SendAsync(request);时,就不能往下继续运行,过程中既没有错误也没有其他消息显示。
本人菜鸟,已经被这个问题弄得焦头烂额了两周了,盼请大牛指教。
public async Task<string> GetAccessTokenAsync()
{
if (SubscriptionKey == string.Empty) return string.Empty;
// Re-use the cached token if there is one.
if ((DateTime.Now - storedTokenTime) < TokenCacheDuration)
{
return storedTokenValue;
}
using (var client = new HttpClient())
using (var request = new HttpRequestMessage())
{
request.Method = HttpMethod.Post;
request.RequestUri = ServiceUrl;
request.Content = new StringContent(string.Empty);
request.Headers.TryAddWithoutValidation(OcpApimSubscriptionKeyHeader, this.SubscriptionKey);
client.Timeout = TimeSpan.FromSeconds(2);
var response = await client.SendAsync(request);
this.RequestStatusCode = response.StatusCode;
response.EnsureSuccessStatusCode();
var token = await response.Content.ReadAsStringAsync();
storedTokenTime = DateTime.Now;
storedTokenValue = "Bearer " + token;
return storedTokenValue;
}
}在调试过程中,发现程序运行到
var response = await client.SendAsync(request);时,就不能往下继续运行,过程中既没有错误也没有其他消息显示。
本人菜鸟,已经被这个问题弄得焦头烂额了两周了,盼请大牛指教。
...全文
相关推荐
HttpClient完整实例(普通参数、JSON格式参数)
使用HttpClient发送POST请求,并解析响应,发送参数可以是普通参数或JSON格式参数。
HTTP异步工具包(HttpClient实现)
HTTP异步工具包(HttpClient实现) http://blog.csdn.net/lyl_studio/article/details/9319549
Apache HttpClient Demo
NULL 博文链接:https://antlove.iteye.com/blog/1815470
C# post方式提交Form表单
C# post方式提交Form表单。 C# post方式提交Form表单,可以支持带参数传入,供大家学习参考。 C# post提交表单。
C# 模拟Post请求
C#模拟post请求,可以在程序中模拟网络请求,方便在程序中使用


