C. Lang
C. Lang

Reputation: 593

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

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();

Upvotes: 4

Views: 2239

Answers (2)

canton7
canton7

Reputation: 42245

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.

Upvotes: 13

Mahmoud-Abdelslam
Mahmoud-Abdelslam

Reputation: 643

 HttpRequestMessage httpRequestMessage = new HttpRequestMessage(HttpMethod.Head, "your url");
 httpClient.SendAsync(httpRequestMessage);

Upvotes: 0

Related Questions