andrew4582

HttpClient Core.Net

Oct 1st, 2011
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.69 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Net;
  4.  
  5. namespace Core.Net {
  6.     public class HttpClient {
  7.         public event EventHandler<ProgressEventArgs> ProgressAvailable = delegate { };
  8.         public event EventHandler<WebRequestEventArgs> SendingRequest = delegate { };
  9.  
  10.         public string UserAgent {
  11.             get;
  12.             set;
  13.         }
  14.  
  15.         public WebRequest CreateRequest(Uri uri, bool acceptCompression) {
  16.             WebRequest request = WebRequest.Create(uri);
  17.             InitializeRequest(request, acceptCompression);
  18.             return request;
  19.         }
  20.  
  21.         public void InitializeRequest(WebRequest request, bool acceptCompression) {
  22.             var httpRequest = request as HttpWebRequest;
  23.             if (httpRequest != null) {
  24.                 httpRequest.UserAgent = UserAgent;
  25.                 if (acceptCompression) {
  26.                     httpRequest.Headers[HttpRequestHeader.AcceptEncoding] = "gzip, deflate";
  27.                     httpRequest.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;
  28.                 }
  29.             }
  30.  
  31.             request.UseDefaultCredentials = true;
  32.             if (request.Proxy != null) {
  33.                 // If we are going through a proxy then just set the default credentials
  34.                 request.Proxy.Credentials = CredentialCache.DefaultCredentials;
  35.             }
  36.  
  37.             // giving clients a chance to examine/modify the request object before the request actually goes out.
  38.             RaiseSendingRequest(request);
  39.         }
  40.  
  41.         public Uri GetRedirectedUri(Uri uri) {
  42.             WebRequest request = CreateRequest(uri, acceptCompression: false);
  43.             using (WebResponse response = request.GetResponse()) {
  44.                 if (response == null) {
  45.                     return null;
  46.                 }
  47.                 return response.ResponseUri;
  48.             }
  49.         }
  50.  
  51.         [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")]
  52.         public byte[] DownloadData(Uri uri) {
  53.             const int ChunkSize = 1024 * 4; // 4KB
  54.  
  55.             byte[] buffer = null;
  56.  
  57.             // we don't want to enable compression when downloading packages
  58.             WebRequest request = CreateRequest(uri, acceptCompression: false);
  59.             using (var response = request.GetResponse()) {
  60.  
  61.                 // total response length
  62.                 int length = (int)response.ContentLength;
  63.                 buffer = new byte[length];
  64.  
  65.                 // We read the response stream chunk by chunk (each chunk is 4KB).
  66.                 // After reading each chunk, we report the progress based on the total number bytes read so far.
  67.                 int totalReadSoFar = 0;
  68.                 using (Stream stream = response.GetResponseStream()) {
  69.                     while (totalReadSoFar < length) {
  70.                         int bytesRead = stream.Read(buffer, totalReadSoFar, Math.Min(length - totalReadSoFar, ChunkSize));
  71.                         if (bytesRead == 0) {
  72.                             break;
  73.                         }
  74.                         else {
  75.                             totalReadSoFar += bytesRead;
  76.                             OnProgressAvailable((totalReadSoFar * 100) / length);
  77.                         }
  78.                     }
  79.                 }
  80.             }
  81.  
  82.             return buffer;
  83.         }
  84.  
  85.         private void OnProgressAvailable(int percentage) {
  86.             ProgressAvailable(this, new ProgressEventArgs(percentage));
  87.         }
  88.  
  89.         private void RaiseSendingRequest(WebRequest webRequest) {
  90.             SendingRequest(this, new WebRequestEventArgs(webRequest));
  91.         }
  92.     }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment