Public async TaskGetAccessTokenAsync ()
{
If (SubscriptionKey==string. The Empty) return string. The Empty;
//Re - use the cached token if there is one.
If ((DateTime. Now - storedTokenTime) & lt; TokenCacheDuration)
{
Return storedTokenValue;
}
Using (var client=new HttpClient ())
Using (var request=new HttpRequestMessage ())
{
Request. The Method=HttpMethod. Post;
Request. RequestUri=ServiceUrl;
Request. The Content=new StringContent (string. The Empty);
Request. Headers. TryAddWithoutValidation (OcpApimSubscriptionKeyHeader, enclosing SubscriptionKey);
Client. The Timeout=TimeSpan. FromSeconds (2);
Var response=await client. SendAsync (request);
Enclosing RequestStatusCode=response. StatusCode;
The response. EnsureSuccessStatusCode ();
Var token=await the response. The content. ReadAsStringAsync ();
StoredTokenTime=DateTime. Now;
StoredTokenValue="https://bbs.csdn.net/topics/Bearer" + token;
Return storedTokenValue;
}
}
In the process of debugging, found that the program runs to
Var response=await client. SendAsync (request); , will not be able to continue running down, in the process of neither wrong nor any other news, according to
I am a rookie, has been the problem with two weeks, looking forward to Daniel, please advise,
CodePudding user response:
Await the back of the code is to wait SendAsync success will perform the ahCodePudding user response: