《软件测试自动化之道》读书笔记 之 请求-响应测试

《软件测试自动化之道》读书笔记 之 请求-响应测试

2016-12-06 

目录

1 发送一个简单的HTTP GET请求并取回响应
2 发送带有认证信息的HTTP请求并取回响应
3 发送复杂的HTTP GET请求并取回响应
4 发送简单的HTTP Post请求给传统的ASP网页
5 发送HTTP POST请求给ASP.ENT WEB应用程序

 

.NET框架提供了三种基本方法和两种底层方法用来发送HTTP请求和取回HTTP相应。下面从容易但不灵活,到不容易但灵活的循序,列出这5种方法(这篇文章只用到前三种):

  • WebClient:使用简单但不能发送认证信息。
  • WebRequest-WebResponse:赋予你更大的灵活性,包括发送认证信息。
  • HttpWebRequest-HttpWebResponse:更为复杂,但赋予你更多控制能力
  • TCPClient:你可以使用的一个底层类,但不常用到
  • Socket:非常底层,不长用到

WebClient、WebRequest-WebResponse、HttpWebRequest-HttpWebResponse命名空间:

using System.Net;

 

1 发送一个简单的HTTP GET请求并取回响应


 返回

被测网站:在IIS6上部署aspx网站

测试代码

        public static void SimpleHttpGet()
        {
            string uri = "http://localhost:82/WebForm.aspx";

            WebClient wc = new WebClient();
            Console.WriteLine("Sending an HTTP GET request to " + uri);
            byte[] bsReponse = wc.DownloadData(uri);
            string strResponse = Encoding.ASCII.GetString(bsReponse);
            Console.WriteLine("HTTP response is: ");
            Console.WriteLine(strResponse);
            
            Console.Read();
        }

结果

2 发送带有认证信息的HTTP请求并取回响应


 返回

测试代码

        public static void HttpGetwithCredential()
        {
            string uri ="http://localhost:82/WebForm.aspx";
            WebRequest wreq=WebRequest.Create(uri);
            string uid="";
            string pwd="";
            string domain="";

            //发送带有网络认证证书的请求
            NetworkCredential nc=new NetworkCredential(uid,pwd,domain);
            wreq.Credentials=nc;
            Console.WriteLine("Sending authenticated request to "+uri);
            WebResponse wres=wreq.GetResponse();
            Stream st=wres.GetResponseStream();
            StreamReader sr=new StreamReader(st);
            string res=sr.ReadToEnd();

            sr.Close();
            st.Close();
            Console.WriteLine("HTTP response is ");
            Console.WriteLine(res);
            Console.Read();
        }

3 发送复杂的HTTP GET请求并取回响应


 返回

测试代码

        public static void ComplexHttpGet()
        {
            string uri = "http://localhost:82/WebForm.aspx";

            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(uri);
            //HttpWebRequest的一些属性对轻量级自动化测试非常重要
            req.Method = "GET";
            req.MaximumAutomaticRedirections = 3;
            req.Timeout = 5000;

            Console.WriteLine("Sending HTTP request");
            HttpWebResponse res = (HttpWebResponse)req.GetResponse();
            Stream resst = res.GetResponseStream();
            StreamReader sr = new StreamReader(resst);
                        
            Console.WriteLine("HTTP response is ");
            Console.WriteLine(sr.ReadToEnd());
            sr.Close();
            resst.Close();
            Console.Read();
        }

4 发送简单的HTTP Post请求给传统的ASP网页


 返回

被测网站内容如下:

classic.html

<html>
<!-- classic.html-->
<body>
    <form name="theForm" method="post" action="classic.asp"
        <p>
            Enter color
            <input type="text" name="inputBox1" />
        </p>
        <input type="submit" value="Send It" />
    </form>
</body>
</html>

classic.asp

<html>
<!-- classic.asp-->
<body>
    <p>You submitted: </p>
    <%
        strColor=Request.Form("inputBox1")
        Response.Write(strColor)
    %>
    <p>Bye</p>
</body>
</html>

部署参考在IIS6上部署aspx网站

测试代码

        public static void SimpleHttpPostAsp()
        {
            string uri = "http://localhost:83/classic.asp";
            string data = "inputBox1=orange";
            byte[] buffer = Encoding.ASCII.GetBytes(data);
            
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(uri);
            req.Method = "POST";
            req.ContentType = "application/x-www-form-urlencoded";
            req.ContentLength = buffer.Length;

            Stream reqst = req.GetRequestStream();
            reqst.Write(buffer, 0, buffer.Length); //把要发送的书写入数据流
            reqst.Flush();
            reqst.Close();

            Console.WriteLine("\nPosting 'orange'");
            HttpWebResponse res = (HttpWebResponse)req.GetResponse();
            Stream resst = res.GetResponseStream();
            StreamReader sr = new StreamReader(resst);
            
            Console.WriteLine("HTTP response is ");
            Console.WriteLine(sr.ReadToEnd());
            sr.Close();
            resst.Close();
            Console.Read();
        }

结果:

5 发送HTTP POST请求给ASP.ENT WEB应用程序


 返回

测试代码

        public static void HttpPostAspx()
        {
            string uri = "http://localhost:82/WebForm.aspx";
            
            string data = 
                "__VIEWSTATE=%2FwEPDwULLTEyNDE2MjMxNzNkZEwWGp029qxg0ycbgB3UBeugsJ04SyNbOLNpc95Kixqc"
                +"&__VIEWSTATEGENERATOR=7F653548"
                +"&__EVENTVALIDATION=%2FwEdAAZ0lKkwvK05W5mIs1ExVxxYFJ3tieLguAzMIB3WfVcaejp0qUSTozdispJAnlxpgx51PjL8QaLFsRLYz%2B7Kfg%2Ffzfg78Z8BXhXifTCAVkevdxEghZBVv0boc2NaC2%2FzVFTRPuZ4ec8CK4rSvp0TUuP1Tl0ENioJP5V4RaWiYgS2tg%3D%3D"
                + "&DropDownList1=red&Button1=Send&TextBox1="; //当前被测示例除了DropDownList1=red需要根据实际情况变化外,其他的值都不变
            byte[] buffer = Encoding.ASCII.GetBytes(data);

            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(uri);
            req.Method = "POST";
            req.ContentType = "application/x-www-form-urlencoded";
            req.ContentLength = buffer.Length;

            Stream reqst = req.GetRequestStream();
            reqst.Write(buffer, 0, buffer.Length); //把要发送的书写入数据流
            reqst.Flush();
            reqst.Close();

            Console.WriteLine("\nPosting 'orange'");
            HttpWebResponse res = (HttpWebResponse)req.GetResponse();
            Stream resst = res.GetResponseStream();
            StreamReader sr = new StreamReader(resst);

            Console.WriteLine("HTTP response is ");
            Console.WriteLine(sr.ReadToEnd());
            sr.Close();
            resst.Close();
            Console.Read();
        }

结果

若发送的测试代码 data 中 DropDownList1=green,结果就会是TextBox1 value=“you picked green”。

若data内容中 __VIEWSTATE等内容不对,结果TextBox1不会返回value属性。

 

posted @ 2016-12-06 18:06  明-Ming  阅读(627)  评论(0编辑  收藏  举报