zoukankan      html  css  js  c++  java
  • 单元测试以文件流的形势传参调用api进行测试

            [TestMethod]
            public void Test()
            {
                byte[] buffer;//文件转换为二进制流
                string path = @"E:aaa";
                Encoding code = Encoding.GetEncoding("utf-8");
                string[] files = Directory.GetFiles(path, "1.pdf");
    
                //把数组转换成流中所需字节数组类型
                FileStream stream = new FileInfo(files[0]).OpenRead();
                buffer = new byte[stream.Length];
                stream.Read(buffer, 0, Convert.ToInt32(stream.Length));
    
                //请求远程HTTP
                string strResult = "";
                try
                {
                    //设置HttpWebRequest基本信息
                    HttpWebRequest myReq = (HttpWebRequest)HttpWebRequest.Create(Api.TestUrl);
                    myReq.Method = "post";
                    myReq.ContentType = "application/octet-stream";
    
    
                    //填充POST数据
                    myReq.ContentLength = buffer.Length;
                    Stream requestStream = myReq.GetRequestStream();
                    requestStream.Write(buffer, 0, buffer.Length);
                    requestStream.Close();
    
                    //发送POST数据请求服务器
                    HttpWebResponse HttpWResp = (HttpWebResponse)myReq.GetResponse();
                    Stream myStream = HttpWResp.GetResponseStream();
    
                    //获取服务器返回信息
                    StreamReader reader = new StreamReader(myStream, code);
                    StringBuilder responseData = new StringBuilder();
                    String line;
                    while ((line = reader.ReadLine()) != null)
                    {
                        responseData.Append(line);
                    }
    
                    //释放
                    myStream.Close();
    
                    strResult = responseData.ToString();
                }
                catch (Exception exp)
                {
                    strResult = "报错:" + exp.Message;
                }
            }

                      这个方法还是查询出来的,以免自己下次遇到忘记,记录了一下

  • 相关阅读:
    完了!生产事故!几百万消息在消息队列里积压了几个小时!
    crontab详解
    系统架构中为什么要引入消息中间件
    Linux常用命令
    什么是JWT(JSON WEB TOKEN)
    API接口安全性设计
    MySQLDump在使用之前一定要想到的事情
    api接口安全以及https
    shell study
    linux中注册系统服务—service命令的原理通俗
  • 原文地址:https://www.cnblogs.com/dyxd/p/6542639.html
Copyright © 2011-2022 走看看