[C# .NET] Useful Code Snippets for Autobuyer

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
    Here I will post some code snippets that I think will help my fellow C# programmer brothers to develop some Cute Autobuyer.

    • First, HowTo lunch a thread (Without params)
    Code (Text):
    1.  
    2.             Thread restocking = new Thread(new ThreadStart(restock)) ;
    3.             restocking.Name = "Restocking thread";
    4.             restocking.Start();
    5.  
    • Second Howto lunch a thread (With params)



    Here is what we CAN'T do

    Code (Text):
    1.  
    2.             Thread restocking = new Thread(new ThreadStart(restock("http://www.neopets.com") )) ;//WRONG !THE COMPILER DENY YOU.
    3.             restocking.Name = "Restocking thread";
    4.             restocking.Start();
    5.  

    Here the code that do the job, since the class Thread don't accept thread we have to do a trick, we have to create a class that will contains our parameter and call the method we need via the new class.
    Code (Text):
    1.  
    2.  
    3.  
    4.             autoBuyer aber = new autoBuyer("http://www.neopets.com");
    5.             Thread  restock= new Thread(new ThreadStart(aber.doRestock ));
    6.             restocking.Name = "Restocking thread";
    7.             restock.Start();
    8.  
    9.    private class autoBuyer
    10.         {
    11.             string url;
    12.  
    13.             public autoBuyer(string url) //constructor
    14.             {
    15.                 this.url = url;
    16.             }
    17.             public doRestock()
    18.             {
    19.                 restock(url);//this is the method we want to lunch in the thread
    20.             }
    21.  
    22.         }
    23.  
    24.  
    25.  

    • Third, How to use delegates, since you use threading now you need to know how to acces a variable outside the thread. If you don't do that you will have cross threading exceptions. That would be bad since it makes crash your application.

    For example, if you want to write a message in your text box to show the current status in your thread you'll have to use a delegate.

    Code (Text):
    1.        
    2.  
    3.  
    4.        delegate  void showStatusDelegate(string message);//This is the delegate
    5.      
    6.         private void showStatus(string message) {              //This is the method that will put your string into the text box
    7.             if (txtStatus.InvokeRequired){
    8.                 this.txtStatus.Invoke(new showStatusDelegate(this.showStatus), message);
    9.             }
    10.             else {
    11.                 txtStatus.Text = message;
    12.             }
    13.        }
    14.  
    15.      //Here is an example inside a thread
    16.    
    17.      showStatus("Yo I'm chill in this thread");
    18.  
    19.  
    20.  
    • Fourth, how to make a HTTP request.

    I suggest you to use my HTTP wrapper, but people may want to make their own and that's ok.

    There's around 3 important steps, in order to make a HTTP request and get the response. ( GET EXAMPLE)

    Code (Text):
    1.  
    2.    
    3.  
    4.            // First we need to build the request so we need to make a HttpWebRequest object.
    5.  
    6.             HttpWebRequest request =  HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.neopets.com");
    7.          
    8.             //Here we set all the attributes of the request
    9.             request.Method = "GET";
    10.             request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; fr; rv:1.9.0.1) Gecko/2008070208 Firefox/3.0.1";
    11.             request.Accept = "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,*/*;q=0.8";
    12.             request.Headers.Add(HttpRequestHeader.AcceptLanguage, "en-us,en;q=0.5");
    13.             request.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip,deflate");
    14.             request.Headers.Add(HttpRequestHeader.AcceptCharset, "ISO-8859-1,utf-8;q=0.7,*;q=0.7");
    15.             request.Headers.Add(HttpRequestHeader.KeepAlive, "300");
    16.             request.KeepAlive = true;
    17.             request.Referer = "http://www.google.com" ;//usually in neopet you need to have the good referer, else the page won't load
    18.             request.CookieContainer = new CookieContainer() ; //here you usually to get the cookie properly(log in and get cookies) this do really nothing
    19.  
    20.             request.Proxy = null;//if you don't do that it will search for proxy and will make you wait 15seconds
    21.             request.AllowAutoRedirect = false;
    22.          
    23.          
    24.  
    25.             //Here we get the response from the HttpWebRequest
    26.             HttpWebResponse resp = (HttpWebResponse)request.GetResponse();
    27.             //we change the response into a Stream
    28.             Stream responseStream = resp.GetResponseStream();
    29.             //if we see in the stream gzip it's ziped else it's not
    30.             if (resp.ContentEncoding.ToLower().Contains("gzip"))
    31.                 responseStream = new GZipStream(responseStream, CompressionMode.Decompress);
    32.             else if (resp.ContentEncoding.ToLower().Contains("deflate"))
    33.                 responseStream = new DeflateStream(responseStream, CompressionMode.Decompress);
    34.  
    35.             //from the reponse stream we read the html with StreamReader
    36.             StreamReader Reader = new StreamReader(responseStream, Encoding.Default);
    37.             string Html = Reader.ReadToEnd(); //Here you have your html code        
    38.  
    39.             responseStream.Close();//Don't forget to close else your buffer will be full, and that's uncool
    40.             resp.Close();
    41.  
    42.             return Html;        
    43.  
    44.  
    45.  
    46.  
    Same thing but with a POST (POST EXAMPLE)

    Code (Text):
    1.  
    2.                  /*
    3.                   CONSTRUCTION OF THE QUERY
    4.                  */
    5.                 _cookieJar = new CookieContainer();
    6.                 string data = "username=" + username + "&password=" + password + "&destination=%2Fpetcentral.phtml";
    7.                 int content_lenght = data.Length;
    8.                 HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://www.neopets.com/login.phtml");
    9.                 req.Method = "POST";
    10.                 req.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; fr; rv:1.9.0.1) Gecko/2008070208 Firefox/3.0.1";
    11.                 req.Headers.Add(HttpRequestHeader.AcceptLanguage, "en-us,en;q=0.5");
    12.                 req.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip,deflate");
    13.                 req.Headers.Add(HttpRequestHeader.AcceptCharset, "ISO-8859-1,utf-8;q=0.7,*;q=0.7");
    14.                 req.Headers.Add(HttpRequestHeader.KeepAlive, "300");
    15.                 req.KeepAlive = true;
    16.                 req.ContentType = "application/x-www-form-urlencoded";
    17.                 req.Referer = "http://www.neopets.com/hi.phtml";
    18.                 req.CookieContainer = _cookieJar;
    19.                 req.ContentLength = content_lenght;
    20.                 req.AllowAutoRedirect = false;
    21.                 req.Proxy = null;
    22.  
    23.                 req.Accept = "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
    24.                 _referer = "http://www.neopets.com/hi.phtml";//still hi.phtml yes
    25.                 Console.WriteLine(_referer);
    26.  
    27.                 /*
    28.                  ENCODING POST DATA
    29.                  */
    30.                 byte[] bytes = System.Text.Encoding.ASCII.GetBytes(data);
    31.                 Stream os = req.GetRequestStream();
    32.                 os.Write(bytes, 0, bytes.Length);
    33.                 os.Close();
    34.  
    35.                 /*
    36.                   GETTING THE ANWSER
    37.                  */
    38.                 HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
    39.                 //we change the response into a Stream
    40.                 Stream responseStream = resp.GetResponseStream();
    41.  
    42.  
    43.                 bool connected = false;
    44.                 foreach (Cookie cook in resp.Cookies)
    45.                 {
    46.                     _cookieJar.Add(cook);
    47.                     if (cook.Name.Contains("neologin"))
    48.                         connected = true;
    49.                 }
    50.  
    51.  
    (Yes this code may help you to log in)

    If you can use/edit these for your programs then I would make me happy.
    Comments and suggestions are welcome !
     
    expon, Phee and chelsea1 like this.
  2. chelsea1

    chelsea1 Level IV

    Joined:
    Nov 26, 2006
    Messages:
    2,538
    Likes Received:
    31
    Location:
    London
    great coding examples

    +rep and +20 cash
     
  3. Skyle

    Skyle Level II

    Joined:
    Aug 13, 2007
    Messages:
    212
    Likes Received:
    7
    Good job. I am sure a few people can learn from this.
     
  4. Chris

    Chris Level II

    Joined:
    Aug 16, 2008
    Messages:
    213
    Likes Received:
    2
    What compiler would you suggest for lcc-win32..?