Пример #1
0
        private static async Task <string> GetPageAsync(string page, string hostname = null, string[] pins = null, byte[] clientCert = null, string certPassword = null, byte[] caCert = null)
        {
            var secureHttpClientHandler = SecureHttpClientHandlerBuilder.Build();

            if (pins != null)
            {
                secureHttpClientHandler.AddCertificatePinner(hostname, pins);
            }
            if (clientCert != null)
            {
                var provider = new ImportedClientCertificateProvider();
                provider.Import(clientCert, certPassword);
                secureHttpClientHandler.SetClientCertificates(provider);
            }
            if (caCert != null)
            {
                secureHttpClientHandler.SetTrustedRoots(caCert);
            }
            string result;

            using (var httpClient = new HttpClient(secureHttpClientHandler))
                using (var response = await httpClient.GetAsync(page).ConfigureAwait(false))
                {
                    result = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

                    if (!response.IsSuccessStatusCode)
                    {
                        throw new HttpResponseException(response.StatusCode, result);
                    }
                }
            return(result);
        }
Пример #2
0
        protected void SetClientCertificate(byte[] clientCert, string certPassword)
        {
            var provider = new ImportedClientCertificateProvider();

            provider.Import(clientCert, certPassword);
            _secureHttpClientHandler.SetClientCertificates(provider);
        }