代碼測試 Scriptmanager 控制的異步頁面

1. web 2.0, 測試建立在 aspx 上。

首先,確定 scriptManager 的 SupportsPartialRendering 屬性 為 true

 <asp:ScriptManager ID="ScriptManager1" runat="server" SupportsPartialRendering="true">
    </asp:ScriptManager>


2. 然後,在代碼中要訪問由 script manager 控制的頁面,

a,創建 uri

b,設定 host, port, 並且 set method,以及設置 requestParameter

c,把 contentType 設定為

application/x-www-form-urlencoded
d,然後,用 stream write 發送請求

這樣就可以發送帶 parameter 的 post 。

重要的要注意的地方,

千萬記得,在 response 得到後,關掉 response..   res.close();!

            try
            {
                UriBuilder addressbuilder = new UriBuilder(hostText.Text.Trim());
                addressbuilder.Port = int.Parse(portText.Text.Trim());

                this.displayInfo(addressbuilder.Uri.ToString());

                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(addressbuilder.Uri);
                // SET POST
                req.Method = "POST";
                req.KeepAlive = true;

                String reqestParameter = "ScriptManager1=actionUpdatePanel|defaultStockTimer" +
                                        "&__EVENTTARGET=defaultStockTimer" +
                                        "&__EVENTARGUMENT=" +
                                        "&__EVENTVALIDATION=/wEWBQL8lJvABQL/wp2hCwLejM6fDwKl8a/HDAL22+6ODiRtlX7qRZMcbQchySeGh6QUKyAz" +
                                        "&__ASYNCPOST=true";
                byte[] rebyte = ASCIIEncoding.UTF8.GetBytes(reqestParameter);

                req.ContentType = "application/x-www-form-urlencoded";
                req.ContentLength = rebyte.Length;

               
                Stream reqs = req.GetRequestStream();
                reqs.Write(rebyte, 0, rebyte.Length);

                reqs.Close();

                if (req == null)
                {
                    this.displayInfo("Request is null.");
                }
                else
                {
                    HttpWebResponse res = (HttpWebResponse)req.GetResponse();
                    this.displayInfo("Get Response Length:" + res.ContentLength.ToString());
                    Stream s = res.GetResponseStream();
                    StreamReader sr = new StreamReader(s);

                    this.displayInfo(sr.ReadToEnd());
                    sr.Close();
                }

            }
            catch (Exception ex)
            {
                this.displayInfo("Error in URL or PORT." + Environment.NewLine + ex.ToString());
            }

Post 的數據,通過使用 fwptt 這個軟件可以得到。下載地址:http://fwptt.sourceforge.net/index.html


恩,繼續。

如果你想要測試,那麼還需要關掉網站的保護系統。

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="isrBank.aspx.cs" Inherits="AWS_LB.isrBank" EnableEventValidation="false" %>
將 EnableEventValidation 設定為 false 即可。

不過這樣的話網站肯定就會有問題。

比如微軟提到的 Injection..

不過這次的測試就是為了 injection

所以恩。。你懂的。


最後一步,就是操作 Session

Session 的話,就需要 CookieContainer 了。

