Example #1
0
        public void StartRequest(string api, string method, XHttpRequestParamters paramter, HttpResponseHandler handler, bool needAuthtication = false)
        {
            var request = (HttpWebRequest)WebRequest.Create(api);

            request.Method      = method;
            request.Accept      = "application/json";
            request.ContentType = "application/x-www-form-urlencoded";
            if (needAuthtication)
            {
                AddAuthenticationHead(new Uri(api), method, paramter);
            }
            foreach (var headerString in paramter.HeaderStrings)
            {
                request.Headers[headerString.Key] = headerString.Value;
            }
            if (paramter.BodyParamters != null)
            {
                var urlEncodedContent = paramter.BodyParamters is string?paramter.BodyParamters.ToString()
                                            : new FormUrlEncodedContent(paramter.BodyParamters.ToKeyValue()).ReadAsStringAsync().Result;

                request.BeginGetRequestStream(PostCallBack, new HttpRequestAsyncState(request, urlEncodedContent, handler));
            }
            else
            {
                request.BeginGetResponse(ReadCallBack, new HttpResponseAsyncResult(request, handler));
            }
        }
Example #2
0
        public Task <string> StartRequestAsync(string api, string method, XHttpRequestParamters paramter, bool needAuthtication = false)
        {
            var request = (HttpWebRequest)WebRequest.Create(api);

            request.Method      = method;
            request.Accept      = "application/json";
            request.ContentType = "application/x-www-form-urlencoded";
            if (needAuthtication)
            {
                AddAuthenticationHead(new Uri(api), method, paramter);
            }
            foreach (var headerString in paramter.HeaderStrings)
            {
                request.Headers.Add(headerString.Key, headerString.Value);
            }
            if (paramter.BodyParamters != null)
            {
                var s = paramter.BodyParamters is string?paramter.BodyParamters.ToString()
                            : new FormUrlEncodedContent(paramter.BodyParamters.ToKeyValue()).ReadAsStringAsync().Result;

                var requestStream = request.GetRequestStream();
                var bytes         = Encoding.UTF8.GetBytes(s);
                requestStream.Write(bytes, 0, bytes.Length);
            }
            return(Task.Factory.FromAsync(request.BeginGetResponse, asyncResult => request.EndGetResponse(asyncResult), null).ContinueWith <string>(t => ReadStreamFromResponse(t.Result)));
        }
Example #3
0
        private void AddAuthenticationHead(Uri apiUrl, string method, XHttpRequestParamters paramter)
        {
            var requestContentBase64String = string.Empty;
            var requestUri        = HttpUtility.UrlEncode(apiUrl.ToString().ToLower());
            var requestHttpMethod = method;
            var requestTimeStamp  = Convert.ToUInt64((DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc)).TotalSeconds).ToString();
            var nonce             = Guid.NewGuid().ToString("N");

            if (paramter.BodyParamters != null)
            {
                requestContentBase64String = Convert.ToBase64String(MD5.Create().ComputeHash(Encoding.UTF8.GetBytes(paramter.BodyParamters is string
                                                                                                                    ?paramter.BodyParamters.ToString()
                                                                                                                        : new FormUrlEncodedContent(paramter.BodyParamters.ToKeyValue()).ReadAsStringAsync().Result)));
            }
            var s     = $"{AppId}{requestHttpMethod}{requestUri}{requestTimeStamp}{nonce}{requestContentBase64String}";
            var key   = Convert.FromBase64String(ApiKey);
            var bytes = Encoding.UTF8.GetBytes(s);

            using (var hmacshA256 = new HMACSHA256(key))
            {
                var base64String = Convert.ToBase64String(hmacshA256.ComputeHash(bytes));
                paramter.AddHeader("Authorization", $"cpx {AppId}:{base64String}:{nonce}:{requestTimeStamp}");
            }
        }