Listing 1: Source code for the Web application

<%@ Page Language="vb" %>
<Script Language="vb" runat="server">
  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
  System.EventArgs)
    If TextBox1.Text = "red" Then
      TextBox2.Text = "roses are red"
    Else If TextBox1.Text = "blue" Then
      TextBox2.Text = "the sky is blue"
    Else If TextBox1.Text = "green" Then
      TextBox2.Text = "the sky is blue" ' deliberate logic error
    Else
      TextBox2.Text = "unknown color"
    End If
  End Sub
</Script>

<html>
  <body>
  <form method="post" runat="server" >
    Enter a color: <asp:TextBox id="TextBox1" runat="server" /> <br>
    <asp:Button id="Button1" runat="server" text="Send It"
	OnClick="Button1_Click" /> <br>
    My comment is: <asp:TextBox id="TextBox2"  runat="server" /> <br>
   </form>
  </body>
</html>

Listing 2: Source code for programmatically posting to Web application

using System;

using System.Web;  // for HttpUtility class
using System.Text; // for Encoding class
using System.Net;  // for HttpWebRequest class
using System.IO;   // for StreamReader class

namespace PostToASPdotNET
{
  class Class1
  {
    [STAThread]
    static void Main(string[] args)
    {
      string url = "http://localhost/PostToASPdotNET/colors.aspx";
      string viewstate = InitialViewState(url);

      viewstate = HttpUtility.UrlEncode(viewstate);
      string data = "TextBox1=red&TextBox2=empty&Button1=clicked&__VIEWSTATE=" +
	  viewstate;
      byte[] buffer = Encoding.UTF8.GetBytes(data);
      string proxy = null;

      HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
      req.Method = "POST";
      req.ContentType = "application/x-www-form-urlencoded";
      req.ContentLength = buffer.Length;
      req.Proxy = new WebProxy(proxy, true); // ignore for local addresses
      req.CookieContainer = new CookieContainer(); // enable cookies

      Stream reqst = req.GetRequestStream(); // add form data to request stream
      reqst.Write(buffer, 0, buffer.Length);
      reqst.Flush();
      reqst.Close();

      Console.WriteLine("\nPosting 'red' to " + url);
      HttpWebResponse res = (HttpWebResponse)req.GetResponse(); // send request,
	  get response

      Console.WriteLine("\nResponse stream is: \n");
      Stream resst = res.GetResponseStream(); // display HTTP response
      StreamReader sr = new StreamReader(resst);
      Console.WriteLine(sr.ReadToEnd());
      sr.Close();
      resst.Close();
  
      Console.WriteLine("\nDone");
      Console.ReadLine();
    } // Main()

    static string InitialViewState(string url)
    {
      WebClient wc = new WebClient();
      Stream st = wc.OpenRead(url);
      StreamReader sr = new StreamReader(st);
      string line;
      while ((line = sr.ReadLine()) != null)
      {
        if (line.IndexOf("__VIEWSTATE") != -1) // found line
        {
          sr.Close();
          st.Close();
          int startIndex = line.IndexOf("value=") + 7;
          int endIndex = line.IndexOf("\"", startIndex);
          int count = endIndex - startIndex;
          return line.Substring(startIndex, count);
        }

      }
      sr.Close();
      st.Close();
      return "not found";
    } // InitialViewState()

  } // Class1
} // ns PostToASPdotNET