且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

UWP推送通知后台任务httpClient无法正常工作

更新时间:2023-02-01 15:24:20

您好RAMESHKUMAR SWANKARI,

Hi RAMESHKUMAR SWANKARI,

我们可以将await表达式用于  HttpClient.PostAsync方法。然后我们就可以得到HttpClient的状态。

We can use the await expression to the HttpClient.PostAsync method. And then we can get the state of the HttpClient.

例如:

private HttpClient httpClient;
private CancellationTokenSource cts;
private void OnProgress(IBackgroundTaskRegistration task, BackgroundTaskProgressEventArgs args)
{
    string u = "http://localhost/HttpClientSample/default.aspx";
    var myuir = new Uri(u);
    string str = "hello";
    var b = new HttpStringContent(str);
        var ignored = Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async () =>
        {
            try
            {
                Windows.Web.Http.HttpResponseMessage response = await httpClient.PostAsync(myuir, b).AsTask(cts.Token);
            }
            catch (TaskCanceledException)
            {
            }
            catch (Exception ex)
            {
            }
            finally
            {
            }
    });
}

***的问候,

Jayden Gu

Jayden Gu