__VIEWSTATE from ASP.NET

is there an easier way?

Page 1 of 1

0 Replies - 1636 Views - Last Post: 20 November 2007 - 11:37 PM Rate Topic: -----

#1 marcells23  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 6
  • View blog
  • Posts: 143
  • Joined: 22-August 07

__VIEWSTATE from ASP.NET

Posted 20 November 2007 - 11:37 PM

OK so i wrote this snippet below because I was trying to do some htmlscraping for my job and it requires authentication.
I understand how to use POST and cookies with c# but there is nothing to retrieve the __VIEWSTATE that is hidden in the html.
Is there a more efficient way of doing this than what I have below?

		
public string statevalue(string url)
		{

			try
			{
				WebRequest Wrq = (HttpWebRequest)WebRequest.Create(url);
				WebResponse Wrsp = Wrq.GetResponse();
				StreamReader reader = new StreamReader(Wrsp.GetResponseStream());


				string hello = reader.ReadToEnd(); //Reads html from url 

				string match = @"__VIEWSTATE"" value="""; //value to look for in web page

				int index = 0;
				char[] html = hello.ToCharArray();
				int i;
				for (i = 0; i < html.Length; i++)//loop through characters to get index(where the match begins)
				{
					if (hello.Substring(i, match.Length).Contains(match))
					{

						index = i;
						break;
					}
					else
					{
						return "No VIEWSTATE Value Found";
					}


				}

				string results = string.Empty;
				int k;
				for (k = (i + match.Length); k < html.Length; k++)//start from index and add each character from viewstate to results string until (") is reached.
				{
					if (html[k].ToString().Contains(@""""))
					{
						break;
					}
					results += html[k].ToString();
				}
				return results;
			}
			catch (Exception ex)
			{

				return null;
			}

		}



Is This A Good Question/Topic? 0
  • +

Page 1 of 1