How to send an HTTP request and not process the response

As a service, my server sometimes needs to notify clients that their resource is ready to use.
To make this happen, they give me a URL as a callback, and when the resource is ready, I send an HTTP request to my callback URL.

What's the best way to initiate an HTTP request and not process the response?

var wc = new WebClient();
wc.DownloadStringAsync(new Uri(url));

      

In this code, for example, although I completely ignore the response, the server will still call a thread to download its response (which I care about ...) once it's ready.

+3


source to share


1 answer


I suggest you search for the package ID: Microsoft.Net.Http and use the more modern HttpClient from that library. Then the question is only about whether to "wait" for an answer or not. If you don't expect, then it is basically fire and forget and you will achieve the desired behavior:

using System;
using System.Net.Http;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
public class HttpHelper
{

    public static async Task WaitForResponse()
    {
        using (var client = new HttpClient())
        {
            HttpResponseMessage response = await  client.GetAsync(new Uri("http://google.com"));
        }
    }

    public static async Task FireAndForget()
    {
        using (var client = new HttpClient())
        {
            client.GetAsync(new Uri("http://google.com"));
        }
    }
}

      



}

0


source







All Articles