• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

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

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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

 

字符串访问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;
                }
            }

        }

 Json数据处理:

JObject jobj = JObject.Parse(response);
//想取值就在jobj后面加节点就行
var OrderId = jobj["data"]["Orders"][i]["OrderId"];
//如想取多重嵌套的Json节点个数,就用JArray
 JArray orderobj = (JArray)jobj["data"]["Orders"];
 orderobj.count;

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
cfread()在判断文件结束时需要注意的一些事发布时间:2022-07-13
下一篇:
C++与C#之辩证:圣人与巨人比较发布时间:2022-07-13
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap