Exemple #1
0
        private async Task <TResult> SendAsync <TResult>(HttpMethod method, Uri uri, object message) where TResult : class
        {
            Verbose($"{method} {uri} {message?.GetType()}");
            var nonceHeader = new AcmeHeader {
                Nonce = nonce
            };

            Verbose($"sending nonce {nonce}");

            HttpContent content = null;

            if (message != null)
            {
                var encodedMessage = jws.Encode(message, nonceHeader);
                var json           = JsonConvert.SerializeObject(encodedMessage, new JsonSerializerSettings()
                {
                    NullValueHandling = NullValueHandling.Ignore, Formatting = Formatting.Indented
                });
                content = new StringContent(json, Encoding.UTF8, "application/json");
            }

            var request = new HttpRequestMessage(method, uri)
            {
                Content = content
            };

            var response = await client.SendAsync(request).ConfigureAwait(false);

            Verbose($"response status: {(int)response.StatusCode} {response.ReasonPhrase}");

            RememberNonce(response);


            if (response.Content.Headers.ContentType.MediaType == "application/problem+json")
            {
                var problemJson = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

                var problem = JsonConvert.DeserializeObject <Problem>(problemJson);
                Verbose($"error response from server: {problem.Type}: {problem.Detail}");
                throw new AcmeException(problem, response);
            }

            if (typeof(TResult) == typeof(CertificateResponse) && response.Content.Headers.ContentType.MediaType == "application/pkix-cert")
            {
                var certificateBytes = await response.Content.ReadAsByteArrayAsync().ConfigureAwait(false);

                var certificateResponse = new CertificateResponse()
                {
                    Certificate = certificateBytes
                };
                GetHeaderValues(response, certificateResponse);
                return(certificateResponse as TResult);
            }

            var responseContent = await response.Content.ReadAsAsync <TResult>().ConfigureAwait(false);

            GetHeaderValues(response, responseContent);

            return(responseContent);
        }