Code:using System; using System.Collections.Generic; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Text.RegularExpressions; using System.IO; using System.IO.Compression; using System.Net.Sockets; namespace goodwrap { class wrapper { private TcpClient client; IDictionary<string, string> colCookies = new Dictionary<string, string>(); public string strCookies; public string LastPage; public string Request(string Method, string URL, string Referer) { string Host = null; string strFile = null; string strPost = null; int pos = 0; if (Referer == null) { Referer = LastPage; } if (URL.Contains("http://")) { Host = URL.Substring(7); } else { Host = URL; } if (Host.Contains("/")) { pos = Host.IndexOf("/", 0); strFile = Host.Substring(pos); Host = Host.Substring(0, pos); } else { strFile = "/"; } if (Method == "POST") { pos = strFile.IndexOf("?"); if (pos != -1) { strPost = strFile.Substring(pos + 1); strFile = strFile.Substring(0, pos); } else { strPost = null; } } LastPage = URL; string ReqHeaders = null; if (Method == "GET" || Method == "PIC") { ReqHeaders = "GET" + " " + strFile + " HTTP/1.1" + "\r\n" + "Host: " + Host + "\r\n" + "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.2; en-US; rv:1.8.0.7) Gecko/20060909 Firefox/1.5.0.7" + "\r\n" + "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5" + "\r\n" + "Accept-Language: en-us,en;q=0.5" + "\r\n" + "Accept-Encoding: gzip, deflate" + "\r\n" + "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7" + "\r\n" + "Keep-Alive: 300" + "\r\n" + "Connection: keep-alive" + "\r\n" + "Referer: " + Referer + "\r\n" + "Cookie: " + strCookies + "\r\n" + "\r\n"; } else { ReqHeaders = "POST " + strFile + " HTTP/1.1" + "\r\n" + "Host: " + Host + "\r\n" + "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.2; en-US; rv:1.8.0.7) Gecko/20060909 Firefox/1.5.0.7" + "\r\n" + "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5" + "\r\n" + "Accept-Language: en-us,en;q=0.5" + "\r\n" + "Accept-Encoding: gzip, deflate" + "\r\n" + "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7" + "\r\n" + "Keep-Alive: 300" + "\r\n" + "Connection: keep-alive" + "\r\n" + "Referer: " + Referer + "\r\n" + "Cookie: " + strCookies + "\r\n" + "Content-Type: application/x-www-form-urlencoded" + "\r\n" + "Content-Length: " + strPost.Length.ToString() + "\r\n" + "Connection: close" + "\r\n" + "\r\n" + strPost; } if(Method == "PIC") ReqHeaders.Replace("Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5" , "Accept: image/png,*/*;q=0.5"); client = new TcpClient(Host, 80); Byte[] headers = System.Text.Encoding.ASCII.GetBytes(ReqHeaders); NetworkStream ns = client.GetStream(); ns.Write(headers, 0, headers.Length); StreamReader sr = new StreamReader(ns, Encoding.Default); String strHTML = sr.ReadToEnd(); string[] strParts = Regex.Split(strHTML, Environment.NewLine + Environment.NewLine); strCookies = ParseCookies(strParts[0]); if (strParts[0].Contains("Content-Encoding")) { strParts[1] = DecompressGzip(strParts[1]); } return strParts[0] + Environment.NewLine + Environment.NewLine + strParts[1]; } public string DecompressGzip(string compressed) { MemoryStream memStream = new MemoryStream(System.Text.Encoding.Default.GetBytes(compressed)); GZipStream decompressStream = new GZipStream(memStream, CompressionMode.Decompress); byte[] endBytes = new byte[4]; int position = (int)memStream.Length - 4; memStream.Position = position; memStream.Read(endBytes, 0, 4); memStream.Position = 0; byte[] buffer = new byte[BitConverter.ToInt32(endBytes, 0) + 100]; int offset = 0; int total = 0; while (true) { int o = decompressStream.Read(buffer, offset, 100); if (o == 0) break; offset += o; total += o; } return Encoding.ASCII.GetString(buffer); } public string NeoLogin(string user, string pass, ref bool loggedIn) { string strHTML = null; Request("GET", "http://neopets.com/loginpage.phtml", "http://google.com"); Pause(1); Request("POST", "http://www.neopets.com/hi.phtml?destination=%2Fpetcentral.phtml&username=" + user, "http://neopets.com/loginpage.phtml"); Pause(1); strHTML = Request("POST", "http://www.neopets.com/login.phtml?username=" + user + "&password=" + pass + "&destination=%2Fpetcentral.phtml", "http://neopets.com/hi.phtml"); if(strHTML.Contains("Set-Cookie: neologin=")){ loggedIn = true; return "Logged In"; } else if(strHTML.Contains("too many times")){ loggedIn = false; return "To Many Login Attempts"; } else if (strHTML.Contains("badpassword")) { loggedIn = false; return "Wrong Password"; } else if (strHTML.Contains("frozen")) { loggedIn = false; return "Account Frozen"; } else if (strHTML.Contains("just a technical problem")) { loggedIn = false; return "Neopets is down for maintenance."; } else { loggedIn = false; return strHTML; } } private static void Pause(double seconds) { double num = seconds * 1000; DateTime t1 = DateTime.Now, t2 = DateTime.Now; TimeSpan tmDiff = t2 - t1; while (Convert.ToDouble(tmDiff.TotalMilliseconds.ToString()) < num) { t2 = DateTime.Now; tmDiff = t2 - t1; Application.DoEvents(); } } public string StripHeaders(string strSource) { string[] strParts = Regex.Split(strSource, Environment.NewLine + Environment.NewLine); return strParts[1]; } public Bitmap GrabPic(string strURL) { MemoryStream memStream = new MemoryStream(System.Text.Encoding.Default.GetBytes(StripHeaders(Request("GET", strURL, LastPage)))); Bitmap bitmap = new Bitmap(memStream); return bitmap; } public void ClearCookies() { colCookies.Clear(); strCookies = null; } public string ParseCookies(string Headers) {//Credit's to Mystical for RegEx string ParseCookies = null; MatchCollection matches; Regex reg = new Regex("set-cookie:\\s*([^=]+)=([^;]+);", RegexOptions.IgnoreCase); if (reg.IsMatch(Headers)) { matches = reg.Matches(Headers); foreach (Match m in matches) { if (colCookies.ContainsKey(m.Groups[1].ToString())) { colCookies.Remove(m.Groups[1].ToString()); colCookies.Add(m.Groups[1].ToString(), m.Groups[1].ToString() + "=" + m.Groups[2].ToString()); } else { colCookies.Add(m.Groups[1].ToString(), m.Groups[1].ToString() + "=" + m.Groups[2].ToString()); } } } foreach (KeyValuePair<string, string> item in colCookies) { ParseCookies = ParseCookies + item.Value.ToString() + "; "; } return ParseCookies; } } }It has in memory gzip decompression, and a function that will return a bitmap from an images url.
instantiate the class like this: wrapper W = new wrapper();
and use it like this:
W.Request("GET", url, referrer);
W.Request("POST", url, referrer);
W.GrabPic(url);
Results 1 to 9 of 9
Thread: For Chad
- 24 Dec. 2009 06:28am #1
For Chad
- 24 Dec. 2009 07:00am #2Code:
public string MidString(string Source, string Starting, string Ending) { int lnStart, lnEnd; if (Source.Contains(Starting) && Source.Contains(Ending)) { lnStart = Source.IndexOf(Starting, 1) + Starting.Length; lnEnd = Source.IndexOf(Ending, 1); return Source.Substring(lnStart + 1, lnEnd - lnStart - 1); } else { return ""; } }
- 24 Dec. 2009 06:20pm #3
- 24 Dec. 2009 10:18pm #4
- 25 Dec. 2009 07:19pm #5
- 25 Dec. 2009 08:58pm #6
- 26 Dec. 2009 06:00pm #7
- 26 Dec. 2009 07:47pm #8
- 26 Dec. 2009 10:31pm #9