| | | 1 | | using System; |
| | | 2 | | using System.Net; |
| | | 3 | | using System.Net.Http; |
| | | 4 | | using System.Threading.Tasks; |
| | | 5 | | using JetBrains.Annotations; |
| | | 6 | | using WireMock.HttpsCertificate; |
| | | 7 | | using WireMock.Validation; |
| | | 8 | | |
| | | 9 | | namespace WireMock.Http |
| | | 10 | | { |
| | | 11 | | internal static class HttpClientHelper |
| | | 12 | | { |
| | | 13 | | public static HttpClient CreateHttpClient(string clientX509Certificate2ThumbprintOrSubjectName = null) |
| | 2 | 14 | | { |
| | | 15 | | #if NETSTANDARD |
| | 2 | 16 | | var handler = new HttpClientHandler |
| | 2 | 17 | | { |
| | 2 | 18 | | CheckCertificateRevocationList = false, |
| | 2 | 19 | | SslProtocols = System.Security.Authentication.SslProtocols.Tls12 | System.Security.Authentication.SslPro |
| | 2 | 20 | | ServerCertificateCustomValidationCallback = (message, cert, chain, errors) => true, |
| | 2 | 21 | | AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate |
| | 2 | 22 | | }; |
| | | 23 | | #elif NET46 |
| | | 24 | | var handler = new HttpClientHandler |
| | | 25 | | { |
| | | 26 | | ServerCertificateCustomValidationCallback = (message, cert, chain, errors) => true, |
| | | 27 | | AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate |
| | | 28 | | }; |
| | | 29 | | ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11; |
| | | 30 | | #else |
| | | 31 | | var handler = new WebRequestHandler |
| | | 32 | | { |
| | | 33 | | ServerCertificateValidationCallback = (sender, certificate, chain, errors) => true, |
| | | 34 | | AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate |
| | | 35 | | }; |
| | | 36 | | ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11; |
| | | 37 | | #endif |
| | | 38 | | |
| | 2 | 39 | | if (!string.IsNullOrEmpty(clientX509Certificate2ThumbprintOrSubjectName)) |
| | 0 | 40 | | { |
| | 0 | 41 | | handler.ClientCertificateOptions = ClientCertificateOption.Manual; |
| | | 42 | | |
| | 0 | 43 | | var x509Certificate2 = ClientCertificateHelper.GetCertificate(clientX509Certificate2ThumbprintOrSubjectN |
| | 0 | 44 | | handler.ClientCertificates.Add(x509Certificate2); |
| | 0 | 45 | | } |
| | | 46 | | |
| | | 47 | | // For proxy we shouldn't follow auto redirects |
| | 2 | 48 | | handler.AllowAutoRedirect = false; |
| | | 49 | | |
| | | 50 | | // If UseCookies enabled, httpClient ignores Cookie header |
| | 2 | 51 | | handler.UseCookies = false; |
| | | 52 | | |
| | 2 | 53 | | var client = new HttpClient(handler); |
| | | 54 | | #if NET452 || NET46 |
| | | 55 | | ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityPro |
| | | 56 | | #endif |
| | 2 | 57 | | return client; |
| | 2 | 58 | | } |
| | | 59 | | |
| | | 60 | | public static async Task<ResponseMessage> SendAsync([NotNull] HttpClient client, [NotNull] RequestMessage reques |
| | 0 | 61 | | { |
| | 0 | 62 | | Check.NotNull(client, nameof(client)); |
| | 0 | 63 | | Check.NotNull(requestMessage, nameof(requestMessage)); |
| | | 64 | | |
| | 0 | 65 | | var originalUri = new Uri(requestMessage.Url); |
| | 0 | 66 | | var requiredUri = new Uri(url); |
| | | 67 | | |
| | | 68 | | // Create HttpRequestMessage |
| | 0 | 69 | | var httpRequestMessage = HttpRequestMessageHelper.Create(requestMessage, url); |
| | | 70 | | |
| | | 71 | | // Call the URL |
| | 0 | 72 | | var httpResponseMessage = await client.SendAsync(httpRequestMessage, HttpCompletionOption.ResponseContentRea |
| | | 73 | | |
| | | 74 | | // Create ResponseMessage |
| | 0 | 75 | | return await HttpResponseMessageHelper.Create(httpResponseMessage, requiredUri, originalUri); |
| | 0 | 76 | | } |
| | | 77 | | } |
| | | 78 | | } |