Async Example

Topics: Web Api
Oct 25, 2011 at 2:00 AM

Is there an example on how to use the aysnc methods of HttpClient? 

I need to send data from a web app (client) to a web service, without holding the web app up.  The service doesn't need to response to the client, it just needs to take the data and do the processing.  I'm trying to use a POST.

The call to the service only needs to be one way.

thanks

marc

Oct 26, 2011 at 12:43 AM

Can you take a look at the CustomFormatter sample where it is using HttpClient to do POST? The only difference here is probably you don't need to block for the result.

HttpClient client = new HttpClient

();

client.BaseAddress = baseAddress;

 

HttpRequestMessage request = new HttpRequestMessage(HttpMethod

.Post, path);

request.Content =

new StringContent("Id = " + id.ToString()+ "; Name = NewCustomer"

);

request.Content.Headers.ContentType =

new System.Net.Http.Headers.MediaTypeHeaderValue("application/xml"

);

PrintOutRequest(request);

client.SendAsync(request);

Let me know if this works for you. You can certainly use ObjectContent to send some CLR type which uses our default formatter for serialization.