daily pastebin goal
57%
SHARE
TWEET

MultiformRequest

a guest Mar 29th, 2014 290 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. namespace Zelka.Apis
  2. {
  3.     using System;
  4.     using System.Collections.Generic;
  5.     using System.IO;
  6.     using System.Net;
  7.     using System.Text;
  8.  
  9.     public class MultiformRequest
  10.     {
  11.         private const string DefaultUserAgent = "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.154 Safari/537.36 OPR/20.0.1387.82";
  12.  
  13.         private readonly string Boundary;
  14.  
  15.         public MultiformRequest(string url)
  16.         {
  17.             this.Url = url;
  18.             this.Boundary = GetNewBoundary();
  19.             this.Stream = new MemoryStream();
  20.             this.RequestParameters = new List<IRequestParameter>();
  21.             this.Encoding = Encoding.UTF8;
  22.             this.UserAgent = DefaultUserAgent;
  23.             this.CookieContainer = new CookieContainer();
  24.         }
  25.  
  26.         public string Url { get; set; }
  27.  
  28.         public Encoding Encoding { get; set; }
  29.  
  30.         private Stream Stream { get; set; }
  31.  
  32.         public string UserAgent { get; set; }
  33.  
  34.         public CookieContainer CookieContainer { get; set; }
  35.  
  36.         public List<IRequestParameter> RequestParameters { get; set; }
  37.  
  38.         public HttpWebResponse GetResponse()
  39.         {
  40.             this.AddRequestParameters();
  41.             this.AddRequestFooter();
  42.  
  43.             byte[] requestData = this.GetDataFromStream();
  44.  
  45.             HttpWebRequest request = this.BuildRequest(requestData);
  46.  
  47.             return request.GetResponse() as HttpWebResponse;
  48.         }
  49.  
  50.         private HttpWebRequest BuildRequest(byte[] requestData)
  51.         {
  52.             HttpWebRequest request = WebRequest.Create(this.Url) as HttpWebRequest;
  53.  
  54.             request.Method = "POST";
  55.             request.ContentType = "multipart/form-data; boundary=" + this.Boundary;
  56.             request.UserAgent = this.UserAgent;
  57.             request.CookieContainer = this.CookieContainer;
  58.             request.ContentLength = requestData.Length;
  59.  
  60.             WriteDataToStream(requestData, request.GetRequestStream());
  61.  
  62.             return request;
  63.         }
  64.  
  65.         private static void WriteDataToStream(byte[] requestData, Stream stream)
  66.         {
  67.             stream.Write(requestData, 0, requestData.Length);
  68.             stream.Close();
  69.         }
  70.  
  71.         private byte[] GetDataFromStream()
  72.         {
  73.             this.Stream.Position = 0;
  74.             byte[] formData = new byte[this.Stream.Length];
  75.             this.Stream.Read(formData, 0, formData.Length);
  76.             this.Stream.Close();
  77.             return formData;
  78.         }
  79.  
  80.         private void AddRequestParameters()
  81.         {
  82.             bool needsNewLine = false;
  83.  
  84.             foreach (IRequestParameter parameter in this.RequestParameters)
  85.             {
  86.                 if (needsNewLine)
  87.                 {
  88.                     this.Stream.Write(this.Encoding.GetBytes("\r\n"), 0, this.Encoding.GetByteCount("\r\n"));
  89.                 }
  90.  
  91.                 needsNewLine = true;
  92.  
  93.                 parameter.WriteToStream(this.Stream, this.Boundary, this.Encoding);
  94.             }
  95.         }
  96.  
  97.         private void AddRequestFooter()
  98.         {
  99.             string footer = "\r\n--" + this.Boundary + "--\r\n";
  100.             this.Stream.Write(this.Encoding.GetBytes(footer), 0, this.Encoding.GetByteCount(footer));
  101.         }
  102.  
  103.         private static string GetNewBoundary()
  104.         {
  105.             return string.Format("----------{0:N}", Guid.NewGuid());
  106.         }
  107.     }
  108. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top