Advertisement
vitareinforce

httploaddatac#

Feb 5th, 2018
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.58 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Net;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9.  
  10. namespace HMD_Holo
  11. {
  12.     class HTTPLoadData
  13.     {
  14.         public string data;
  15.         HttpWebRequest webRequest;
  16.         public HTTPLoadData(string url)
  17.         {
  18.             try
  19.             {
  20.                 webRequest = (HttpWebRequest)WebRequest.Create(url);
  21.                 webRequest.BeginGetResponse(new AsyncCallback(FinishWebRequest), webRequest);
  22.             }
  23.             catch (WebException w)
  24.             {
  25.                 Debug.WriteLine("WWW Request Error : " + w.Message.ToString());
  26.             }
  27.             catch (Exception e)
  28.             {
  29.                 Debug.WriteLine("Unknown Request Error : " + e.Message.ToString());
  30.             }
  31.  
  32.         }
  33.  
  34.         private void FinishWebRequest(IAsyncResult result)
  35.         {
  36.             try
  37.             {
  38.                 HttpWebResponse response = (result.AsyncState as HttpWebRequest).EndGetResponse(result) as HttpWebResponse;
  39.                 Stream dataStream = response.GetResponseStream();
  40.                 StreamReader reader = new StreamReader(dataStream);
  41.                 data = reader.ReadToEnd();
  42.             }
  43.             catch (WebException w)
  44.             {
  45.                 Debug.WriteLine("WWW Response Error : " + w.Message.ToString());
  46.             }
  47.             catch (Exception e)
  48.             {
  49.                 Debug.WriteLine("Unknown Response Error : " + e.Message.ToString());
  50.             }
  51.         }
  52.     }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement