您无法获取标头,GetJsonAsync<T>因为它返回Task<T>而不是原始响应.您可以GetAsync在下一步调用和反序列化您的有效负载:
HttpResponseMessage response = await url.GetAsync();
HttpResponseHeaders headers = response.Headers;
FooPayload payload = await response.ReadFromJsonAsync<FooPayload>();
Run Code Online (Sandbox Code Playgroud)
ReadFromJsonAsync 是一种扩展方法:
public static async Task<TBody> ReadFromJsonAsync<TBody>(this HttpResponseMessage response)
{
if (response.Content == null) return default(TBody);
string content = await response.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<TBody>(content);
}
Run Code Online (Sandbox Code Playgroud)
PS这就是为什么我更喜欢并建议使用原始HttpClient而不是任何第三方高级客户端,如RestSharp或Flurl.
| 归档时间: |
|
| 查看次数: |
1909 次 |
| 最近记录: |