Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 136,096 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,629 people online right now. Registration is fast and FREE... Join Now!




__VIEWSTATE from ASP.NET

 
Reply to this topicStart new topic

__VIEWSTATE from ASP.NET, is there an easier way?

marcells23
20 Nov, 2007 - 10:37 PM
Post #1

D.I.C Head
Group Icon

Joined: 22 Aug, 2007
Posts: 134



Thanked: 3 times
Dream Kudos: 125
My Contributions
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?

CODE
        
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;
            }

        }

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/1/08 08:39PM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month