Advertisement
Guest User

Untitled

a guest
Oct 21st, 2016
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.99 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Net;
  4. using System.Text;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7.  
  8. namespace HttpRequestExceptions
  9. {
  10.     class Program
  11.     {
  12.         private Timer connectionTimer; // Normaly we use DispatcherTimer
  13.         private int connectionTimerInterval = 1000; // ms
  14.  
  15.         // Normally there would be valid url (something like this
  16.         // http://192.168.1.2/goform/ReadWrite), but right now we will simulate
  17.         // request timeout with malformed url.
  18.         const string url = "malformedURL";
  19.  
  20.         // Connection query just for completeness.
  21.         // Right now it does not affect exceptions.
  22.         readonly string connectedQuery = "redirect=/response.asp&variable=controlxp.axes.Running&value=none&read=1";
  23.  
  24.         public Program()
  25.         {
  26.             connectionTimer = new Timer(_ => ConnectionTimer_Tick(), null, 0, connectionTimerInterval);
  27.         }
  28.  
  29.         private async void ConnectionTimer_Tick()
  30.         {
  31.             connectionTimer.Change(Timeout.Infinite, Timeout.Infinite);
  32.             await CheckConnection();
  33.             connectionTimer.Change(connectionTimerInterval, connectionTimerInterval);
  34.         }
  35.  
  36.         private async Task CheckConnection()
  37.         {
  38.             try
  39.             {
  40.                 // Right now we don't care about content of response.
  41.                 // We only care if we got something.
  42.                 string response = await SendRequestAsync(connectedQuery);
  43.             }
  44.             catch (Exception ex) // For now we will catch everything.
  45.             {
  46.                 // Normaly this code is used in WPF application.
  47.                 System.Console.WriteLine("Set controls enabled to false");
  48.             }
  49.         }
  50.  
  51.         // Asynchronously sends HttpRequest with given data.
  52.         // Here Exceptions are thrown. Basically any exception from this
  53.         // method we don't care about and don't want to see in output window.
  54.         private async Task<string> SendRequestAsync(string postData)
  55.         {
  56.             HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
  57.             byte[] data = Encoding.ASCII.GetBytes(postData);
  58.  
  59.             request.Method = "POST";
  60.             request.ContentType = "application/x-www-form-urlencoded";
  61.             request.ContentLength = data.Length;
  62.  
  63.             using (Stream stream = await request.GetRequestStreamAsync())
  64.             {
  65.                 await stream.WriteAsync(data, 0, data.Length);
  66.             }
  67.  
  68.             string responseString;
  69.  
  70.             using (WebResponse response = await request.GetResponseAsync())
  71.             using (StreamReader reader = new StreamReader(response.GetResponseStream()))
  72.             {
  73.                 responseString = await reader.ReadToEndAsync();
  74.             }
  75.  
  76.             return responseString;
  77.         }
  78.  
  79.         static void Main(string[] args)
  80.         {
  81.             Program p = new Program();
  82.  
  83.             System.Console.ReadLine();
  84.         }
  85.     }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement