eventvalidation error for screen scraping
<p>I'm trying to scrape the login page of a site I work on and submit a username/password via code to log into the site. I want to do this in a checking service for site health reasons. I'm running into a few issues, the first of which deals with getting this message:</p>

<p><code>Exception information: 
    Exception type: ArgumentException 
    Exception message: Invalid postback or callback argument.  Event validation is enabled using &lt;pages enableEventValidation="true"/&gt; in configuration or &lt;%@ Page EnableEventValidation="true" %&gt; in a page.  For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them.  If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation.</code></p>

<p>I've found sites that say I'd have to turn eventvalidation off, but I don't want to do that for security reasons. Is there a way to get around this?</p>

<p>Here is the code. I basically took it right off K. Scott Allen's article here: <a href="http://odetocode.com/Articles/162.aspx" rel="nofollow">http://odetocode.com/Articles/162.aspx</a></p>

<pre><code>  StringBuilder sb = new StringBuilder();
        var encryptedConnectionString = GlobalDataObject.EncryptSecure("conn string here", GlobalDataObject.Seed);
        sb.AppendFormat("client_id={0};", "client");
        sb.AppendFormat("client_directory={0};", "client");
        sb.AppendFormat("user_id={0};", "12");
        sb.AppendFormat("conn_string={0};", encryptedConnectionString);
        StringBuilder cookiesString = sb;

        HttpWebRequest webRequest = WebRequest.Create("http://localhost/site/login.aspx?c=client") as HttpWebRequest;
        webRequest.Headers.Add("Cookie", cookiesString.ToString());
        StreamReader responseReader = new StreamReader(
              webRequest.GetResponse().GetResponseStream()
           );
        string responseData = responseReader.ReadToEnd();
        responseReader.Close();

        // extract the viewstate value and build out POST data
        string viewState = ExtractViewState(responseData);
        string postData = string.Format("__VIEWSTATE={0}&amp;Login1$Password={1}&amp;Login1$UserName={2}&amp;Login1$LoginButton={3}",
                                                            viewState,
                                                  HttpUtility.UrlEncode(username),
                                                  HttpUtility.UrlEncode(password),
                                                  "Log In");

        // have a cookie container ready to receive the forms auth cookie
        CookieContainer cookies = new CookieContainer();

        // now post to the login form
        webRequest = WebRequest.Create("http://localhost/site/login.aspx") as HttpWebRequest;
        webRequest.Method = "POST";
        webRequest.ContentType = "application/x-www-form-urlencoded";
        webRequest.CookieContainer = cookies;

        // write the form values into the request message
        StreamWriter requestWriter = new StreamWriter(webRequest.GetRequestStream());
        requestWriter.Write(postData);
        requestWriter.Close();
        webRequest.AuthenticationLevel = AuthenticationLevel.None;
        // we don't need the contents of the response, just the cookie it issues
        webRequest.GetResponse().Close();  ///ERROR HAPPENS HERE

        // now we can send out cookie along with a request for the protected page
        webRequest = WebRequest.Create("http://localhost/site/user/home.aspx") as HttpWebRequest;
        webRequest.CookieContainer = cookies;
        responseReader = new StreamReader(webRequest.GetResponse().GetResponseStream());

        // and read the response
        responseData = responseReader.ReadToEnd();
        responseReader.Close();

        return responseData;
</code></pre>

<p>Thanks. </p>
