01 protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
02 {
03
04 }
05
06 protected void Button1_Click(object sender, EventArgs e)
07 {
08 if (DropDownList1.SelectedValue == "Two")
09 {
10 lblInfor.Text = "Two";
11 }
12 else
13 {
14 lblInfor.Text = "One";
15 }
16 }
功能代码非常简单,Button1被单击后显示dropdownlist 的文本值:
这里的OnePost 与TwoPost 分别模拟WebForm中 Post 按钮Click功能。
贴出模拟的核心代码:PostByWebRequest 函数:
01 private void PostByWebRequest(string strPostValue)
02 {
03 try
04 {
05 string URI = ":2026/webform1.aspx/";
06 HttpWebRequest request = WebRequest.Create(URI) as HttpWebRequest;
07 request.Method = "GET";
08 request.KeepAlive = true;
09 request.CookieContainer = cookieContainer;
10 HttpWebResponse response = request.GetResponse() as HttpWebResponse;
11 System.IO.Stream responseStream = response.GetResponseStream();
12 System.IO.StreamReader reader = new System.IO.StreamReader(responseStream, Encoding.UTF8);
13 //返回的页面html文本
14 string srcString = reader.ReadToEnd();
15 //VeiwState
16 string viewStateFlag = "id=\"__VIEWSTATE\" value=\"";
17 int len1 = srcString.IndexOf(viewStateFlag) + viewStateFlag.Length;
18 int len2 = srcString.IndexOf("\"", len1);
19 string viewState = srcString.Substring(len1, len2 - len1);
20 //EventValidation
21 string eventValidationFlag = "id=\"__EVENTVALIDATION\" value=\"";
22 len1 = srcString.IndexOf(eventValidationFlag) + eventValidationFlag.Length;
23 len2 = srcString.IndexOf("\"", len1);
24 string eventValidation = srcString.Substring(len1, len2 - len1);
25
26 //编码
27 viewState = System.Web.HttpUtility.UrlEncode(viewState);
28 eventValidation = System.Web.HttpUtility.UrlEncode(eventValidation);
29
30 //这里可以通过抓包工具获得poststring.记得中文需要UrlEncode编码。
31 string formatString = "DropDownList1={0}&Button1={1}&__VIEWSTATE={2}&__EVENTVALIDATION={3}";
32 string postString = string.Format(formatString, strPostValue,
33 "Do PostBack", viewState, eventValidation);
34
35 byte[] postData = Encoding.UTF8.GetBytes(postString);
36
37 URI = ":2026/webform1.aspx/";
38 //POST
39 request = WebRequest.Create(URI) as HttpWebRequest;
40 request.Method = "POST";
41 request.KeepAlive = false;
42 request.ContentType = "application/x-www-form-urlencoded";
43 request.CookieContainer = cookieContainer;
44 request.ContentLength = postData.Length;
45
46 System.IO.Stream outputStream = request.GetRequestStream();
47 outputStream.Write(postData, 0, postData.Length);
48 outputStream.Close();
49
50 response = request.GetResponse() as HttpWebResponse;
51 responseStream = response.GetResponseStream();
52 reader = new System.IO.StreamReader(responseStream, Encoding.UTF8);
53 srcString = reader.ReadToEnd();
54 }
55 catch (Exception ex)
56 {
57 string msg = ex.Message;
58 MessageBox.Show(ex.Message);
59 }
60 }
PostByWebRequest函数调用如下:
OnePost 按钮单击执行: PostByWebRequest("One");
TwoPost 按钮单击执行: PostByWebRequest("Two");
注:如果在PostByWebRequest函数的第31行,不传入ViewState或EVENTVALIDATION,WinForm内的两个按钮均不能成功模拟WebForm。最终模拟的正确效果是:预先在WebForm 的 Button_Click内设置断点,接着点击WinForm内的两个按钮,VS会自动截获到该断点设置。说明我们顺利模拟了WebForm的POST的机制。如果点击TwoPost 按钮时,WebForm 服务器端不但会执行Post按钮事件还会执行DropDownlist 的SelectedIndexChanged事件。为什么会执行到DropDownlist 的SelectedIndexChanged事件呢?这一切都归功于ViewState。它记录了WebForm的表单内容从而做出了相应的处理。所以WebForm 的 ViewState 机制还是很强大的。让我们用事件机制去开发Web程序。同时ViewState机制让我们的web form 程序更难被“爬虫”获取程序内容,想用程序Post WebForm 就必须传入传入ViewState 。否则POST不会返回正确的页面内容。