为什么HttpClient.GetAsync()缺少部分内容?

时间:2018-07-16 19:07:50

标签: c# .net wpf uwp

Windows.Web.Http.HttpClientSystem.Net.Http.HttpClient收到不同的答复。

protected override void OnLaunched(LaunchActivatedEventArgs e) {
    var client = new Windows.Web.Http.HttpClient();
    var taskHttpResponse = client.GetAsync(new Uri(@"https://apis.live.net/v5.0/me?access_token=abcdefg"));
    Debug.WriteLine(taskHttpResponse.AsTask().Result.Content.ReadAsStringAsync().AsTask().Result);
}
{
  "error": {
  "code": "request_token_invalid", 
  "message": "The access token isn't valid."    } }

WindowsRT

private static void Main() {
    var client = new System.Net.Http.HttpClient();
    var taskHttpResponse = client.GetAsync(new Uri(@"https://apis.live.net/v5.0/me?access_token=abcdefg"));
    Console.WriteLine(taskHttpResponse.Result.Content.ReadAsStringAsync().Result);
}
}  }  "message": "The access token isn't valid."

.Net

你在开玩笑吗?

1 个答案:

答案 0 :(得分:1)

您不应使用 SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy"); Collections.sort(templateDirs, (o1, o2) -> { if (o1.get(3) == null || o2.get(3) == null) return 0; try { boolean b = formatter.parse(o1.get(3)).before(formatter.parse(o2.get(3))); return b ? 1:-1 ; } catch (ParseException e) { e.printStackTrace(); } return 0; }); 来使UI中的死锁见https://walterlv.github.io/post/deadlock-in-task-wait.html

我将您的代码更改为ReadAsStringAsync().Result,可以得到结果。

enter image description here

我发现您使用await的代码没有被未授权会是一个例外401。我认为该api必须在没有身份验证的情况下使用。