Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a HTTP Completion Option for a post request in System.Net.Http?

Tags:

c#

httprequest

I want to send a post request but to speed things up not to download the entire page, just the head (or some smaller section of the content). I know there is a way you can do this with get requests but I need to do this with a post request. I am programming with c# and System.Net.Http. However, I am willing to use another library if its necessary.

Here is how the get request can only download headers:

var request = new HttpRequestMessage(HttpMethod.Get, url);
var getTask = client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead);

And here is my current code:

var response = await client.PostAsync(url, content);
var responseString = await response.Content.ReadAsStringAsync();
like image 726
C. Lang Avatar asked Oct 29 '25 01:10

C. Lang


1 Answers

You can just change HttpMethod.Get to HttpMethod.Post in your first sample, and assign to request.Content:

var request = new HttpRequestMessage(HttpMethod.Post, url)
{
    Content = content,
};
var response = await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead);

Of course, calling response.Content.ReadAsStringAsync() negates the point of using HttpCompletionOption.ResponseHeadersRead.

like image 96
canton7 Avatar answered Oct 31 '25 15:10

canton7