標籤:
產生的html內容
<body> <form method="post" action="./Login.aspx" id="form1"><div class="aspNetHidden"><input type="hidden" name="__EVENTTARGET" id="__EVENTTARGET" value="" /><input type="hidden" name="__EVENTARGUMENT" id="__EVENTARGUMENT" value="" /><input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUKMTE3NTYyOTE3Ng9kFgICAQ9kFgICBQ8PZBYCHgdvbmNsaWNrBS50aGlzLmRpc2FibGVkPXRydWU7X19kb1Bvc3RCYWNrKCdidG5Mb2dpbicsJycpZGTYVIQduJiktQAsjmET0WpCKVEwoSyPSdqBY5R5rDqlKA==" /></div><script type="text/javascript">//<![CDATA[var theForm = document.forms[‘form1‘];if (!theForm) { theForm = document.form1;}function __doPostBack(eventTarget, eventArgument) { if (!theForm.onsubmit || (theForm.onsubmit() != false)) { theForm.__EVENTTARGET.value = eventTarget; theForm.__EVENTARGUMENT.value = eventArgument; theForm.submit(); }}//]]></script><div class="aspNetHidden"> <input type="hidden" name="__VIEWSTATEGENERATOR" id="__VIEWSTATEGENERATOR" value="82312306" /> <input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="/wEdAASgOrQ71iMnYy6bc/2yqhwyR1LBKX1P1xh290RQyTesRe73C5Hghb+Z/bZTMreJjC6inihG6d/Xh3PZm3b5AoMQw8nbR9PLyUvJjDo4AdVamTiJMoO8PpF19MUnfVREfjA=" /></div> <div class="top_div" style="text-align:center;"></div> <div style="background: rgb(255, 255, 255); margin: -100px auto auto; border: 1px solid rgb(231, 231, 231); border-image: none; width: 400px; height: 230px; text-align: center;"> <div style="width: 165px; height: 96px; position: absolute;"> <div class="tou"></div> <div class="initial_left_hand" id="left_hand"></div> <div class="initial_right_hand" id="right_hand"></div> </div> <P style="padding: 30px 0px 10px; position: relative;"> <span class="u_logo"></span> <input name="UserName" type="text" id="UserName" class="ipt" placeholder="請輸入使用者名稱" /> </P> <P style="position: relative;"> <span class="p_logo"></span> <input name="UserPass" type="password" id="UserPass" class="ipt" placeholder="請輸入密碼" /> </P> <div style="height: 50px; line-height: 50px; margin-top: 30px; border-top-color: rgb(231, 231, 231); border-top-width: 0px; border-top-style: solid;"> <P style="margin: 0px 35px 20px 45px;"> <input type="submit" name="btnLogin" value="登入" onclick="this.disabled=true;__doPostBack('btnLogin','');" id="btnLogin" style="height:28px;width:60px;" /> </P> </div> <div style="height: 20px; line-height: 20px; margin-top: 0px; border-top-color: rgb(231, 231, 231); border-top-width: 0px; border-top-style: solid;"> <span id="lblMsg" style="color:Red;background-color:Transparent;"></span> </div> </div> <div style="text-align:center;"></div> </form></body></html>
上面二行加粗的是必帶的viewstatus屬性
下面是類比請求的代碼
string strViewState = System.Web.HttpUtility.UrlEncode("/wEPDwUKMTE3NTYyOTE3Ng9kFgICAQ9kFgICBQ8PZBYCHgdvbmNsaWNrBS50aGlzLmRpc2FibGVkPXRydWU7X19kb1Bvc3RCYWNrKCdidG5Mb2dpbicsJycpZGTYVIQduJiktQAsjmET0WpCKVEwoSyPSdqBY5R5rDqlKA=="); string strEventValidation = System.Web.HttpUtility.UrlEncode("/wEdAASgOrQ71iMnYy6bc/2yqhwyR1LBKX1P1xh290RQyTesRe73C5Hghb+Z/bZTMreJjC6inihG6d/Xh3PZm3b5AoMQw8nbR9PLyUvJjDo4AdVamTiJMoO8PpF19MUnfVREfjA="); StringBuilder url = new StringBuilder(); url.Append("UserPass=123456"); url.Append("&UserName=whtydn"); url.Append("&UserName=whtydn"); url.Append("&__VIEWSTATE="+ strViewState); url.Append("&__EVENTVALIDATION=" + strEventValidation); url.Append("&btnLogin=登入"); byte[] data = System.Text.Encoding.ASCII.GetBytes(url.ToString()); Uri uri = new Uri("http://www.etaor.com/Admin/Login.aspx"); System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(uri); request.Method = "post"; request.ContentType = "application/x-www-form-urlencoded"; request.ContentLength = data.Length; Stream requestStream = request.GetRequestStream(); requestStream.Write(data, 0, data.Length); requestStream.Close(); System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse(); Stream responseStream = response.GetResponseStream(); StreamReader readStream = new StreamReader(responseStream, System.Text.Encoding.UTF8); string retext = readStream.ReadToEnd().ToString(); readStream.Close(); Console.WriteLine("Result: " + retext);
C# 類比webform裡面按鈕的點擊事件