Advertisement
starbeamrainbowlabs

ErrorReportingClient.cs

Apr 11th, 2016
366
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.66 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Net;
  4. using System.Text;
  5.  
  6. public class Program
  7. {
  8.     public static readonly string Name = "Dividing program";
  9.     public static readonly string Version = "0.1";
  10.  
  11.     public static string ProgramId
  12.     {
  13.         get { return string.Format("{0}/{1}", Name, Version); }
  14.     }
  15.  
  16.     public static int Main(string[] args)
  17.     {
  18.         float a = 0, b = 0, c = 0;
  19.  
  20.         try
  21.         {
  22.             Console.WriteLine(ProgramId);
  23.             Console.WriteLine("This program divides one number by another.");
  24.  
  25.             Console.Write("Enter number 1: ");
  26.             a = float.Parse(Console.ReadLine());
  27.             Console.Write("Enter number 2: ");
  28.             b = float.Parse(Console.ReadLine());
  29.  
  30.             c = a / b;
  31.             Console.WriteLine("Number 1 divided by number 2 is {0}.", c);
  32.         }
  33.         catch(Exception error)
  34.         {
  35.             Console.WriteLine("An error has occurred!");
  36.             Console.Write("Would you like to report it? [Y/n] ");
  37.  
  38.             bool sendReport = false;
  39.             while(true)
  40.             {
  41.                 ConsoleKey key = Console.ReadKey().Key;
  42.                 if (key == ConsoleKey.Y) {
  43.                     sendReport = true;
  44.                     break;
  45.                 }
  46.                 else if (key == ConsoleKey.N)
  47.                     break;
  48.             }
  49.             Console.WriteLine();
  50.  
  51.             if(!sendReport)
  52.             {
  53.                 Console.WriteLine("No report has been sent.");
  54.                 Console.WriteLine("Press any key to exit.");
  55.                 Console.ReadKey(true);
  56.                 return 1;
  57.             }
  58.  
  59.             Console.Write("Collecting data - ");
  60.             MemoryStream dataStream  = new MemoryStream();
  61.             StreamWriter dataIn = new StreamWriter(dataStream);
  62.             dataIn.WriteLine("***** Error Report *****");
  63.             dataIn.WriteLine(error.ToString());
  64.             dataIn.WriteLine();
  65.             dataIn.WriteLine("*** Details ***");
  66.             dataIn.WriteLine("a: {0}", a);
  67.             dataIn.WriteLine("b: {0}", b);
  68.             dataIn.WriteLine("c: {0}", c);
  69.             dataIn.Flush();
  70.  
  71.             dataStream.Seek(0, SeekOrigin.Begin);
  72.             string errorReport = new StreamReader(dataStream).ReadToEnd();
  73.             Console.WriteLine("done");
  74.  
  75.             Console.Write("Sending report - ");
  76.             HttpWebRequest reportSender = WebRequest.CreateHttp("https://starbeamrainbowlabs.com/reportSender.php");
  77.             reportSender.Method = "POST";
  78.             byte[] payload = Encoding.UTF8.GetBytes(errorReport);
  79.             reportSender.ContentType = "text/plain";
  80.             reportSender.ContentLength = payload.Length;
  81.             reportSender.UserAgent = ProgramId;
  82.             Stream requestStream = reportSender.GetRequestStream();
  83.             requestStream.Write(payload, 0, payload.Length);
  84.             requestStream.Close();
  85.  
  86.             WebResponse reportResponse = reportSender.GetResponse();
  87.             Console.WriteLine("done");
  88.             Console.WriteLine("Server response: {0}", ((HttpWebResponse)reportResponse).StatusDescription);
  89.             Console.WriteLine("Press any key to exit.");
  90.             Console.ReadKey(true);
  91.             return 1;
  92.         }
  93.  
  94.         return 0;
  95.     }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement