Exemplo n.º 1
0
        public ReturnT PerformJsonRequest <ReturnT>(string url, HttpTools.RequestTypes method, object postData)
        {
            AsyncState asyncState;

            if (!typeof(ReturnT).Equals(typeof(string)))
            {
                throw new NotImplementedException("PerformJsonRequest accepts only string types for ReturnT");
            }

            if (method.Equals(HttpTools.RequestTypes.Get))
            {
                object stringGetResponse = Convert.ChangeType(PerformGet(url), typeof(ReturnT), null);
                return((ReturnT)stringGetResponse);
            }


            //preform an Async Call
            asyncState = BeginAsyncPOST <ReturnT>(url, postData);

            //wait for result
            asyncState.syncPoint.WaitOne();

            // get the response
            return((ReturnT)asyncState.responseObject);
        }
Exemplo n.º 2
0
        //private NameValueCollection ObjectToNameValueCollection<T>(T obj)  --> Stayed in HttpTools (PortLib) with Dic<s,s>

        #endregion

        #region JSON Request

        public ReturnT PerformJsonRequest <ReturnT>(string url, HttpTools.RequestTypes method, object postData)
        {
            //Initilize the http request
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);

            req.ContentType = "application/json";
            req.Method      = Enum.GetName(typeof(HttpTools.RequestTypes), method);

            //If posting data - serialize it to a json object
            if (method != HttpTools.RequestTypes.Get)
            {
                StringBuilder sbJsonRequest = new StringBuilder();
                var           T             = postData.GetType();
                foreach (var prop in T.GetProperties())
                {
                    if (HttpTools.NativeTypes.Contains(prop.PropertyType))
                    {
                        sbJsonRequest.AppendFormat("\"{0}\":\"{1}\",", prop.Name.ToLower(), prop.GetValue(postData, null));
                    }
                }

                using (var sWriter = new StreamWriter(req.GetRequestStream()))
                {
                    sWriter.Write("{" + sbJsonRequest.ToString().TrimEnd(',') + "}");
                }
            }

            //Submit the Http Request
            string responseJson = "";

            try
            {
                using (var wResponse = req.GetResponse())
                {
                    StreamReader sReader = new StreamReader(wResponse.GetResponseStream());
                    responseJson = sReader.ReadToEnd();
                }
            }
            catch (WebException ex)
            {
                using (WebResponse response = ex.Response)
                {
                    StreamReader sReader = new StreamReader(response.GetResponseStream());
                    responseJson = sReader.ReadToEnd();
                }
            }

            if (typeof(ReturnT) == typeof(string))
            {
                return((ReturnT)Convert.ChangeType(responseJson, typeof(ReturnT)));
            }

            return(fastJSON.JSON.Instance.ToObject <ReturnT>(responseJson));
        }
Exemplo n.º 3
0
        public ApigeeResponse PreformConnectionRequest(string entity1, string item, string verb, string entity2, string item2, HttpTools.RequestTypes request)
        {
            var path = "/" + entity1 + "/" + item + "/" + verb + "/" + entity2 + "/" + item2;

            var rawResults = PerformRequest <string>(path, request, null);

            //parse response
            var response = new ApigeeResponse(rawResults, true);

            return(response);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Performs a Request agianst the UserGridUrl + provided path
        /// </summary>
        /// <typeparam name="retrunT">Return Type</typeparam>
        /// <param name="path">Sub Path Of the Get Request</param>
        /// <returns>Object of Type T</returns>
        public retrunT PerformRequest <retrunT>(string path, HttpTools.RequestTypes method, object data)
        {
            string requestPath = BuildPath(path);

            return(IhttpTools.PerformJsonRequest <retrunT>(requestPath, method, data));
        }