Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Xml;
- using System.Net;
- using System.Web;
- using System.IO;
- namespace NetBot1
- {
- public class Browser
- {
- private CookieContainer _cookies;
- private HttpWebRequest _request;
- private HttpWebResponse _response;
- /// <summary>
- /// Constructor of the object Browser
- /// </summary>
- public Browser()
- {
- _cookies = new CookieContainer();
- _request = null;
- _response = null;
- }
- /// <summary>
- /// Creates a request, which later can be sent
- /// </summary>
- /// <param name="url">The URL the request is intended to</param>
- /// <param name="referer">The URL of the page who referenced (may modify the result)</param>
- /// <returns></returns>
- public void CreateRequest(string url, string referer)
- {
- _request = (HttpWebRequest)WebRequest.Create(url); //creating request based on the url
- _request.Timeout = 30000; //given in milliseconds
- _request.UserAgent = "a C# bot screwing you"; // :)
- _request.CookieContainer = _cookies; //saving cookies
- _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, */*";
- //a list of what type of files do I accept, dont worry this wont override those mentioned in .htaccess in the server
- _request.Referer = referer; //who referenced, it may change the result
- _request.ContentType = "application/x-www-form-urlencoded"; //don't ask
- }
- /// <summary>
- /// It submit's the this.Request object, and creates the this.Response object
- /// </summary>
- public void SubmitReques()
- {
- this.SubmitRequest("");
- }
- /// <summary>
- /// It submit's the this.Request object, and creates the this.Response object, using the post data specifiesd
- /// </summary>
- /// <param name="postData">The post body</param>
- public void SubmitRequest(string postData)
- {
- if (_request != null)
- {
- if (postData != "")
- {
- byte[] lbPostBuffer = System.Text.Encoding.GetEncoding(1252).GetBytes(postData);
- //1252 is the windows standard
- //the server wont give you the information just yet, it gives you a reference to a stream where the data should be written
- _request.ContentLength = lbPostBuffer.Length;
- Stream postDataStram = _request.GetRequestStream();
- postDataStram.Write(lbPostBuffer, 0, lbPostBuffer.Length);
- postDataStram.Close();
- }
- if (_response != null) _response.Close(); //handling automatically
- _response = (HttpWebResponse)_request.GetResponse(); // finally getting the response
- }
- else throw new NullReferenceException("No request was created. Use this.CreateRequest() to create one."); //if you see this while running than screw you
- }
- /// <summary>
- /// Returns a string containig the body of the response. Save this to a .HTML file
- /// </summary>
- /// <returns>Contains the reponse body</returns>
- public new string ToString()
- {
- if (_response != null)
- {
- Encoding encoding = Encoding.GetEncoding("iso-8859-2"); // used by Lamer, I know, this is lame
- //getting a reference to a stream which contains our data
- StreamReader responseStream = new StreamReader(_response.GetResponseStream(), encoding);
- string lcHtml = responseStream.ReadToEnd(); //this will read all at once
- responseStream.Close();
- return lcHtml;
- }
- else throw new NullReferenceException("No Response object was created, use this.SubmitRequest() to create one.");
- }
- //destructor of this class
- ~Browser()
- {
- if (_response != null) _response.Close(); //if it wasn't closed before, now we'll do it
- }
- #region Get-Setters
- public CookieContainer Cookies
- {
- get
- {
- return _cookies;
- }
- set
- {
- _cookies = value;
- }
- }
- public HttpWebRequest Request
- {
- get
- {
- return _request;
- }
- set
- {
- _request = value;
- }
- }
- public HttpWebResponse Response
- {
- get
- {
- return _response;
- }
- set
- {
- _response = value;
- }
- }
- #endregion
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement