private static RequestResponse MakeRequest(RequestConfig config) { var req = (HttpWebRequest)WebRequest.Create(config.URI); req.Method = config.Method.Method; req.ContentType = config.ContentType; if (config.Method.Method == HttpMethod.Post.Method && config.Body != null) { var stOut = new StreamWriter(req.GetRequestStream()); stOut.Write(config.Body); stOut.Close(); } var returnVal = new RequestResponse(); try { var webResponse = (HttpWebResponse)req.GetResponse(); returnVal.StatusCode = webResponse.StatusCode; if (webResponse.StatusCode == HttpStatusCode.OK) { Stream responseStream = req.GetResponse().GetResponseStream(); if (responseStream != null) { var reader = new StreamReader(responseStream); returnVal.ReturnValue = reader.ReadToEnd(); } } } catch (Exception ex) { returnVal.ReturnValue = ex.Message; returnVal.StatusCode = HttpStatusCode.NotFound; } return returnVal; }
public static RequestResponse Get(string uri, string body) { var config = new RequestConfig { URI = uri, Method = HttpMethod.Get, ContentType = "text/json", Body = body }; return MakeRequest(config); }