private string REST_Execute_WithPostData_JSON(string uri, string method, Dictionary<string, string> postdata)
{
HttpWebRequest restclient = (HttpWebRequest)this.REST_GetClient(uri);
restclient.Method = method;
restclient.Accept = "application/json";
List<string> encoded_postdata_pairs = new List<string>();
foreach (string key in postdata.Keys)
{
encoded_postdata_pairs.Add(key + "=" + HttpUtility.UrlPathEncode(postdata[key]));
}
if (encoded_postdata_pairs.Count() > 0)
{
byte[] byteArray = Encoding.UTF8.GetBytes(string.Join("&", encoded_postdata_pairs.ToArray()));
restclient.ContentType = "application/x-www-form-urlencoded";
restclient.ContentLength = byteArray.Length;
Stream dataStream = restclient.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
}
try
{
HttpWebResponse response = (HttpWebResponse)restclient.GetResponse();
if ((int)response.StatusCode < 200 || (int)response.StatusCode > 299)
{
throw new Exception("Bad status code: " + response.StatusCode);
}
StreamReader reader = new StreamReader(response.GetResponseStream());
string json = reader.ReadToEnd();
reader.Close();
response.Close();
return json;
}
catch (WebException e)
{
HttpWebResponse response = (HttpWebResponse)e.Response;
StreamReader reader = new StreamReader(response.GetResponseStream());
string json = reader.ReadToEnd();
reader.Close();
response.Close();
throw new Exception("REST request for " + uri + " failed with response: " + (int)response.StatusCode + " " + response.StatusDescription + ", the body of the response was: " + json);
}
}