码迷,mamicode.com
首页 > Windows程序 > 详细

基于C#的接口自动化测试(一)

时间:2017-01-17 09:35:50      阅读:252      评论:0      收藏:0      [点我收藏+]

标签:protocol   method   interface   serial   ted   stat   cti   false   nbsp   

其实就是找个地方然后给关键的代码做个笔记什么的……

 

字符串访问API接口,访问方法为POST:

 string url = URL;
            string RequestParam = Param;
            string headername = HeaderName;
            string header = Header;
            string html = "";
            try
            {
                WebRequest wbreq = WebRequest.Create(url);
            }

            catch (WebException WebEx)
            {
                Console.WriteLine("无法访问的URI:" + "\r\n" + WebEx.ToString());
            }

            byte[] byteArray = Encoding.UTF8.GetBytes(RequestParam);
            {
                //POST访问接口
                HttpWebRequest RequestInterfaceRequsetByString = (HttpWebRequest)HttpWebRequest.Create(new Uri(URL));
                RequestInterfaceRequsetByString.KeepAlive = false;
                RequestInterfaceRequsetByString.ProtocolVersion = HttpVersion.Version11;
                RequestInterfaceRequsetByString.Method = "post";
                RequestInterfaceRequsetByString.ContentType = "application/x-www-form-urlencoded";
                RequestInterfaceRequsetByString.Timeout = -1;//超时时间设置为无限大
                RequestInterfaceRequsetByString.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:42.0) Gecko/20100101 Firefox/42.0";
                Encoding encoding = Encoding.GetEncoding("utf-8");
                RequestInterfaceRequsetByString.Headers.Add(headername, Header);
                try
                {
                    Stream requeststream = RequestInterfaceRequsetByString.GetRequestStream();
                    requeststream.Write(byteArray, 0, byteArray.Length);
                    requeststream.Close();
                    try
                    {
                        HttpWebResponse response = (HttpWebResponse)RequestInterfaceRequsetByString.GetResponse();
                        Stream responsestream = response.GetResponseStream();
                        StreamReader sr = new StreamReader(responsestream);
                        html = sr.ReadToEnd();   //从头读到尾,放到字符串html                             
                        responsestream.Close();
                        response.Close();
                    }

                    catch (Exception ex)
                    {
                        html = ex.Message;
                    }
                }


                catch (Exception ex)
                {
                    html = ex.Message;
                }
            }

            return html;
        }

 json转字典类型:

 public static Dictionary<string, object> ConvertDictionary(string str)
        {
            JavaScriptSerializer jss = new JavaScriptSerializer();
            //string jsstr = ConvertJsonString(str);
            try
            {
                return jss.Deserialize<Dictionary<string, object>>(str);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

字典类型访问API接口:与字符串类型访问接口基本类似,但是需要加入字典相关的东西,访问方法为POST

Dictionary<string, object> requestdic = ConvertToDictionary.ConvertDictionary(RequestParam);
            StringBuilder buffer = new StringBuilder();
            int i = 0;
            foreach (string key in requestdic.Keys)
            {
                if (i > 0)
                {
                    buffer.AppendFormat("&{0}={1}", key, requestdic[key]);
                }

                else
                {
                    buffer.AppendFormat("{0}={1}", key, requestdic[key]);
                }
                i++;
            }

            byte[] byteArray = Encoding.UTF8.GetBytes(buffer.ToString());

格式化json字符串:

public static string ConvertJsonString(string str)
        {
            //格式化json字符串
            JsonSerializer serializer = new JsonSerializer();
            TextReader tr = new StringReader(str);
            JsonTextReader jtr = new JsonTextReader(tr);
             object obj = serializer.Deserialize(jtr);

                if (obj != null)
                {
                    StringWriter textWriter = new StringWriter();
                    JsonTextWriter jsonWriter = new JsonTextWriter(textWriter)
                    {
                        Formatting = Formatting.Indented,
                        Indentation = 4,
                        IndentChar =  
                    };
                    serializer.Serialize(jsonWriter, obj);
                    return textWriter.ToString();
                }

                else
                {
                    return str;
                }
            }

        }

 

基于C#的接口自动化测试(一)

标签:protocol   method   interface   serial   ted   stat   cti   false   nbsp   

原文地址:http://www.cnblogs.com/rd-ddddd/p/6291746.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!