Exemple #1
0
        public static T Post <T>(string url, string interfaceName, string method, params ServiceParameterInfo[] paramsData)
        {
            using (var httpclient = new HttpClient())
            {
                for (int i = 0; i < paramsData.Length; i++)
                {
                    paramsData[i].Sort = i;
                }

                var @params = new Dictionary <string, string>()
                {
                    { "interfacename", interfaceName },
                    { "methodname", method },
                    { "param", paramsData.ToJson() },
                };

                var content = new FormUrlEncodedContent(@params);

                var key = Guid.NewGuid().ToString();
                ApiRequest.AddChildrenRequest(key, url, "POST", @params.ToJson());
                var wathch = new Stopwatch();
                wathch.Start();

                var response = httpclient.PostAsync(url, content).Result.Content.ReadAsStringAsync().Result;

                wathch.Stop();
                ApiRequest.AddChildrenReponse(key, response, wathch.Elapsed.TotalMilliseconds);

                return(response.ToDeserialize <T>());
            }
        }
Exemple #2
0
        //private static HttpClient _client = new HttpClient();

        private static T LogAction <T>(string url, object data, string method, Dictionary <string, string> customerHeaders, string contentType = "application/json")
        {
            var request = WebRequest.CreateHttp(url);

            request.Timeout = 10000;

            if (customerHeaders != null)
            {
                foreach (var header in customerHeaders)
                {
                    request.Headers.Add(header.Key, header.Value);
                }
            }

            request.Method      = method.ToUpper();
            request.ContentType = $"{contentType};charset=UTF-8";

            var @params = data.ToJson();
            var key     = Guid.NewGuid().ToString();

            ApiRequest.AddChildrenRequest(key, url, method, @params);
            var wathch = new Stopwatch();

            wathch.Start();

            if (request.Method != "GET" && data != null)
            {
                if (contentType == "application/json")
                {
                    var reqStreamWriter = new StreamWriter(request.GetRequestStream());
                    reqStreamWriter.Write(@params);
                    reqStreamWriter.Close();
                }
                else
                {
                    var postData = Encoding.UTF8.GetBytes(data.ObjectToQuery());

                    var reqStream = request.GetRequestStream();
                    reqStream.Write(postData, 0, postData.Length);
                    reqStream.Close();
                    //reqStreamWriter.Write(postData, 0, postData.Length);
                }
            }

            var response       = request.GetResponse() as HttpWebResponse;
            var responseStream = new StreamReader(response.GetResponseStream());
            var result         = responseStream.ReadToEnd();

            responseStream.Close();

            wathch.Stop();
            ApiRequest.AddChildrenReponse(key, result, wathch.Elapsed.TotalMilliseconds);

            if (HttpContext.Current == null)
            {
                EngineContext.Current.Resolve <ILogger>().InsertHttpLog("HttpLog", new ChildRequest
                {
                    ElapsedTime = wathch.Elapsed.TotalMilliseconds,
                    Method      = method,
                    Params      = @params,
                    Url         = url,
                    Value       = result
                }.ToJson());
            }

            return(result.ToDeserialize <T>());
        }