我有如下代码:
...
AuthenticationHeaderValue authHeaders = new AuthenticationHeaderValue("OAuth2", Contract.AccessToken);
string result = await PostRequest.AuthenticatedGetData(fullUrl, null, authHeaders);
return result;
...
public static async Task<string> AuthenticatedGetData(string url, FormUrlEncodedContent data, AuthenticationHeaderValue authValue)
{
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(authValue.Parameter);
HttpResponseMessage response = await client.PostAsync(new Uri(url), data);
response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
response.EnsureSuccessStatusCode();
string responseBody = await response.Content.ReadAsStringAsync();
return responseBody;
}
使用 await part 的响应只是继续一个正在进行的循环,什么也不会发生。有任何想法我做错了什么吗?
实际问题是,我该如何发送以下标头:
Authorization: OAuth2 ACCESS_TOKEN
连接外部 Web API
http.DefaultRequestHeaders.TryAddWithoutValidation("Authorization", myVariableWithCustomAuthFormat);
- eidylon