Esempio n. 1
0
        private string BuildRequestBody(OutcomingData data)
        {
            var nonce = _random.Next().ToString("D");
            var items = data.GetFields(_jsonParser)
                        .Concat(GetCommonOutcomingFields(data))
                        .Where(x => !string.IsNullOrEmpty(x.Value))
                        .ToList();

            items.Add(new KeyValuePair <string, string>("sign", ComputeSign(items)));

            var xml = new XElement("xml", items.Select(x => new XElement(x.Key, x.Value)));

            return(xml.ToString(SaveOptions.DisableFormatting));
        }
Esempio n. 2
0
        public async Task <TIncoming> SendRequest <TIncoming, TErrorCode>(string url, bool requiresClientCert, OutcomingData data, bool checkSignatue)
            where TIncoming : IncomingData <TErrorCode>, new()
            where TErrorCode : struct
        {
            var response = await GetResponse(url, requiresClientCert, data);

            var responseBody = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

            return(ParseResponse <TIncoming, TErrorCode>(responseBody, checkSignatue));
        }
Esempio n. 3
0
        private IEnumerable <KeyValuePair <string, string> > GetCommonOutcomingFields(OutcomingData data)
        {
            yield return(new KeyValuePair <string, string>(data.AppIdFieldName, _appid));

            yield return(new KeyValuePair <string, string>(data.MerchantIdFieldName, _mch_id));

            yield return(new KeyValuePair <string, string>(data.SubAppIdFieldName, _sub_appid));

            yield return(new KeyValuePair <string, string>(data.SubMerchantIdFieldName, _sub_mch_id));

            var nonce = _random.Next().ToString("D");

            yield return(new KeyValuePair <string, string>("nonce_str", nonce));
        }
Esempio n. 4
0
        public async Task <HttpResponseMessage> GetResponse(string url, bool requiresClientCert, OutcomingData data)
        {
            var requestBody = BuildRequestBody(data);
            var request     = new HttpRequestMessage(HttpMethod.Post, url)
            {
                Content = new StringContent(requestBody, Encoding.UTF8)
            };

            HttpClientHandler handler;

            if (requiresClientCert)
            {
                handler = new HttpClientHandler
                {
                    ClientCertificateOptions = ClientCertificateOption.Manual,
                    AutomaticDecompression   = DecompressionMethods.Deflate |
                                               DecompressionMethods.GZip,
                };
                handler.ClientCertificates.Add(_cert);
            }
            else
            {
                handler = new HttpClientHandler
                {
                    AutomaticDecompression = DecompressionMethods.Deflate |
                                             DecompressionMethods.GZip,
                };
            }

            var http = CreateHttpClient(handler);

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

            response.EnsureSuccessStatusCode();
            return(response);
        }