I'm learning C# and I'm interested in learning GET/POST
Any simple C# Neopets login example?
#1
Posted 03 August 2016 - 08:49 PM
#2
Posted 04 August 2016 - 07:11 AM
Don't forget that you also need a wrapper.
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 6.1; en-US; rv:1.9.1.4) Gecko/20091016 Firefox/3.5.4 (.NET CLR 3.5.30729)" + "\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 6.1; en-US; rv:1.9.1.4) Gecko/20091016 Firefox/3.5.4 (.NET CLR 3.5.30729)" + "\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; } } }
private void Login_Click(object sender, EventArgs e) { var loggedIn = false; myWrapper.NeoLogin(user.Text, pass.Text, ref loggedIn); if (loggedIn) { MessageBox.Show("We're successfully logged in. Congratulations.", "SUCCESS", MessageBoxButtons.OK, MessageBoxIcon.Hand); } else { MessageBox.Show("Your login was not successful.", "FAIL", MessageBoxButtons.OK, MessageBoxIcon.Hand); }
You'll have to declare...
goodwrap.wrapper myWrapper = new goodwrap.wrapper();
...in your public partial class.
#3
Posted 04 August 2016 - 08:42 AM
Don't forget that you also need a wrapper.
Nice! Thank you Adam
#4
Posted 06 August 2016 - 01:42 PM
Nice! Thank you Adam
No problemo! Let me know if you have any questions; I'm far far far from the best at C#, but I can help answer some pretty basic questions.
#5
Posted 08 September 2016 - 06:12 PM
THANK YOU SO MUCH @Adam FOR SHARING THE CODE!!!
I tried your code and was able to get to to login successfully.
Once I have logged in, lets say i want to navigate from the petcentral to the bank. What code do I used? And the same time I would like to retrieve the page and store it as a string.
Simillar to this line of code. I would like to mimic something like it where it takes the url of the page as a parameter and also the referring page to replicate user navigation from the site. But I would like to retrieve the webpage as a string.
strHTML = Request("POST", "http://www.neopets.c...html?username=" + user + "&password=" + pass + "&destination=%2Fpetcentral.phtml", "http://neopets.com/hi.phtml");
Hopefully someone can help me with this.
Thanks,
Verge
#6
Posted 09 September 2016 - 05:25 AM
Disclaimer: this is 4 years old and can probably be done much better with new C# standards, including async/await among other improvements. It'll probably still work though.
This is a really small, simple (aptly named as such) HTTP Wrapper written in C#.
Features:
- GET and POST methods supported (no PUT)
- Cookie management
- Proxy support
- Custom user-Agent
- Gzip decompression
Usage - Setup w/ proxy and custom user agent
var proxy = new WebProxy("127.0.0.1", 8888); proxy.Credentials = new NetworkCredential("foo", "bar"); var options = new WrapperOptions(proxy, "Mozilla/5.0 (Macintosh; Intel...snip"); var wrapper = new SimpleWrapper(options);
Usage - Humanised Neopets login (GET & POST)
GetString and PostString are the two methods used for GET and POST HTTP requests. Both return the HTML from the HTTP response, sans any headers.
wrapper.GetString("http://www.neopets.com"); var loginResponse = wrapper.PostString( "http://www.neopets.com/login.phtml", string.Format("destination=%252F&username={0}&password={1}", username, password));
Attached Files
0 user(s) are reading this topic
0 members, 0 guests, 0 anonymous users