Difference between revisions of "Template:Base Code for Consuming API Using CSharp"

From Updox API
Jump to: navigation, search
(Created page with " private void SendReceiveJSON(string json, string url) { var httpWebRequest = WebRequest.Create(url); httpWebRequest.ContentType = "text/jso...")
 
Line 1: Line 1:
    private void SendReceiveJSON(string json, string url)
+
  private void SendReceiveJSON(string json, string url)
        {
+
      {
            var httpWebRequest = WebRequest.Create(url);
+
          var httpWebRequest = WebRequest.Create(url);
            httpWebRequest.ContentType = "text/json";
+
          httpWebRequest.ContentType = "text/json";
            httpWebRequest.Method = "POST";
+
          httpWebRequest.Method = "POST";
            using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
+
          using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
            {
+
          {
                streamWriter.Write(json);
+
              streamWriter.Write(json);
                streamWriter.Flush();
+
              streamWriter.Flush();
                streamWriter.Close();
+
              streamWriter.Close();
                var httpResponse = (HttpWebResponse) httpWebRequest.GetResponse();
+
              var httpResponse = (HttpWebResponse) httpWebRequest.GetResponse();
                using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
+
              using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
                {
+
              {
                    var result = streamReader.ReadToEnd();
+
                  var result = streamReader.ReadToEnd();
                }
+
              }
            }
+
          }
        }
+
      }

Revision as of 17:08, 15 November 2013

 private void SendReceiveJSON(string json, string url)
     {
         var httpWebRequest = WebRequest.Create(url);
         httpWebRequest.ContentType = "text/json";
         httpWebRequest.Method = "POST";
         using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
         {
             streamWriter.Write(json);
             streamWriter.Flush();
             streamWriter.Close();
             var httpResponse = (HttpWebResponse) httpWebRequest.GetResponse();
             using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
             {
                 var result = streamReader.ReadToEnd();
             }
         }
     }