Advertisement
Guest User

Sinistra_D32

a guest
Aug 12th, 2009
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.15 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Xml;
  6. using System.Net;
  7. using System.Web;
  8. using System.IO;
  9.  
  10. namespace NetBot1
  11. {
  12.     public class Browser
  13.     {
  14.         private CookieContainer _cookies;
  15.         private HttpWebRequest _request;
  16.         private HttpWebResponse _response;
  17.  
  18.         /// <summary>
  19.         /// Constructor of the object Browser
  20.         /// </summary>
  21.         public Browser()
  22.         {
  23.             _cookies = new CookieContainer();
  24.             _request = null;
  25.             _response = null;
  26.         }
  27.  
  28.         /// <summary>
  29.         /// Creates a request, which later can be sent
  30.         /// </summary>
  31.         /// <param name="url">The URL the request is intended to</param>
  32.         /// <param name="referer">The URL of the page who referenced (may modify the result)</param>
  33.         /// <returns></returns>
  34.         public void CreateRequest(string url, string referer)
  35.         {
  36.             _request = (HttpWebRequest)WebRequest.Create(url); //creating request based on the url
  37.             _request.Timeout = 30000; //given in milliseconds
  38.             _request.UserAgent = "a C# bot screwing you"; // :)
  39.             _request.CookieContainer = _cookies; //saving cookies
  40.             _request.Accept = "image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/x-ms-application, application/x-ms-xbap, application/vnd.ms-xpsdocument, application/xaml+xml, application/x-silverlight, */*";
  41.             //a list of what type of files do I accept, dont worry this wont override those mentioned in .htaccess in the server
  42.             _request.Referer = referer; //who referenced, it may change the result
  43.             _request.ContentType = "application/x-www-form-urlencoded"; //don't ask
  44.         }
  45.  
  46.         /// <summary>
  47.         /// It submit's the this.Request object, and creates the this.Response object
  48.         /// </summary>
  49.         public void SubmitReques()
  50.         {
  51.             this.SubmitRequest("");
  52.         }
  53.  
  54.         /// <summary>
  55.         /// It submit's the this.Request object, and creates the this.Response object, using the post data specifiesd
  56.         /// </summary>
  57.         /// <param name="postData">The post body</param>
  58.         public void SubmitRequest(string postData)
  59.         {
  60.             if (_request != null)
  61.             {
  62.                 if (postData != "")
  63.                 {
  64.                     byte[] lbPostBuffer = System.Text.Encoding.GetEncoding(1252).GetBytes(postData);
  65.                     //1252 is the windows standard
  66.                     //the server wont give you the information just yet, it gives you a reference to a stream where the data should be written
  67.                     _request.ContentLength = lbPostBuffer.Length;
  68.                     Stream postDataStram = _request.GetRequestStream();
  69.                     postDataStram.Write(lbPostBuffer, 0, lbPostBuffer.Length);
  70.                     postDataStram.Close();
  71.                 }
  72.                 if (_response != null) _response.Close(); //handling automatically
  73.                 _response = (HttpWebResponse)_request.GetResponse();  // finally getting the response
  74.             }
  75.             else throw new NullReferenceException("No request was created. Use this.CreateRequest() to create one."); //if you see this while running than screw you
  76.         }
  77.  
  78.         /// <summary>
  79.         /// Returns a string containig the body of the response. Save this to a .HTML file
  80.         /// </summary>
  81.         /// <returns>Contains the reponse body</returns>
  82.         public new string ToString()
  83.         {
  84.             if (_response != null)
  85.             {
  86.                 Encoding encoding = Encoding.GetEncoding("iso-8859-2");  // used by Lamer, I know, this is lame
  87.                 //getting a reference to a stream which contains our data
  88.                 StreamReader responseStream = new StreamReader(_response.GetResponseStream(), encoding);
  89.                 string lcHtml = responseStream.ReadToEnd(); //this will read all at once
  90.                 responseStream.Close();
  91.                 return lcHtml;
  92.             }
  93.             else throw new NullReferenceException("No Response object was created, use this.SubmitRequest() to create one.");
  94.         }
  95.  
  96.         //destructor of this class
  97.         ~Browser()
  98.         {
  99.             if (_response != null) _response.Close(); //if it wasn't closed before, now we'll do it
  100.         }
  101.  
  102.         #region Get-Setters
  103.         public CookieContainer Cookies
  104.         {
  105.             get
  106.             {
  107.                 return _cookies;
  108.             }
  109.             set
  110.             {
  111.                 _cookies = value;
  112.             }
  113.         }
  114.  
  115.         public HttpWebRequest Request
  116.         {
  117.             get
  118.             {
  119.                 return _request;
  120.             }
  121.             set
  122.             {
  123.                 _request = value;
  124.             }
  125.         }
  126.  
  127.         public HttpWebResponse Response
  128.         {
  129.             get
  130.             {
  131.                 return _response;
  132.             }
  133.             set
  134.             {
  135.                 _response = value;
  136.             }
  137.         }
  138.         #endregion
  139.     }
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement