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