public static async Task <HttpResponseMessage> SendAsync(this HttpClient client, object url, object method, object headers = null, object content = null, CancellationToken cancellationToken = default) { if (client is null) { throw new ArgumentNullException(nameof(client)); } if (url is null) { throw new ArgumentNullException(nameof(url)); } if (!(url is Uri) && !(url is string)) { throw new ArgumentOutOfRangeException(nameof(url), $"For '{url}', only the following types are supported: Uri, string"); } if (method is null) { throw new ArgumentNullException(nameof(method)); } if (!(method is HttpMethod) && !(method is string)) { throw new ArgumentOutOfRangeException(nameof(method), $"For '{method}', only the following types are supported: HttpMethod, string"); } using var request = new HttpRequestMessage(method is string methodString ? new HttpMethod(methodString) : (HttpMethod)method, url is string urlString ? new Uri(urlString) : (Uri)url); if (!(content is null) && !ContentConverters.TryConvert(request, content)) { throw new ArgumentOutOfRangeException(nameof(content), $"For '{content}', only the following types are supported: {ContentConverters.SupportedTypesString}"); } if (!(headers is null) && !HeaderConverters.TryConvert(request, headers)) { throw new ArgumentOutOfRangeException(nameof(headers), $"For '{headers}', only the following types are supported: {HeaderConverters.SupportedTypesString}"); } return(await client.SendAsync(request, cancellationToken)); }
public static async Task <HttpResponseMessage> SendAsync(this HttpClient client, object url, object method, object headers = null, object content = null, CancellationToken cancellationToken = default) { if (client is null) { throw new ArgumentNullException(nameof(client)); } if (url is null) { throw new ArgumentNullException(nameof(url)); } if (!(url is Uri) && !(url is string)) { throw new ArgumentOutOfRangeException(nameof(url), $"For '{url}', only the following types are supported: Uri, string"); } if (method is null) { throw new ArgumentNullException(nameof(method)); } if (!(method is HttpMethod) && !(method is string)) { throw new ArgumentOutOfRangeException(nameof(method), $"For '{method}', only the following types are supported: HttpMethod, string"); } using var request = new HttpRequestMessage(method is string methodString ? new HttpMethod(methodString) : (HttpMethod)method, url is string urlString ? new Uri(urlString) : (Uri)url); if (!(content is null) && !ContentConverters.TryConvert(request, content)) { throw new ArgumentOutOfRangeException(nameof(content), $"For '{content}', only the following types are supported: {ContentConverters.SupportedTypesString}"); } if (!(headers is null) && !HeaderConverters.TryConvert(request, headers)) { throw new ArgumentOutOfRangeException(nameof(headers), $"For '{headers}', only the following types are supported: {HeaderConverters.SupportedTypesString}"); } #if DEBUG if (!(headers is null) && HttpMessageInvoker_Handler_FieldInfo.GetValue(client) is HttpClientHandler handler && handler.UseCookies) { using var r = new HttpRequestMessage { Content = new StringContent(string.Empty) }; HeaderConverters.TryConvert(r, headers); if (r.Headers.Contains("Cookie")) { System.Diagnostics.Debug.Assert(false, "Don't set cookies by headers if HttpClientHandler.UseCookies is true."); } } #endif return(await client.SendAsync(request, cancellationToken)); }