Пример #1
0
        public static Task <HttpResponseMessage> PostFromJsonAsync <TValue>(this HttpClient client, string url, TValue value)
        {
            var json    = JsonConvert.SerializeObject(value);
            var content = new StringContent(json);

            // 请求标题头
            // Accept:*/*
            client.DefaultRequestHeaders.Add("Accept", "*/*");
            // Content-MD5: Base64(MD5(Body))
            content.Headers.ContentMD5 = MD5Encrypt.GetContentMD5Data(json);
            // Content-Type:application/json
            content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json");
            // Date:yyyy-MM-dd HH:mm:ss
            //client.DefaultRequestHeaders.Add("Date", DateTime.Now.ToString());
            // X-Ca-Key:密钥ID
            client.DefaultRequestHeaders.Add("X-Ca-Key", "");
            // X-Ca-Signature:签名
            client.DefaultRequestHeaders.Add("X-Ca-Signature", Sha256.GetSignature(SecretKey, GetPresignature(client, content)));
            // X-Ca-Signature-Headers:x-ca-key
            client.DefaultRequestHeaders.Add("X-Ca-Signature-Headers", "x-ca-key");

            return(client.PostAsync(url, content));
        }