[C# .NET] HTTP WRAPPER

Discussion in 'Code Snippets and Tutorials' started by zav75, Aug 10, 2008.

  1. zav75

    zav75 Level I

    Joined:
    Feb 28, 2007
    Messages:
    76
    Likes Received:
    6
    Location:
    Canada, Province of Québec
    This is a wrapper that I made for my autobuyer. It mimic firefox . For the moment it's not 100% optimized, but it still fast and probably safer than any other wrapper. Why ? Bacause it's more like a little browser than a simple wrapper. I won't enter the details, but I have made it look the more I can like firefox. It still can be improved if you have any suggestion go ahead.

    [justify]Features[/justify]

    • Gzip/deflate
    • Download haggle's image
    • Manage the references
    • Manage the cookies
    • Can download many kind of ressources as JPG/SWF/PNG/CSS/JS etc.. to fake firefox's behavior
    • Simple methods interface



    Code (Text):
    1.  
    2. using System;
    3. using System.Collections.Generic;
    4. using System.Text;
    5. using System.Net.Sockets;
    6. using System.Drawing;
    7. using System.Drawing.Imaging;
    8. using System.IO;
    9. using System.Net;
    10. using System.ComponentModel;
    11. using System.Data;
    12. using System.Threading;
    13. using System.Collections;
    14. using System.IO.Compression;
    15. /*
    16.  * Author : Zav75
    17.  *
    18.  * Purpose : This is a Wrapper for HTTP requests
    19.  * this was made specially for the site of neopets.com
    20.  * This should be useful to make an autobuyer.
    21.  *
    22.  * Contact me : zavier86@hotmail.com
    23.  *
    24.  * */
    25. namespace AutoBuyer
    26. {
    27.  
    28.     /// <summary>
    29.     /// This class has as a goal to emulate someone who will navigate in neopet
    30.     /// with firefox. To do it propetly it is needed that we download everything
    31.     /// that firefox download, all the images and javaScripts. Probly this class
    32.     /// can be used for any other site and you would not be seen.
    33.     /// </summary>
    34.     class WrapperHTTP
    35.     {
    36.         static private string _referer = "http://www.neopets.com/hi.phtml";
    37.         static private CookieContainer _cookieJar = null;
    38.         static private bool _isLoggedIn = false;
    39.  
    40.         private struct AcceptHeader
    41.         {
    42.  
    43.             public const String JPG = "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,*/*;q=0.8";
    44.             public const String PNG = "image/png,image/*;q=0.8,*/*;q=0.5";
    45.             public const String HTML = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
    46.             public const String CSS = "text/css,*/*;q=0.1";
    47.             public const String JS = "*/*";
    48.             public const String PHP = "*/*";
    49.             public const String CAPCHA_SHOW = "image/png,image/*;q=0.8,*/*;q=0.5,en;q=0.3";
    50.             public const String GIF = "image/png,image/*;q=0.8,*/*;q=0.5";
    51.             public const String APP_X_SWF = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
    52.         };
    53.  
    54.  
    55.         /// <summary>
    56.         /// Constructor
    57.         /// </summary>
    58.         public WrapperHTTP()
    59.         {
    60.         }
    61.         /// <summary>
    62.         /// This method fake a restocker fast haggling
    63.         /// </summary>
    64.         /// <param name="offer"></param>
    65.         /// <returns></returns>
    66.         static private string smartHaggle(string offer) {
    67.             //ie: offer = 15789
    68.             string newOffer = "";
    69.             string digit = "";
    70.             newOffer = offer.Substring(0,1);// ie :1
    71.             digit = offer.Substring(1, 1);// ie : 5
    72.  
    73.             for (int i = 1; i < offer.Length; i++)
    74.             {
    75.                 newOffer += digit;
    76.  
    77.                 //if the fast gaggling is lower than what the merchant ask
    78.                 if ((int.Parse(newOffer) < int.Parse(offer)) && (i == offer.Length-1))//ie: 15555 < 15789
    79.                 {
    80.                     int tmp =  int.Parse(digit) ;
    81.                     tmp++;
    82.                     digit = tmp.ToString();//we increment the digit
    83.                     i = 0;
    84.                     newOffer = offer.Substring(0, 1);
    85.                 }
    86.             }
    87.  
    88.  
    89.  
    90.             return newOffer;
    91.         }
    92.  
    93.         /// <summary>
    94.         /// This method buys an item from a shop
    95.         /// </summary>
    96.         /// <param name="XY">The coordinnates from the picture</param>
    97.         /// <param name="offer"></param>
    98.         /// <param name="referer"></param>
    99.         /// <returns></returns>
    100.         static public bool buy(string XY, string offer, string referer)
    101.         {
    102.             bool reponse = false;
    103.             int lenght;
    104.             String[] tmp = offer.Split(',');
    105.  
    106.             offer = "";
    107.             for (int p = 0; p < tmp.Length; p++)
    108.                 offer += tmp[p];
    109.  
    110.  
    111.  
    112.             offer = smartHaggle(offer);
    113.             lenght = XY.Length + offer.Length + 14;//current_offer=
    114.             string data = "current_offer=" + offer + XY;
    115.  
    116.             HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.neopets.com/haggle.phtml");
    117.  
    118.  
    119.             request.CookieContainer = _cookieJar;
    120.             request.Method = "POST";
    121.             request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; fr; rv:1.9.0.1) Gecko/2008070208 Firefox/3.0.1";
    122.             request.Accept = AcceptHeader.CAPCHA_SHOW;
    123.             request.Headers.Add(HttpRequestHeader.AcceptLanguage, "en-us,en;q=0.5");
    124.             request.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip,deflate");
    125.             request.Headers.Add(HttpRequestHeader.AcceptCharset, "ISO-8859-1,utf-8;q=0.7,*;q=0.7");
    126.             request.Headers.Add(HttpRequestHeader.KeepAlive, "300");
    127.             request.ContentType = "application/x-www-form-urlencoded";
    128.             request.KeepAlive = true;
    129.             request.Referer = "http://www.neopets.com/" + referer;
    130.             request.AllowAutoRedirect = false;
    131.             request.Proxy = null;
    132.  
    133.  
    134.  
    135.             byte[] bytes = Encoding.ASCII.GetBytes(data);
    136.             request.ContentLength = bytes.Length;
    137.             Stream os = request.GetRequestStream();
    138.             os.Write(bytes, 0, bytes.Length);
    139.             os.Close();
    140.  
    141.             //After buying we are redirected to the shop from this url for all shops
    142.             _referer = "http://www.neopets.com/haggle.phtml";
    143.             Console.WriteLine(_referer);
    144.  
    145.             //we get the response from the server
    146.             HttpWebResponse WebResponse = (HttpWebResponse)request.GetResponse();
    147.             //we change the response into a Stream
    148.             Stream responseStream = WebResponse.GetResponseStream();
    149.  
    150.  
    151.             //if we see in the stream gzip it's ziped else it's not
    152.             if (WebResponse.ContentEncoding.ToLower().Contains("gzip"))
    153.                 responseStream = new GZipStream(responseStream, CompressionMode.Decompress);
    154.             else if (WebResponse.ContentEncoding.ToLower().Contains("deflate"))
    155.                 responseStream = new DeflateStream(responseStream, CompressionMode.Decompress);
    156.  
    157.             //from the reponse stream we read the html with StreamReader
    158.             StreamReader Reader = new StreamReader(responseStream, Encoding.Default);
    159.             string Html = Reader.ReadToEnd();
    160.  
    161.             if (Html.IndexOf("I accept your offer") != -1)
    162.             {
    163.                 Console.WriteLine("Item bought !");
    164.                 reponse = true;
    165.             }
    166.             else
    167.             {
    168.                 Console.WriteLine("Item missed !");
    169.                 reponse = false;
    170.             }
    171.  
    172.  
    173.             Reader.Close(); // And we close
    174.             Reader = null;
    175.  
    176.             return reponse;
    177.         }
    178.  
    179.  
    180.         static public void connecteToNeopet(string password, string username)
    181.         {
    182.             if (!_isLoggedIn)
    183.             {
    184.                 /*
    185.                   CONSTRUCTION OF THE QUERY
    186.                  */
    187.                 _cookieJar = new CookieContainer();
    188.                 string data = "username=" + username + "&password=" + password + "&destination=%2Fpetcentral.phtml";
    189.                 int content_lenght = data.Length;
    190.                 HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://www.neopets.com/login.phtml");
    191.                 req.Method = "POST";
    192.                 req.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; fr; rv:1.9.0.1) Gecko/2008070208 Firefox/3.0.1";
    193.                 req.Headers.Add(HttpRequestHeader.AcceptLanguage, "en-us,en;q=0.5");
    194.                 req.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip,deflate");
    195.                 req.Headers.Add(HttpRequestHeader.AcceptCharset, "ISO-8859-1,utf-8;q=0.7,*;q=0.7");
    196.                 req.Headers.Add(HttpRequestHeader.KeepAlive, "300");
    197.                 req.KeepAlive = true;
    198.                 req.ContentType = "application/x-www-form-urlencoded";
    199.                 req.Referer = "http://www.neopets.com/hi.phtml";
    200.                 req.CookieContainer = _cookieJar;
    201.                 req.ContentLength = content_lenght;
    202.                 req.AllowAutoRedirect = false;
    203.                 req.Proxy = null;
    204.  
    205.                 req.Accept = "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
    206.                 _referer = "http://www.neopets.com/hi.phtml";//still hi.phtml yes
    207.                 Console.WriteLine(_referer);
    208.  
    209.                 /*
    210.                  ENCODING POST DATA
    211.                  */
    212.                 byte[] bytes = System.Text.Encoding.ASCII.GetBytes(data);
    213.                 Stream os = req.GetRequestStream();
    214.                 os.Write(bytes, 0, bytes.Length);
    215.                 os.Close();
    216.  
    217.                 /*
    218.                   GETTING THE ANWSER
    219.                  */
    220.                 HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
    221.                 //we change the response into a Stream
    222.                 Stream responseStream = resp.GetResponseStream();
    223.  
    224.  
    225.                 bool connected = false;
    226.                 foreach (Cookie cook in resp.Cookies)
    227.                 {
    228.                     _cookieJar.Add(cook);
    229.                     if (cook.Name.Contains("neologin"))
    230.                         connected = true;
    231.                 }
    232.  
    233.  
    234.  
    235.                 Thread downloadAll = new Thread(new ThreadStart(downloadIndex));
    236.                 downloadAll.Start();
    237.  
    238.                 /*
    239.                  ANALYSING RESULTS
    240.                  */
    241.  
    242.                 //I guesse if we see logout it means we are in ^_^
    243.                 if (connected)
    244.                 {
    245.                     Console.WriteLine("Connection succesful\r\n");
    246.                 }
    247.                 else
    248.                 {
    249.                     Console.WriteLine("You don't have the correct password/username");
    250.                 }
    251.                 _isLoggedIn = true;
    252.             }
    253.             else
    254.             {
    255.  
    256.                 logOut();
    257.                 connecteToNeopet(password, username);
    258.                 _isLoggedIn = true;
    259.             }
    260.         }
    261.  
    262.  
    263.         /// <summary>
    264.         /// This methis log out of neopet
    265.         /// </summary>
    266.         static public void logOut()
    267.         {
    268.  
    269.             /*
    270.             CONSTRUCTION OF THE QUERY TO LOG OUT
    271.             */
    272.             HttpWebRequest request = HTTPqueryMaker("http://www.neopets.com/logout.phtml");
    273.  
    274.             _cookieJar = null;
    275.             _referer = "http://www.neopets.com/hi.phtml";
    276.  
    277.             /*
    278.              GETTING THE ANWSER
    279.             */
    280.             HttpWebResponse resp = (HttpWebResponse)request.GetResponse();
    281.             //we change the response into a Stream
    282.             Stream responseStream = resp.GetResponseStream();
    283.             //if we see in the stream gzip it's ziped else it's not
    284.             if (resp.ContentEncoding.ToLower().Contains("gzip"))
    285.                 responseStream = new GZipStream(responseStream, CompressionMode.Decompress);
    286.             else if (resp.ContentEncoding.ToLower().Contains("deflate"))
    287.                 responseStream = new DeflateStream(responseStream, CompressionMode.Decompress);
    288.  
    289.             resp.Close();
    290.             responseStream.Close();
    291.  
    292.         }
    293.  
    294.  
    295.         static private void downloadIndex()
    296.         {
    297.             /*
    298.                CONSTRUCTION OF THE QUERY GO AT THE INDEX http://www.neopets.com/index.phtml
    299.              */
    300.  
    301.             //
    302.             _referer = "http://www.neopets.com/hi.phtml";
    303.             HttpWebRequest request = HTTPqueryMaker("http://www.neopets.com/index.phtml");
    304.             //Here we put referer ready to LogIn()
    305.             _referer = "http://www.neopets.com/index.phtml";
    306.  
    307.  
    308.  
    309.             /*
    310.              GETTING THE ANWSER
    311.             */
    312.             HttpWebResponse resp = (HttpWebResponse)request.GetResponse();
    313.             //we change the response into a Stream
    314.             Stream responseStream = resp.GetResponseStream();
    315.             //if we see in the stream gzip it's ziped else it's not
    316.             if (resp.ContentEncoding.ToLower().Contains("gzip"))
    317.                 responseStream = new GZipStream(responseStream, CompressionMode.Decompress);
    318.             else if (resp.ContentEncoding.ToLower().Contains("deflate"))
    319.                 responseStream = new DeflateStream(responseStream, CompressionMode.Decompress);
    320.  
    321.             //from the reponse stream we read the html with StreamReader
    322.             // StreamReader Reader = new StreamReader(responseStream, Encoding.Default);
    323.             // string Html = Reader.ReadToEnd();
    324.  
    325.             responseStream.Close();
    326.             resp.Close();
    327.  
    328.         }
    329.  
    330.  
    331.         /// <summary>
    332.         /// This class is for the Thread downlaodRessources, so we can pass an argument
    333.         /// </summary>
    334.         private class downlaodItems
    335.         {
    336.             string rawHTML;
    337.  
    338.             public downlaodItems(string rawHTML)
    339.             {
    340.                 this.rawHTML = rawHTML;
    341.             }
    342.  
    343.             public void download()
    344.             {
    345.                 downlaodRessources(rawHTML);
    346.             }
    347.  
    348.  
    349.         }
    350.  
    351.  
    352.  
    353.         /// <summary>
    354.         /// This return a html page and download all needed components
    355.         /// </summary>
    356.         /// <param name="url">URL of the html page</param>
    357.         /// <returns>html page in string format</returns>
    358.         static public string downloadPage(string url)
    359.         {
    360.  
    361.             /*
    362.              CONSTRUCTION OF THE QUERY
    363.              */
    364.             HttpWebRequest request = HTTPqueryMaker(url);
    365.  
    366.  
    367.             /*
    368.              GETTING THE ANWSER
    369.             */
    370.             HttpWebResponse resp = (HttpWebResponse)request.GetResponse();
    371.             //we change the response into a Stream
    372.             Stream responseStream = resp.GetResponseStream();
    373.             //if we see in the stream gzip it's ziped else it's not
    374.             if (resp.ContentEncoding.ToLower().Contains("gzip"))
    375.                 responseStream = new GZipStream(responseStream, CompressionMode.Decompress);
    376.             else if (resp.ContentEncoding.ToLower().Contains("deflate"))
    377.                 responseStream = new DeflateStream(responseStream, CompressionMode.Decompress);
    378.  
    379.             //from the reponse stream we read the html with StreamReader
    380.             StreamReader Reader = new StreamReader(responseStream, Encoding.Default);
    381.             string Html = Reader.ReadToEnd();
    382.             //Console.WriteLine(Html);
    383.  
    384.             //*** Here we download all the file that usually firefox download ***//
    385.  
    386.             // I created a class so we can call a thread with params
    387.             //and left the unimportant downloads in a thread
    388.             downlaodItems dl = new downlaodItems(Html);
    389.  
    390.             Thread downloadAll = new Thread(new ThreadStart(dl.download));
    391.             downloadAll.Start();
    392.  
    393.  
    394.             responseStream.Close();
    395.             resp.Close();
    396.  
    397.             return Html;
    398.  
    399.  
    400.  
    401.         }
    402.  
    403.         /// <summary>
    404.         /// Download safely the haggle image, but you need to find the image's url and referer on your own
    405.         /// </summary>
    406.         /// <param name="urlImage">
    407.         /// Should look like this :
    408.         /// http://www.neopets.com/captcha_show.phtml?_x_pwned=f199f0ffb046abaf7bba1ed20b231e98
    409.         /// using http://www.neopets.com/captcha_show.phtml alone is not recommanded.      
    410.         /// </param>
    411.         ///
    412.         /// <param name="referer">
    413.         /// Should look like this:
    414.         /// http://www.neopets.com/haggle.phtml?obj_info_id=7432&stock_id=891236737&brr=1366
    415.         /// </param>
    416.         /// <returns>
    417.         ///
    418.         /// </returns>
    419.         static public Bitmap downloadImage(string urlImage,string referer)        {
    420.             try
    421.             {
    422.                 _referer = referer;
    423.                 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(urlImage);
    424.                 request.CookieContainer = _cookieJar;
    425.                 request.Method = "GET";
    426.                 request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; fr; rv:1.9.0.1) Gecko/2008070208 Firefox/3.0.1";
    427.                 request.Accept = "image/png,image/*;q=0.8,*/*;q=0.5,en;q=0.3";
    428.                 request.Headers.Add(HttpRequestHeader.AcceptLanguage, "en-us,en;q=0.5");
    429.                 request.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip,deflate");
    430.                 request.Headers.Add(HttpRequestHeader.AcceptCharset, "ISO-8859-1,utf-8;q=0.7,*;q=0.7");              
    431.                 request.KeepAlive = true;
    432.                 request.Proxy = null;
    433.                 request.Referer = _referer;
    434.                 request.AllowAutoRedirect = false;
    435.  
    436.  
    437.                 Console.WriteLine("download : " + urlImage + " with " + _referer + "as referer..");
    438.  
    439.                 HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    440.                 Stream responseStream = response.GetResponseStream();
    441.  
    442.                 if (response.ContentEncoding.ToLower().Contains("gzip"))
    443.                     responseStream = new GZipStream(responseStream, CompressionMode.Decompress);
    444.                 else if (response.ContentEncoding.ToLower().Contains("deflate"))
    445.                     responseStream = new DeflateStream(responseStream, CompressionMode.Decompress);
    446.                
    447.                
    448.                 return new Bitmap(responseStream);
    449.  
    450.             }
    451.             catch (Exception e)
    452.             {
    453.                 Console.WriteLine(e.Message);
    454.                 return null;
    455.             }
    456.         }
    457.  
    458.         /// <summary>
    459.         /// Download all the ressources that firefox usually download from one html document
    460.         /// </summary>
    461.         /// <param name="rawHTML"></param>
    462.         static private void downlaodRessources(string rawHTML)
    463.         {
    464.             int begin = 0;
    465.             int end = 0;
    466.             int numberOfRessources = countOccurence(rawHTML, "src=\"");
    467.             string ressourceLink = "";
    468.  
    469.  
    470.             for (int i = 0; i < numberOfRessources; i++)
    471.             {
    472.                 begin = rawHTML.IndexOf("src=\"", begin) + 5;//src="http://images.neopets.com/n.js"
    473.                 end = rawHTML.IndexOf("\"", begin);
    474.                 int lenght = end - begin;
    475.                 ressourceLink = rawHTML.Substring(begin, lenght);
    476.  
    477.  
    478.                 if (ressourceLink.Contains(".jpg"))
    479.                     downloadRessource(ressourceLink, AcceptHeader.JPG);
    480.                 if (ressourceLink.Contains(".png"))
    481.                     downloadRessource(ressourceLink, AcceptHeader.PNG);
    482.                 if (ressourceLink.Contains(".swf"))
    483.                     downloadRessource(ressourceLink, AcceptHeader.APP_X_SWF);
    484.                 if (ressourceLink.Contains(".html"))
    485.                     downloadHTML(ressourceLink);//html is a special case
    486.                 if (ressourceLink.Contains(".css"))
    487.                     downloadRessource(ressourceLink, AcceptHeader.CSS);
    488.                 if (ressourceLink.Contains(".js"))
    489.                     downloadRessource(ressourceLink, AcceptHeader.JS);
    490.                 if (ressourceLink.Contains(".gif"))
    491.                     downloadRessource(ressourceLink, AcceptHeader.GIF);
    492.                 if (ressourceLink.Contains(".icon"))
    493.                     Console.WriteLine("ICON FOUND !!!");//debug
    494.  
    495.  
    496.             }
    497.  
    498.  
    499.  
    500.         }
    501.         /// <summary>
    502.         /// Count the number of time a patern of string appears in a html file
    503.         /// </summary>
    504.         /// <param name="html"></param>
    505.         /// <returns></returns>
    506.         static private int countOccurence(string html, string patern)
    507.         {
    508.  
    509.             int index = 0;
    510.             int count = 0;
    511.             while (html.IndexOf(patern, index) != -1)
    512.             {
    513.                 index = html.IndexOf(patern, index) + patern.Length; //we add  occurence.Length so we don't while eternity
    514.                 count++;
    515.             }
    516.             return count;
    517.         }
    518.  
    519.         /// <summary>
    520.         ///  Download any file GIF/JPG/ICON/SWF/CSS/JS/GIF
    521.         /// </summary>
    522.         /// <param name="url"></param>
    523.         /// <param name="accept"></param>
    524.  
    525.         static private void downloadRessource(string url, string accept)
    526.         {
    527.  
    528.             /*
    529.              CONSTRUCTION OF THE QUERY
    530.              */
    531.  
    532.             //Here we make a check up so it's a http url
    533.             //else request will throw exceptions
    534.             if (!url.ToLower().Contains("http"))
    535.                 return;
    536.             string temporaryReferer = _referer;
    537.             HttpWebRequest request = HTTPqueryMaker(url);
    538.             _referer = temporaryReferer; //we don't want to change the referer when we download ressources
    539.  
    540.             /*
    541.              GETTING THE ANWSER
    542.             */
    543.             HttpWebResponse resp = (HttpWebResponse)request.GetResponse();
    544.             //we change the response into a Stream
    545.             Stream responseStream = resp.GetResponseStream();
    546.             //if we see in the stream gzip it's ziped else it's not
    547.             if (resp.ContentEncoding.ToLower().Contains("gzip"))
    548.                 responseStream = new GZipStream(responseStream, CompressionMode.Decompress);
    549.             else if (resp.ContentEncoding.ToLower().Contains("deflate"))
    550.                 responseStream = new DeflateStream(responseStream, CompressionMode.Decompress);
    551.  
    552.             responseStream.Close();
    553.             resp.Close();
    554.  
    555.  
    556.             //Here we don't really want to use the stuff we downloaded it's just for show.
    557.  
    558.  
    559.         }
    560.  
    561.         /// <summary>
    562.         /// Only Download a html document, just to fake being a true firefox browser
    563.         /// But do not return html document as his public version. This is a useful
    564.         /// method
    565.         /// </summary>
    566.         /// <param name="url"></param>
    567.         static private void downloadHTML(string url)
    568.         {
    569.             /*
    570.               CONSTRUCTION OF THE QUERY
    571.              */
    572.             HttpWebRequest request = HTTPqueryMaker(url);
    573.  
    574.  
    575.  
    576.             HttpWebResponse resp = (HttpWebResponse)request.GetResponse();
    577.             //we change the response into a Stream
    578.             Stream responseStream = resp.GetResponseStream();
    579.  
    580.  
    581.             //if we see in the stream gzip it's ziped else it's not
    582.             if (resp.ContentEncoding.ToLower().Contains("gzip"))
    583.                 responseStream = new GZipStream(responseStream, CompressionMode.Decompress);
    584.             else if (resp.ContentEncoding.ToLower().Contains("deflate"))
    585.                 responseStream = new DeflateStream(responseStream, CompressionMode.Decompress);
    586.  
    587.             //from the reponse stream we read the html with StreamReader
    588.             StreamReader Reader = new StreamReader(responseStream, Encoding.Default);
    589.             string Html = Reader.ReadToEnd();
    590.  
    591.             resp.Close();
    592.             responseStream.Close();
    593.  
    594.             downlaodItems dl = new downlaodItems(Html);
    595.             Thread downloadAll = new Thread(new ThreadStart(dl.download));
    596.             downloadAll.Start();
    597.         }
    598.  
    599.  
    600.  
    601.         /// <summary>
    602.         /// This method is used to create a HttpWebRequest Object from a url
    603.         /// </summary>
    604.         /// <param name="url"></param>
    605.         /// <returns></returns>
    606.         static private HttpWebRequest HTTPqueryMaker(string url)
    607.         {
    608.  
    609.             HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
    610.             request.Method = "GET";
    611.  
    612.             request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; fr; rv:1.9.0.1) Gecko/2008070208 Firefox/3.0.1";
    613.             request.Accept = AcceptHeader.HTML;
    614.             request.Headers.Add(HttpRequestHeader.AcceptLanguage, "en-us,en;q=0.5");
    615.             request.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip,deflate");
    616.             request.Headers.Add(HttpRequestHeader.AcceptCharset, "ISO-8859-1,utf-8;q=0.7,*;q=0.7");
    617.             request.Headers.Add(HttpRequestHeader.KeepAlive, "300");
    618.             request.KeepAlive = true;
    619.             request.Referer = _referer;
    620.             request.CookieContainer = _cookieJar;
    621.  
    622.             request.Proxy = null;
    623.             request.AllowAutoRedirect = false;
    624.             Console.WriteLine("download : " + url + " with " + _referer + " as referer..");
    625.             _referer = url;
    626.  
    627.             return request;
    628.  
    629.         }
    630.  
    631.         /// <summary>
    632.         /// Return the actual session via a CookieContainer
    633.         /// </summary>
    634.         /// <returns></returns>
    635.         static public CookieContainer getCookies()
    636.         {
    637.             return _cookieJar;
    638.         }
    639.  
    640.  
    641.     }
    642. }
    643.  
    I would like to hear your comments and ideas on this wrapper. Thanks in advance.
     
  2. Zach

    Zach Newbie

    Joined:
    May 26, 2008
    Messages:
    31
    Likes Received:
    1
    Why don't you just make a function to connect to the internet?

    Try a syntax like this:

    Code (Text):
    1. static private void GETRequest(string url, string referer)
    2. static private void POSTRequest(string url, string data, string referer)
    3.  
     
  3. zav75

    zav75 Level I

    Joined:
    Feb 28, 2007
    Messages:
    76
    Likes Received:
    6
    Location:
    Canada, Province of Québec
    Humm , I won't do that because people make mistake with referers and it makes the program easily detectable by tnt.. I use the systaxe

    Code (Text):
    1. static public string downloadPage(string url)
    because my wrapper manage the referer alone.

    For the post, in neopet the only two post that I needed was in connect, to get your cookie, and the one in buy (to buy an item from a shop). Of course, if you want to use it for an application that buy/sell stock you'll have to make the buy function for the stock, however you already have all the example needed and you probably juste change the url and post data. But, I'll agree that it may be useful to have a post for other operations. Except that I would have the following syntaxe

    Code (Text):
    1. static private void POSTRequest(string url, string data)
    again for the same reason, to force people use the same path as a true firefox user, and to be sure the referers are well managed.