首先創建 CookieContainer 然後在發送 Request 的時候加入即可。

        private CookieContainer cc;
        public WLG()
        {
            InitializeComponent();
            cc = new CookieContainer();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            try
            {
                UriBuilder addressbuilder = new UriBuilder(hostText.Text.Trim());
                addressbuilder.Port = int.Parse(portText.Text.Trim());

                this.displayInfo(addressbuilder.Uri.ToString());

                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(addressbuilder.Uri);
                // SET POST
                req.CookieContainer = cc; // <<COOKIE CONTAINER HERE
                req.Method = "POST";
                req.KeepAlive = true;

                String reqestParameter = "ScriptManager1=contentUpdatePanel|loginFuncButton" +
                                        "&usernameText=" + usernameText.Text.Trim() +
                                        "&passwordText=" + passwordText.Text.Trim() +
                                        //"&__EVENTVALIDATION=/wEWCAL11oX8DQLejM6fDwKl8a/HDAL22+6ODgKn+4ShDwLpg8H7AwK/uYXQBwL/wp2hC2dv3TV/KVZmPt6Oiw3A1S+q8PP+" +
                                        "&__EVENTTARGET=" +
                                        "&__EVENTARGUMENT=" +
                                        "&__ASYNCPOST=true" + 
                                        "&loginFuncButton=Login";
                byte[] rebyte = ASCIIEncoding.UTF8.GetBytes(reqestParameter);

                req.ContentType = "application/x-www-form-urlencoded";
                req.ContentLength = rebyte.Length;


                Stream reqs = req.GetRequestStream();
                reqs.Write(rebyte, 0, rebyte.Length);

                reqs.Close();

                if (req == null)
                {
                    this.displayInfo("Request is null.");
                }
                else
                {
                    HttpWebResponse res = (HttpWebResponse)req.GetResponse();
                    this.displayInfo("Get Response Length:" + res.ContentLength.ToString());
                    Stream s = res.GetResponseStream();
                    StreamReader sr = new StreamReader(s);

                    String content = sr.ReadToEnd();
                    this.displayInfo(content);
                    webBrowser1.DocumentText = content;

                    foreach (Cookie ck in res.Cookies)
                    {
                        this.displayInfo(ck.Name + "=" + ck.Value);
                    }
                }

            }
            catch (Exception ex)
            {
                this.displayInfo("Error in URL or PORT." + Environment.NewLine + ex.ToString());
            }

需要調用Session的時候,使用 Cookie 容器即可。

            try
            {
                UriBuilder addressbuilder = new UriBuilder("http://localhost/isrStockDetail.aspx");
                addressbuilder.Port = int.Parse(portText.Text.Trim());

                this.displayInfo(addressbuilder.Uri.ToString());

                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(addressbuilder.Uri);
                // SET POST
                req.CookieContainer = cc;
                req.Method = "POST";
                req.KeepAlive = true;

                String reqestParameter = "action=commentUser" +
                                        "&stockName=NASDAQ:AAPL" +
                                        "&ScriptManager1=UpdatePanel1|Button1" +
                                        "&TextBox1=nihaoma" +
                                        "&Button1=Push" +
                                        "&__ASYNCPOST=true" +
                                        //"&__VIEWSTATE=/wEPDwUJNjI1MDgxOTc5D2QWAgIDD2QWBAIDDw8WAh4EVGV4dAUHRVRSOkRQV2RkAgUPZBYCZg9kFgQCAQ8PFgIfAAUWTm8gY29tbWVudCBmb3IgRVRSOkRQV2RkAgMPDxYCHgdWaXNpYmxlZ2RkZPaHUZEAUJOnc6ZUSSkikUVC6ozH" +
                                        //"&__EVENTVALIDATION=/wEWBAKI3d6ODwKM54rGBgLWlM+bAgLs0bLrBuOLecbcd4U2Nl48r9WiUeNCxXW5" +
                                        "&__EVENTTARGET=" +
                                        "&__EVENTARGUMENT=";
                                    
                byte[] rebyte = ASCIIEncoding.UTF8.GetBytes(reqestParameter);

                req.ContentType = "application/x-www-form-urlencoded";
                req.ContentLength = rebyte.Length;


                Stream reqs = req.GetRequestStream();
                reqs.Write(rebyte, 0, rebyte.Length);

                reqs.Close();

                if (req == null)
                {
                    this.displayInfo("Request is null.");
                }
                else
                {
                    HttpWebResponse res = (HttpWebResponse)req.GetResponse();
                    this.displayInfo("Get Response Length:" + res.ContentLength.ToString());
                    Stream s = res.GetResponseStream();
                    StreamReader sr = new StreamReader(s);

                    String content = sr.ReadToEnd();
                    this.displayInfo(content);
                    webBrowser1.DocumentText = content;
                }

            }
            catch (Exception ex)
            {
                this.displayInfo("Error in URL or PORT." + Environment.NewLine + ex.ToString());
            }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值