Advertisement
Guest User

WebRequest.cs

a guest
May 31st, 2016
354
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.54 KB | None | 0 0
  1. namespace Assets.Scripts
  2. {
  3.    using System;
  4.    using System.Net;
  5.    using System.Net.Security;
  6.    using System.Text;
  7.  
  8.    /// <summary>
  9.    /// Web request using the WebClient class.
  10.    /// </summary>
  11.    /// <seealso cref="Assets.Scripts.WebRequest" />
  12.    internal class WebRequest
  13.    {
  14.       /// <summary>
  15.       /// The web client.
  16.       /// </summary>
  17.       private WebClient _client;
  18.  
  19.       /// <summary>
  20.       /// The error message.
  21.       /// </summary>
  22.       private string _error;
  23.  
  24.       /// <summary>
  25.       /// The is done flag.
  26.       /// </summary>
  27.       private bool _isDone;
  28.  
  29.       /// <summary>
  30.       /// The progress.
  31.       /// </summary>
  32.       private float _progress;
  33.  
  34.       /// <summary>
  35.       /// The text.
  36.       /// </summary>
  37.       private string _text;
  38.  
  39.       /// <summary>
  40.       /// Initializes a new instance of the <see cref="WebRequest"/> class.
  41.       /// </summary>
  42.       /// <param name="url">The URL.</param>
  43.       public WebRequest(string url)
  44.       {
  45.          this._client = new System.Net.WebClient();
  46.          this._client.DownloadProgressChanged += this.WebClientProgressChanged;
  47.          this._client.DownloadStringCompleted += this.WebClientDownloadCompleted;
  48.          this._client.DownloadStringAsync(new Uri(url));
  49.       }
  50.  
  51.       /// <summary>
  52.       /// Initializes a new instance of the <see cref="WebRequestDotNet"/> class.
  53.       /// </summary>
  54.       /// <param name="url">The URL.</param>
  55.       /// <param name="form">The form.</param>
  56.       public WebRequest(string url, UnityEngine.WWWForm form)
  57.       {
  58.          this._client = new System.Net.WebClient();
  59.          this._client.UploadProgressChanged += this.WebClientUploadProgressChanged;
  60.          this._client.UploadDataCompleted += this.WebClientUploadCompleted;
  61.  
  62.          this._client.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
  63.  
  64.          // Try to get the header from the Unity form
  65.          foreach (var header in form.headers)
  66.          {
  67.             const string ContentType = "Content-Type";
  68.             if (header.Key == ContentType)
  69.             {
  70.                string contentType = header.Value;
  71.                this._client.Headers.Remove(ContentType);
  72.                this._client.Headers[ContentType] = contentType;
  73.             }
  74.          }
  75.  
  76.          this._client.UploadDataAsync(new Uri(url), form.data);
  77.       }
  78.  
  79.       /// <summary>
  80.       /// Gets the error message. Returns null if there is no error.
  81.       /// </summary>
  82.       /// <value>
  83.       /// The error message.
  84.       /// </value>
  85.       public string Error
  86.       {
  87.          get
  88.          {
  89.             return this._error;
  90.          }
  91.       }
  92.  
  93.       /// <summary>
  94.       /// Gets a value indicating whether this request is done.
  95.       /// </summary>
  96.       /// <value>
  97.       ///   <c>true</c> if this instance is done; otherwise, <c>false</c>.
  98.       /// </value>
  99.       public bool IsDone
  100.       {
  101.          get
  102.          {
  103.             return this._isDone;
  104.          }
  105.       }
  106.  
  107.       /// <summary>
  108.       /// Gets the progress, 0 to 1.
  109.       /// </summary>
  110.       /// <value>
  111.       /// The progress.
  112.       /// </value>
  113.       public float Progress
  114.       {
  115.          get
  116.          {
  117.             return this._progress / 100.0f;
  118.          }
  119.       }
  120.  
  121.       /// <summary>
  122.       /// Gets the resulting text.
  123.       /// </summary>
  124.       /// <value>
  125.       /// The text.
  126.       /// </value>
  127.       public string Text
  128.       {
  129.          get
  130.          {
  131.             return this._text;
  132.          }
  133.       }
  134.  
  135.       /// <summary>
  136.       /// Called when the download is complete.
  137.       /// </summary>
  138.       /// <param name="sender">The sender.</param>
  139.       /// <param name="e">The <see cref="DownloadStringCompletedEventArgs"/> instance containing the event data.</param>
  140.       private void WebClientDownloadCompleted(object sender, DownloadStringCompletedEventArgs e)
  141.       {
  142.          if (e.Error != null)
  143.          {
  144.             this._error = e.Error.ToString();
  145.          }
  146.          else
  147.          {
  148.             this._text = e.Result;
  149.          }
  150.  
  151.          this._isDone = true;
  152.       }
  153.  
  154.       /// <summary>
  155.       /// Called when the progress changes.
  156.       /// </summary>
  157.       /// <param name="sender">The sender.</param>
  158.       /// <param name="e">The <see cref="DownloadProgressChangedEventArgs"/> instance containing the event data.</param>
  159.       private void WebClientProgressChanged(object sender, DownloadProgressChangedEventArgs e)
  160.       {
  161.          this._progress += e.ProgressPercentage;
  162.       }
  163.  
  164.       /// <summary>
  165.       /// Called when the upload is complete.
  166.       /// </summary>
  167.       /// <param name="sender">The sender.</param>
  168.       /// <param name="e">The <see cref="UploadValuesCompletedEventArgs"/> instance containing the event data.</param>
  169.       private void WebClientUploadCompleted(object sender, UploadDataCompletedEventArgs e)
  170.       {
  171.          if (e.Error != null)
  172.          {
  173.             this._error = e.Error.ToString();
  174.          }
  175.          else
  176.          {
  177.             this._text = Encoding.UTF8.GetString(e.Result);
  178.          }
  179.  
  180.          this._isDone = true;
  181.       }
  182.  
  183.       /// <summary>
  184.       /// Called when the upload progress changes.
  185.       /// </summary>
  186.       /// <param name="sender">The sender.</param>
  187.       /// <param name="e">The <see cref="UploadProgressChangedEventArgs"/> instance containing the event data.</param>
  188.       private void WebClientUploadProgressChanged(object sender, UploadProgressChangedEventArgs e)
  189.       {
  190.          this._progress += e.ProgressPercentage;
  191.       }
  192.    }
  193. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement