2

Is there anyway to prevent HttpWebRequest.GetRequestStream from throwing an exception when the method is GET? I know that HEAD and GET should not contain a body in valid HTTP, but some servers accept bodies for these methods anyway (e.g., Elasticsearch's _search feature). I have an open source project for doing REST calls and I was wondering if I could circumvent this behavior to avoid surprises.

At some point, I will probably create a version of my library using the new System.Net.Http.HttpClient class. Does this class have the same restrictions?

Travis Parks
  • 8,435
  • 12
  • 52
  • 85

1 Answers1

0

.Net Core 2.0:

System.Net.Http.HttpClient (Version=4.2.0.0) does not have this restriction if you use the SendAsync method.

var request = new HttpRequestMessage(HttpMethod.Get, "http://example.com/api/_search");
request.Content = new StringContent(jsonSearchCriteria, Encoding.UTF8, "application/json");
var response = await httpClient.SendAsync(request,
    HttpCompletionOption.ResponseContentRead);
var jsonResponse = await response.Content.ReadAsStringAsync();
KTCO
  • 2,115
  • 23
  • 21