public HomeController(ILogger <HomeController> logger, Rc.ApplicationContext rc_context, Sb.ApplicationContext sb_context)
 {
     try
     {
         _rc_context = rc_context;
         _sb_context = sb_context;
         _logger     = logger;
     }
     catch (Exception e)
     {
         Log.Write(e);
     }
 }
Exemple #2
0
        public static T reqResp <T>(HttpContext http, Sb.ApplicationContext sb_context, object _obj_req, string action, HttpMethod meth = null)
        {
            var log_requst = new Sb.PaymentRequestHistory()
            {
                date       = DateTime.Now,
                isResponse = false,
                url        = baseAddress + action,
                uid        = http.Session.Id,
                body       = JsonConvert.SerializeObject(_obj_req, Config.jsonSerializerSettings)
            };

            sb_context.paymentRequestHistories.Add(log_requst);
            sb_context.SaveChanges();


            if (_obj_req.GetType() == typeof(SberClasses.Register_toSber))
            {
                Sb.OrderInfo orderInfo = Config.GetSessionValue <Sb.OrderInfo>(http, Config.ss_OrderInfo);
                orderInfo.response_id = log_requst.id;

                sb_context.orderInfo.Add(orderInfo);
                sb_context.SaveChanges();
            }


            var obj_resp = typeof(T);
            var obj_req  = _obj_req.GetType();

            object a = Activator.CreateInstance(obj_resp);

            try
            {
                var form  = new Dictionary <string, string>();
                var query = action + "?";
                var json  = "{";
                foreach (var p in obj_req.GetProperties())
                {
                    var v = obj_req.GetProperty(p.Name).GetValue(_obj_req);
                    if (v != null)
                    {
                        //var val = System.Web.HttpUtility.UrlEncode(v.ToString().Replace("\"", "'"));
                        var val = v.ToString();

                        json += "\"" + p.Name + "\":\"" + val + "\",";
                        form.Add(p.Name, val);
                        query += p.Name + "=" + val + "&";
                    }
                }
                json  = (json.Length > 1 ? json.Substring(0, json.Length - 1) : json) + "}";
                query = query.Substring(0, query.Length - 1);

                using (HttpClient client = new HttpClient())
                {
                    client.BaseAddress = baseAddress;
                    client.DefaultRequestHeaders.UserAgent.ParseAdd("Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.79 Safari/537.36");
                    client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("*/*"));

                    HttpRequestMessage httpRequest = null;

                    if (meth != null && meth.Equals(HttpMethod.Get))
                    {
                        httpRequest = new HttpRequestMessage(HttpMethod.Get, query);
                    }
                    else
                    {
                        httpRequest = new HttpRequestMessage(HttpMethod.Post, action);
                        //httpRequest.Content = new StringContent(json, Encoding.UTF8, "application/json");//"application/x-www-form-urlencoded"//"text/plain"//"application/json"
                        httpRequest.Content = new FormUrlEncodedContent(form);
                        httpRequest.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/x-www-form-urlencoded");
                    }

                    Task.Run(async() =>
                    {
                        var response = await client.SendAsync(httpRequest);

                        //Console.WriteLine("url=>    " + response.RequestMessage.RequestUri);
                        //foreach (var h in httpRequest.Content.Headers) Console.WriteLine("header=> " + h.Key + ": " + h.Value.FirstOrDefault());
                        //Console.WriteLine("body=>   " + await httpRequest.Content.ReadAsStringAsync());

                        var res = await response.Content.ReadAsStringAsync();
                        a       = JsonConvert.DeserializeObject(res, obj_resp, Config.jsonSerializerSettings);
                    }).Wait();
                }
            }
            catch (Exception err)
            {
                Console.WriteLine("error=>  " + err.Message);
            }
            finally
            {
                var log_response = new Sb.PaymentRequestHistory()
                {
                    date       = DateTime.Now,
                    isResponse = true,
                    url        = baseAddress + action,
                    uid        = http.Session.Id,
                    body       = JsonConvert.SerializeObject(a, Config.jsonSerializerSettings)
                };

                sb_context.paymentRequestHistories.Add(log_response);
                sb_context.SaveChanges();


                GC.Collect();
            }

            return((T)a);
        }