Exemple #1
0
        public static List <T> PostRead <T>(string methodName, string data)
        {
            GetResponseList <T> getResponseList = new GetResponseList <T>();

            try
            {
                if (!string.IsNullOrEmpty(methodName))
                {
                    //Yoonus
                    if (HttpContext.Current.Session["token"] == null)
                    {
                        HttpContext.Current.Session["token"] = Service.GetToken();
                    }
                    else if (((TokenInfo)HttpContext.Current.Session["token"]).token_expires_datetime <= DateTime.Now)
                    {
                        HttpContext.Current.Session["token"] = Service.GetToken();
                    }
                    //Yoonus

                    StringBuilder stringBuilder = new StringBuilder();
                    stringBuilder.Append("{");
                    if (Service.Parameters.Count > 0)
                    {
                        foreach (KeyValuePair <string, string> parameter in Service.Parameters)
                        {
                            stringBuilder.Append(string.Concat(new string[] { "\"", parameter.Key, "\":\"", parameter.Value, "\"," }));
                        }
                    }
                    stringBuilder.Append("}");
                    byte[]         bytes  = Encoding.ASCII.GetBytes(data);
                    HttpWebRequest length = (HttpWebRequest)WebRequest.Create(string.Concat(Service.url, methodName));
                    length.Method      = "POST";
                    length.ContentType = "application/json";

                    TokenInfo item = (TokenInfo)HttpContext.Current.Session["token"];
                    length.Headers.Add("Authorization", string.Concat(item.token_type, " ", item.access_token));
                    length.ContentLength = (long)((int)bytes.Length);
                    using (Stream requestStream = length.GetRequestStream())
                    {
                        requestStream.Write(bytes, 0, (int)bytes.Length);
                    }
                    using (WebResponse response = length.GetResponse())
                    {
                        using (StreamReader streamReader = new StreamReader(response.GetResponseStream()))
                        {
                            getResponseList = JsonConvert.DeserializeObject <GetResponseList <T> >(streamReader.ReadToEnd());
                        }
                        if (getResponseList == null)
                        {
                            getResponseList = new GetResponseList <T>()
                            {
                                result = new List <T>()
                            };
                        }
                        if (getResponseList.success != "1")
                        {
                            getResponseList.result = new List <T>();
                        }
                    }
                }
            }
            catch (WebException webException)
            {
                using (Stream responseStream = webException.Response.GetResponseStream())
                {
                    (new StreamReader(responseStream)).ReadToEnd();
                }
            }
            catch (Exception exception)
            {
                throw exception;
            }
            return(getResponseList.result);
        }
Exemple #2
0
        public static List <T> GetList <T>(string methodName)
        {
            GetResponseList <T> getResponseList = new GetResponseList <T>();

            try
            {
                if (!string.IsNullOrEmpty(methodName))
                {
                    //Yoonus
                    if (HttpContext.Current.Session["token"] == null)
                    {
                        HttpContext.Current.Session["token"] = Service.GetToken();
                    }
                    else if (((TokenInfo)HttpContext.Current.Session["token"]).token_expires_datetime <= DateTime.Now)
                    {
                        HttpContext.Current.Session["token"] = Service.GetToken();
                    }
                    //Yoonus

                    StringBuilder stringBuilder = new StringBuilder();
                    if (Service.Parameters.Count > 0)
                    {
                        foreach (KeyValuePair <string, string> parameter in Service.Parameters)
                        {
                            stringBuilder.Append(string.Concat((stringBuilder.ToString() == string.Empty ? "?" : "&"), parameter.Key, "=", parameter.Value));
                        }
                        methodName = string.Concat(methodName, stringBuilder.ToString());
                    }
                    HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(string.Concat(Service.url, methodName));
                    httpWebRequest.Method      = "GET";
                    httpWebRequest.ContentType = "application/json";

                    TokenInfo item = (TokenInfo)HttpContext.Current.Session["token"];
                    httpWebRequest.Headers.Add("Authorization", string.Concat(item.token_type, " ", item.access_token));
                    using (WebResponse response = httpWebRequest.GetResponse())
                    {
                        using (StreamReader streamReader = new StreamReader(response.GetResponseStream()))
                        {
                            getResponseList = JsonConvert.DeserializeObject <GetResponseList <T> >(streamReader.ReadToEnd());
                        }
                        if (getResponseList == null)
                        {
                            getResponseList = new GetResponseList <T>()
                            {
                                result = new List <T>()
                            };
                        }
                        if (getResponseList.success != "1")
                        {
                            getResponseList.result = new List <T>();
                        }
                    }
                }
            }
            catch (WebException webException)
            {
                using (Stream responseStream = webException.Response.GetResponseStream())
                {
                    (new StreamReader(responseStream)).ReadToEnd();
                }
            }
            catch (Exception exception)
            {
                throw exception;
            }
            return(getResponseList.result);
        }