C# 類比webform裡面按鈕的點擊事件

來源:互聯網
上載者:User

標籤:

產生的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(&#39;btnLogin&#39;,&#39;&#39;);" 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裡面按鈕的點擊事件

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.