// Collection of code snippets by Arne Vajhøj // (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) using System; using System.IO; using System.Net; using System.Web; using System.Text.RegularExpressions; namespace E { public class MainClass { public static string Get(string url, CookieContainer session) { HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(url); wr.Method = "GET"; wr.CookieContainer = session; string html = (new StreamReader(wr.GetResponse().GetResponseStream())).ReadToEnd(); return html; } public static string Post(string url, string fields, CookieContainer session) { HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(url); wr.Method = "POST"; wr.CookieContainer = session; wr.ContentType = "application/x-www-form-urlencoded"; StreamWriter post = new StreamWriter(wr.GetRequestStream()); post.Write(fields); post.Close(); string html = (new StreamReader(wr.GetResponse().GetResponseStream())).ReadToEnd(); return html; } public static void Main(string[] args) { CookieContainer session = new CookieContainer(); string first = Get("http://localhost/test.aspx", session); Console.WriteLine(first); string viewstate = HttpUtility.UrlEncode(Regex.Matches(first,"(?:)")[0].Groups[1].Value); string eventvalidation = HttpUtility.UrlEncode(Regex.Matches(first,"(?:)")[0].Groups[1].Value); Console.WriteLine(viewstate); Console.WriteLine(eventvalidation); string login = Post("http://localhost/Login.aspx?ReturnUrl=%2ftest.aspx", "__EVENTTARGET=&__EVENTARGUMENT=&Login1%24UserName=supermand&Login1%24Password=hemmeligt&__VIEWSTATE=" + viewstate + "&Login1%24LoginButton=Log+In&__EVENTVALIDATION=" + eventvalidation, session); Console.WriteLine(login); string second = Get("http://localhost/test.aspx", session); Console.WriteLine(second); } } }