How to use HttpWebRequest (.NET) asynchronously?

后端 未结 8 1595
天涯浪人
天涯浪人 2020-11-22 13:56

How can I use HttpWebRequest (.NET, C#) asynchronously?

相关标签:
8条回答
  • 2020-11-22 14:36

    I ended up using BackgroundWorker, it is definitely asynchronous unlike some of the above solutions, it handles returning to the GUI thread for you, and it is very easy to understand.

    It is also very easy to handle exceptions, as they end up in the RunWorkerCompleted method, but make sure you read this: Unhandled exceptions in BackgroundWorker

    I used WebClient but obviously you could use HttpWebRequest.GetResponse if you wanted.

    var worker = new BackgroundWorker();
    
    worker.DoWork += (sender, args) => {
        args.Result = new WebClient().DownloadString(settings.test_url);
    };
    
    worker.RunWorkerCompleted += (sender, e) => {
        if (e.Error != null) {
            connectivityLabel.Text = "Error: " + e.Error.Message;
        } else {
            connectivityLabel.Text = "Connectivity OK";
            Log.d("result:" + e.Result);
        }
    };
    
    connectivityLabel.Text = "Testing Connectivity";
    worker.RunWorkerAsync();
    
    0 讨论(0)
  • 2020-11-22 14:43

    By far the easiest way is by using TaskFactory.FromAsync from the TPL. It's literally a couple of lines of code when used in conjunction with the new async/await keywords:

    var request = WebRequest.Create("http://www.stackoverflow.com");
    var response = (HttpWebResponse) await Task.Factory
        .FromAsync<WebResponse>(request.BeginGetResponse,
                                request.EndGetResponse,
                                null);
    Debug.Assert(response.StatusCode == HttpStatusCode.OK);
    

    If you can't use the C#5 compiler then the above can be accomplished using the Task.ContinueWith method:

    Task.Factory.FromAsync<WebResponse>(request.BeginGetResponse,
                                        request.EndGetResponse,
                                        null)
        .ContinueWith(task =>
        {
            var response = (HttpWebResponse) task.Result;
            Debug.Assert(response.StatusCode == HttpStatusCode.OK);
        });
    
    0 讨论(0)
提交回复
热议问题