Return to Snippet

Revision: 6185
at May 5, 2008 17:21 by BlueCockatoo

Initial Code
byte[] response;

WebClient webClient = new WebClient();
response = webClient.DownloadData(LOGIN_URL);

string viewstate = ExtractViewState(

string postData = String.Format(
   viewstate, USERNAME, PASSWORD);

webClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
response = webClient.UploadData(
        LOGIN_URL, "POST", Encoding.ASCII.GetBytes(postData)

private string ExtractViewState(string s)
   string viewStateNameDelimiter = "__VIEWSTATE";
   string valueDelimiter = "value=\"";
   int viewStateNamePosition = s.IndexOf(viewStateNameDelimiter);     
   int viewStateValuePosition = s.IndexOf(
         valueDelimiter, viewStateNamePosition

   int viewStateStartPosition = viewStateValuePosition + 
   int viewStateEndPosition = s.IndexOf("\"", viewStateStartPosition);

   return HttpUtility.UrlEncodeUnicode(
               viewStateEndPosition - viewStateStartPosition

Initial URL

Initial Description
How to use WebClient to post to an ASP.Net page and maintain viewstate for proxy purposes.

Initial Title
Screen Scraping, ViewState, and Authentication using ASP.Net

Initial Tags
c, aspnet

Initial Language