コード例 #1
0
        public static dynamic POSTRequest(string apiName, dynamic request, Enumeration.WebMethod Method, string token)
        {
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls
                                                   | SecurityProtocolType.Tls11
                                                   | SecurityProtocolType.Tls12;
            dynamic  dataItem;
            string   URldata;
            string   restUrl;
            string   QueryData   = null;
            DateTime RequestDate = DateTime.Now;


            int index = apiName.IndexOf("?");

            if (index > 0)
            {
                QueryData = apiName.Substring(index + 1);
            }

            URldata = apiName;
            try
            {
                using (WebClient Client = new WebClient())
                {
                    if (Method == Enumeration.WebMethod.PUT ||
                        Method == Enumeration.WebMethod.GET && token != string.Empty || Method == Enumeration.WebMethod.POST)
                    {
                        Client.Headers.Add("Authorization", "Bearer " + token);
                        // http.Headers.Add("Authorization", "Bearer " + token);
                    }
                    Client.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
                    var output = Client.UploadString(URldata, request);
                    var d      = JsonConvert.DeserializeObject <dynamic>(output);
                    dataItem = d;
                }
                return(dataItem);
            }
            catch (WebException ex)
            {
                //ExceptionList.SendErrorToText(ex);
                HttpWebResponse httpResponse = (HttpWebResponse)ex.Response;
                string          responseData = string.Empty;
                if (ex.Status == WebExceptionStatus.ProtocolError && httpResponse != null)
                {
                    WebResponse response = ex.Response;
                    dataItem = new StreamReader(response.GetResponseStream()).ReadToEnd();
                }
                else
                {
                    dataItem = JsonConvert.SerializeObject(new { code = 500, message = ex.Message, URLdata = apiName });
                }
                dataItem = JsonConvert.DeserializeObject <dynamic>(responseData);

                //SessionKey.Error = JsonConvert.DeserializeObject<ResponseBaseModel>(errData);
                return(dataItem);
            }
        }
コード例 #2
0
        public TModelResponse Get <TModelResponse>(string apiName, dynamic request1, Enumeration.WebMethod Method, string token, string apiUrl = null)
        {
            string restUrl = (apiUrl == null) ? SiteKey.ImageURL : apiUrl;

            object list    = null;
            string URldata = restUrl + apiName;

            try
            {
                var content = APIBind.CasePOST(request1, URldata);
                var data    = JsonConvert.DeserializeObject <TModelResponse>(content);
                if (data != null)
                {
                    list = data;
                }
                return((TModelResponse)list);
            }
            catch (WebException ex)
            {
                HttpWebResponse httpResponse = (HttpWebResponse)ex.Response;
                string          responseData = string.Empty;
                if (ex.Status == WebExceptionStatus.ProtocolError && httpResponse != null)
                {
                    WebResponse response = ex.Response;
                    responseData = new StreamReader(response.GetResponseStream()).ReadToEnd();
                }
                else
                {
                    responseData = JsonConvert.SerializeObject(new ResponseBaseModel()
                    {
                        Code = 500, Message = ex.Message, URLdata = apiName
                    });
                }
                list = JsonConvert.DeserializeObject <TModelResponse>(responseData);

                return((TModelResponse)list);
            }
        }
コード例 #3
0
        public static dynamic SendRequest(string apiName, dynamic request, Enumeration.WebMethod Method, string token)
        {
            dynamic dataItem;

            string restUrl;
            string URldata;

            DateTime RequestDate = DateTime.Now;

            URldata = apiName;

            try
            {
                using (WebClient Client = new WebClient())
                {
                    var http = (HttpWebRequest)WebRequest.Create(URldata);
                    http.Accept      = "application/json";
                    http.ContentType = "application/x-www-form-urlencoded"; //"application/json";



                    if (Method == Enumeration.WebMethod.PUT || Method == Enumeration.WebMethod.GET && token != string.Empty || Method == Enumeration.WebMethod.POST)
                    {
                        Client.Headers.Add("Authorization", "Bearer " + token);
                        http.Headers.Add("Authorization", "Bearer " + token);
                    }

                    if (Method == Enumeration.WebMethod.POST || Method == Enumeration.WebMethod.PUT)
                    {
                        string jsonString = string.Empty;
                        // PUT HERE YOUR JSON PARSED CONTENT >>;
                        jsonString = JsonConvert.SerializeObject(request);
                        //  ASCIIEncoding encoding = new ASCIIEncoding();
                        UTF8Encoding encoding  = new UTF8Encoding();
                        Byte[]       bytes     = encoding.GetBytes(jsonString);
                        Stream       newStream = http.GetRequestStream();
                        newStream.Write(bytes, 0, bytes.Length);
                        newStream.Close();
                    }
                    RequestDate = DateTime.Now;
                    var response = http.GetResponse();
                    var stream   = response.GetResponseStream();
                    var sr       = new StreamReader(stream);
                    var content  = sr.ReadToEnd();
                    var data     = JsonConvert.DeserializeObject <dynamic>(content);
                    dataItem = data;
                }
                return(dataItem);
            }
            catch (WebException ex)
            {
                //ExceptionList.SendErrorToText(ex);
                HttpWebResponse httpResponse = (HttpWebResponse)ex.Response;
                string          responseData = string.Empty;
                if (ex.Status == WebExceptionStatus.ProtocolError && httpResponse != null)
                {
                    WebResponse response = ex.Response;
                    responseData = new StreamReader(response.GetResponseStream()).ReadToEnd();
                }
                else
                {
                    //responseData = JsonConvert.SerializeObject(new ResponseBaseModel() { code = 500, message = ex.Message, URLdata = apiName });
                }
                dataItem = JsonConvert.DeserializeObject <dynamic>(responseData);
                return(dataItem);
            }
        }