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); }
//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)); }
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); }
/// <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)); }