HttpClient GetAsync fails in background task on Windows 8

白昼怎懂夜的黑 提交于 2019-11-30 13:02:39

You have to call IBackgroundTaskInterface.GetDeferral and then call its Complete method when your Task is complete.

Following is the way I am doing it and it works for me

        // Create a New HttpClient object.
        var handler = new HttpClientHandler {AllowAutoRedirect = false};
        var client = new HttpClient(handler);
        client.DefaultRequestHeaders.Add("user-agent",
                                         "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)");

        var response = await client.GetAsync(url);
        response.EnsureSuccessStatusCode();
        return await response.Content.ReadAsStringAsync();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!