[chronojump] Added json POST example



commit 57fde940d691d0520b4f85507dbebbd9da4bdaa6
Author: Xavier de Blas <xaviblas gmail com>
Date:   Sat Jan 17 01:10:35 2015 +0100

    Added json POST example

 testing-stuff/json_post.cs |   93 +++++++++++++++++++++++--------------------
 1 files changed, 50 insertions(+), 43 deletions(-)
---
diff --git a/testing-stuff/json_post.cs b/testing-stuff/json_post.cs
index 2b0cdb8..efaa19b 100644
--- a/testing-stuff/json_post.cs
+++ b/testing-stuff/json_post.cs
@@ -9,51 +9,58 @@ using System.Json;
 
 public class HelloWorld
 {
-    static public byte[] readFile(string filePath)
-    {
-       return System.IO.File.ReadAllBytes(filePath); 
-    }
+       static private string getLogsDir() 
+       {
+               return Path.Combine(
+                               Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
+                               "Chronojump" + Path.DirectorySeparatorChar + "logs");
+       }
 
-    static public void Main()
-    {
-            string filePath = "/home/carles/ip.txt";
+       static private byte[] readFile(string filePath)
+       {
+               return System.IO.File.ReadAllBytes(filePath); 
+       }
 
-            // Create a request using a URL that can receive a post. 
-            WebRequest request = WebRequest.Create ("http://chronojump.org:8080/api/backtrace";);
-            // Set the Method property of the request to POST.
-            request.Method = "POST";
-            // Create POST data and convert it to a byte array.
-            byte[] byteArray = readFile(filePath);
-            // Set the ContentType property of the WebRequest.
-            request.ContentType = "application/x-www-form-urlencoded";
-            // Set the ContentLength property of the WebRequest.
-            request.ContentLength = byteArray.Length;
-            // Get the request stream.
-            Stream dataStream = request.GetRequestStream ();
-            // Write the data to the request stream.
-            dataStream.Write (byteArray, 0, byteArray.Length);
-            // Close the Stream object.
-            dataStream.Close ();
-            // Get the response.
-            WebResponse response = request.GetResponse ();
-            // Display the status.
-            Console.WriteLine (((HttpWebResponse)response).StatusDescription);
-            // Get the stream containing content returned by the server.
-            dataStream = response.GetResponseStream ();
-            // Open the stream using a StreamReader for easy access.
-            StreamReader reader = new StreamReader (dataStream);
-            // Read the content.
-            string responseFromServer = reader.ReadToEnd ();
-            // Display the content.
-            Console.WriteLine (responseFromServer);
-            // Clean up the streams.
-            reader.Close ();
-            dataStream.Close ();
-            response.Close ();    
+       static public void Main()
+       {
+               string filePath = getLogsDir() + Path.DirectorySeparatorChar + "log_chronojump.txt";
 
+               // Create a request using a URL that can receive a post. 
+               WebRequest request = WebRequest.Create ("http://chronojump.org:8080/api/backtrace";);
+               // Set the Method property of the request to POST.
+               request.Method = "POST";
+               // Create POST data and convert it to a byte array.
+               byte[] byteArray = readFile(filePath);
+               // Set the ContentType property of the WebRequest.
+               request.ContentType = "application/x-www-form-urlencoded";
+               // Set the ContentLength property of the WebRequest.
+               request.ContentLength = byteArray.Length;
+               // Get the request stream.
+               Stream dataStream = request.GetRequestStream ();
+               // Write the data to the request stream.
+               dataStream.Write (byteArray, 0, byteArray.Length);
+               // Close the Stream object.
+               dataStream.Close ();
+               // Get the response.
+               WebResponse response = request.GetResponse ();
+               // Display the status.
+               Console.WriteLine (((HttpWebResponse)response).StatusDescription);
+               // Get the stream containing content returned by the server.
+               dataStream = response.GetResponseStream ();
+               // Open the stream using a StreamReader for easy access.
+               StreamReader reader = new StreamReader (dataStream);
+               // Read the content.
+               string responseFromServer = reader.ReadToEnd ();
+               // Display the content.
+               Console.WriteLine (responseFromServer);
+               // Clean up the streams.
+               reader.Close ();
+               dataStream.Close ();
+               response.Close ();    
 
-            JsonValue result = JsonValue.Parse(responseFromServer);
-            string crash_id = result["crash_id"];
-            Console.WriteLine("crash_id: "+ crash_id);
-    }
+
+               JsonValue result = JsonValue.Parse(responseFromServer);
+               string crash_id = result["crash_id"];
+               Console.WriteLine("crash_id: "+ crash_id);
+       }
 }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]