How do I send an OPTIONS request with System.Net.Http.HttpClient
exposed methods for HttpClient
I was expecting a OptionsAsync
switch (httpMethod) {
case HTTP_METHODS.DELETE:
{
httpResponseMessage = httpClient.DeleteAsync(uri).Result;
break;
}
case HTTP_METHODS.GET:
{
httpResponseMessage = httpClient.GetAsync(uri).Result;
break;
}
case HTTP_METHODS.POST:
{
httpResponseMessage = httpClient.PostAsync(uri, httpContent).Result;
break;
}
case HTTP_METHODS.PUT:
{
httpResponseMessage = httpClient.PutAsync(uri, httpContent).Result;
break;
}
case HTTP_METHODS.OPTION:
{
//not sure what method to call on httpclient here to make Options request
httpResponseMessage = httpClient.PutAsync(uri, httpContent).Result;
if (httpResponseMessage.Headers.Contains("X-CSRF-TOKEN")) {
IEnumerable < string > headerValues = httpResponseMessage.Headers.GetValues("X-CSRF-TOKEN");
csrfToken = headerValues.FirstOrDefault();
}
break;
}
}
The Hypertext Transfer Protocol (HTTP) is an application protocol for distributed, collaborative, hypermedia information systems. HTTP is the foundation of data communication for the World Wide Web. HttpClient is a base class for sending HTTP requests and receiving HTTP responses from a resource identified by a URI.
HttpClient is a base class for sending HTTP requests and receiving HTTP responses from a resource identified by a URI. HTTP response status codes indicate whether a specific HTTP request has been successfully completed.
The Java HTTP Client supports both HTTP/1.1 and HTTP/2. By default the client will send requests using HTTP/2. Requests sent to servers that do not yet support HTTP/2 will automatically be downgraded to HTTP/1.1. There are two ways to create an HttpClient.
Because HTTP DELETE requests typically contain no body, the DeleteAsync method doesn't provide an overload that accepts an instance of HttpContent. To learn more about using different HTTP verbs with HttpClient, see HttpClient. HttpClient has the concept of delegating handlers that can be linked together for outgoing HTTP requests.
There are no wrappers for that kind of methods (e.g. OPTIONS
and HEAD
), but you could use SendAsync
just like these wrappers do:
var request = new HttpRequestMessage(HttpMethod.Options, "url");
var result = await _httpClient.SendAsync(request);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With