Пример #1
0
        private APIBaseResponse _execute(string url, APIBaseMethod method, List <APIBaseParameter> headers, dynamic body, List <APIBaseParameter> pathParam = null)
        {
            var rst = new APIBaseResponse
            {
                ResponseStatus = -1,
            };

            try
            {
                rst.ResponseString = string.Empty;
                var stringBody = string.Empty;

                // Make Url
                var uri = buildUrl(url, method, body, pathParam);

                // Make body
                if (body != null && method != APIBaseMethod.GET)
                {
                    stringBody = json2String(body);
                    //stringBody = stringBody.Replace("\\\\", "\\");
                }

                return(_executeBase(uri, method, headers, stringBody));
            }
            catch (Exception ex)
            {
                if (rst.Error == null)
                {
                    rst.Error = ex;
                }
            }
            return(rst);
        }
Пример #2
0
        protected JsonResult JsonResult(APIBaseResponse response)
        {
            if (!response.IsSuccess)
            {
                return(ErrorJsonResult(GetMessage(response.ResponseCode)));
            }

            return(SuccessJsonResult());
        }
Пример #3
0
        public APIBaseResponse UpdateCustomerPassword(string userNo, string password)
        {
            var response = new APIBaseResponse {
                IsSuccess = true
            };
            var customerDb = customerRepository.GetCustomer(userNo);

            if (customerDb == null)
            {
                response.IsSuccess    = false;
                response.ResponseCode = "COM_001";
                return(response);
            }

            customerDb.Password = Cryptor.Encrypt(password);
            return(response);
        }
Пример #4
0
        public APIBaseResponse CreateCustomer(Customer customer)
        {
            var response = new APIBaseResponse {
                IsSuccess = true
            };
            var customerDb = customerRepository.GetCustomer(customer.UserNo);

            if (customerDb != null)
            {
                response.IsSuccess    = false;
                response.ResponseCode = "CM_001";
                return(response);
            }

            customer.Password = Cryptor.Encrypt(customer.Password);
            customerRepository.Insert(customer);
            unitOfWork.Commit();
            return(response);
        }
Пример #5
0
        public APIBaseResponse UpdateCustomer(Customer customer)
        {
            var response = new APIBaseResponse {
                IsSuccess = true
            };
            var customerDb = customerRepository.GetCustomer(customer.UserNo);

            if (customerDb == null)
            {
                response.IsSuccess    = false;
                response.ResponseCode = "COM_001";
                return(response);
            }

            customerDb.Name   = customer.Name;
            customerDb.Sex    = customer.Sex;
            customerDb.UserNo = customer.UserNo;
            customerRepository.Update(customerDb);
            unitOfWork.Commit();
            return(response);
        }
Пример #6
0
        private APIBaseResponse _executeBase(string url, APIBaseMethod method, List <APIBaseParameter> headers, string strBody)
        {
            var rst = new APIBaseResponse
            {
                ResponseStatus = -1,
            };

            try
            {
                rst.ResponseString = string.Empty;

                try
                {
                    if (EnabelSSL3)
                    {
                        try
                        {
                            ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls12;
                        }
                        catch (Exception) { }
                    }

                    var            uri     = new Uri(url);
                    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
                    request.ContentType = ContentType;
                    request.Method      = method + string.Empty;
                    request.KeepAlive   = false;
                    //request.UseDefaultCredentials = true;

                    // Add header
                    if (headers != null)
                    {
                        foreach (var header in headers)
                        {
                            if (string.IsNullOrEmpty(header.Key))
                            {
                                continue;
                            }
                            if (header.Key.ToLower() == "content-type")
                            {
                                continue;
                            }
                            if (header.Key.ToLower() == "host")
                            {
                                //request.Host = (string.IsNullOrEmpty(header.StringValue) ? (header.Value + "") : header.StringValue);
                                //request.Headers.Add(header.Key, (string.IsNullOrEmpty(header.StringValue) ? (header.Value + "") : header.StringValue));
                                continue;
                            }
                            request.Headers.Add(header.Key, (string.IsNullOrEmpty(header.StringValue) ? (header.Value + "") : header.StringValue));
                        }
                    }


                    // Add cert
                    if (Certificate != null)
                    {
                        request.ClientCertificates.Clear();
                        request.ClientCertificates.Add(Certificate);
                    }
                    else
                    {
                        if (uri.Scheme.ToLower() == "https")
                        {
                            ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) => { return(true); };
                        }
                    }

                    if (method != APIBaseMethod.GET && !string.IsNullOrWhiteSpace(strBody))
                    {
                        using (var streamWriter = new StreamWriter(request.GetRequestStream()))
                        {
                            streamWriter.Write(strBody);
                            streamWriter.Flush();
                            streamWriter.Close();
                        }
                    }

                    var response = (WebResponse)request.GetResponse();
                    using (var streamReader = new StreamReader(response.GetResponseStream()))
                    {
                        rst.ResponseString = streamReader.ReadToEnd();
                    }
                    rst.ResponseStatus = 200;
                }
                catch (WebException e)
                {
                    rst.Error = e;
                    if (e.Response != null)
                    {
                        using (WebResponse wresponse = e.Response)
                        {
                            HttpWebResponse httpResponse = (HttpWebResponse)wresponse;
                            rst.ResponseStatus = (int)httpResponse.StatusCode;
                            using (Stream data = wresponse.GetResponseStream())
                                using (var reader = new StreamReader(data))
                                {
                                    rst.ResponseString = reader.ReadToEnd();
                                }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                if (rst.Error == null)
                {
                    rst.Error = ex;
                }
            }
            return(rst);
        }