Exemple #1
0
        public void doTest()
        {
            string         myCaseToSend = "";
            myAutoHttpTest myReportData = new myAutoHttpTest("null");

            if (!isRunCase)
            {
                MessageBox.Show("项目已停止", "run stop");
                return;
            }
            string myTarget = myReceiveData.vaneUrl + parentsXml.ChildNodes[nowIndex].Attributes[1].Value;

            parentsTree.Nodes[showIcoIndex].ImageIndex         = 5;
            parentsTree.Nodes[showIcoIndex].SelectedImageIndex = 5;
            myReportData.caseId = parentsXml.ChildNodes[nowIndex].Attributes[0].Value;
            myCaseToSend        = myDataAnalysis.myCreatSendData(parentsXml.ChildNodes[nowIndex].ChildNodes[2].InnerText);
            //check the data
            if (myCaseToSend == "can't find =")
            {
                myReportData.startTime = DateTime.Now.ToString("HH:mm:ss");
                myReportData.result    = "skip ; 未发送";
                myReportData.remark    = "用例数据解析失败";
                myReportData.ret       = "break";
                parentsTree.Nodes[showIcoIndex].ImageIndex         = 13;
                parentsTree.Nodes[showIcoIndex].SelectedImageIndex = 13;
                myReceiveData.myData.Add(myReportData);
                return;
            }
            //here i will send the data
            SendData(myTarget, myCaseToSend, parentsXml.ChildNodes[nowIndex].ChildNodes[4].InnerText, ref myReportData);
            if (myReportData.result.StartsWith("Unknow"))
            {
                myReportData.remark = "未知错误";
                myReportData.ret    = "unknow";
                parentsTree.Nodes[showIcoIndex].ImageIndex         = 12;
                parentsTree.Nodes[showIcoIndex].SelectedImageIndex = 12;
                myReceiveData.myData.Add(myReportData);
                return;
            }
            if (myReportData.result.StartsWith("Error"))
            {
                myReportData.remark = "请求发送模块异常";
                myReportData.ret    = "error";
                parentsTree.Nodes[showIcoIndex].ImageIndex         = 14;
                parentsTree.Nodes[showIcoIndex].SelectedImageIndex = 14;
                myReceiveData.myData.Add(myReportData);
                return;
            }

            //now i am sure the request is ok
            if (myReportData.result == parentsXml.ChildNodes[nowIndex].ChildNodes[3].InnerText)
            {
                myReportData.ret    = "pass";
                myReportData.remark = "测试通过";
                parentsTree.Nodes[showIcoIndex].ImageIndex         = 15;
                parentsTree.Nodes[showIcoIndex].SelectedImageIndex = 15;
            }
            else
            {
                myReportData.ret    = "fail";
                myReportData.remark = "预期值:" + parentsXml.ChildNodes[nowIndex].ChildNodes[3].InnerText;
                parentsTree.Nodes[showIcoIndex].ImageIndex         = 7;
                parentsTree.Nodes[showIcoIndex].SelectedImageIndex = 7;
            }
            //fill the result here
            myReceiveData.myData.Add(myReportData);
        }
Exemple #2
0
        /// <summary>
        /// i will Send Data
        /// </summary>
        /// <param name="url"> url </param>
        /// <param name="data"> param </param>
        /// <param name="method">GET/POST</param>
        /// <param name="myAht">the myAutoHttpTest will fill the data</param>
        /// <returns>back </returns>
        public static string SendData(string url, string data, string method, ref myAutoHttpTest myAht)
        {
            string    re      = "";
            Stopwatch myWatch = new Stopwatch();

            try
            {
                //except POST other data will add the url,if you want adjust the ruleschange here
                if (method.ToUpper() != "POST")
                {
                    url += "?" + data;
                    data = null;
                }
                WebRequest wr = WebRequest.Create(url);
                wr.Method      = method;
                wr.ContentType = "application/x-www-form-urlencoded";
                char[]        reserved   = { '?', '=', '&' };
                StringBuilder UrlEncoded = new StringBuilder();
                byte[]        SomeBytes  = null;
                myAht.startTime = DateTime.Now.ToString("HH:mm:ss");
                myWatch.Start();
                if (data != null)
                {
                    SomeBytes        = Encoding.UTF8.GetBytes(data);
                    wr.ContentLength = SomeBytes.Length;
                    myWatch.Reset();
                    myWatch.Start();
                    Stream newStream = wr.GetRequestStream();
                    newStream.Write(SomeBytes, 0, SomeBytes.Length);
                    myWatch.Stop();
                    newStream.Close();
                }
                else
                {
                    wr.ContentLength = 0;
                }

                if (data == null)
                {
                    myWatch.Start();
                }
                WebResponse result = wr.GetResponse();
                if (data == null)
                {
                    myWatch.Stop();
                }

                Stream ReceiveStream = result.GetResponseStream();

                Byte[] read  = new Byte[512];
                int    bytes = ReceiveStream.Read(read, 0, 512);

                re = "";
                while (bytes > 0)
                {
                    Encoding encode = System.Text.Encoding.GetEncoding("UTF-8");
                    re   += encode.GetString(read, 0, bytes);
                    bytes = ReceiveStream.Read(read, 0, 512);
                }
            }
            catch (WebException wex)
            {
                if (wex.Response != null)
                {
                    using (var errorResponse = (HttpWebResponse)wex.Response)
                    {
                        re += "StatusCode:  " + Convert.ToInt32(((HttpWebResponse)wex.Response).StatusCode) + "\r\n";
                        using (var reader = new StreamReader(errorResponse.GetResponseStream()))
                        {
                            re += reader.ReadToEnd();
                        }
                    }
                }
                else
                {
                    re += "Unknow Error";
                }
            }
            catch (Exception ex)
            {
                re = "Error : " + ex.Message;
            }

            if (myWatch.IsRunning)
            {
                myWatch.Stop();
            }
            else
            {
                myAht.spanTime = myWatch.ElapsedMilliseconds.ToString();
            }
            myAht.result = re;
            return(re);
        }