post上传文件

private void post1()
        {
            string url = @"http://*******";//这里就不暴露我们的地址啦
            string modelId = "4f1e2e3d-6231-4b13-96a4-835e5af10394";
            string updateTime = "2016-11-03 14:17:25";
            string encrypt = "f933797503d6e2c36762428a280e0559";

            string filePath = @"C:/2.html";
            string fileName = "2.html";

            byte[] fileContentByte = new byte[1024]; // 文件内容二进制

            #region 将文件转成二进制

            FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
            fileContentByte = new byte[fs.Length]; // 二进制文件
            fs.Read(fileContentByte, 0, Convert.ToInt32(fs.Length));
            fs.Close();

            #endregion


            #region 定义请求体中的内容 并转成二进制

            string boundary = "ceshi";
            string Enter = "\r\n";

            string modelIdStr = "--" + boundary + Enter
                    + "Content-Disposition: form-data; name=\"modelId\"" + Enter + Enter
                    + modelId + Enter;

            string fileContentStr = "--" + boundary + Enter
                    + "Content-Type:application/octet-stream" + Enter
                    + "Content-Disposition: form-data; name=\"fileContent\"; filename=\"" + fileName + "\"" + Enter + Enter;

            string updateTimeStr = Enter + "--" + boundary + Enter
                    + "Content-Disposition: form-data; name=\"updateTime\"" + Enter + Enter
                    + updateTime;

            string encryptStr = Enter + "--" + boundary + Enter
                    + "Content-Disposition: form-data; name=\"encrypt\"" + Enter + Enter
                    + encrypt + Enter + "--" + boundary + "--";


            var modelIdStrByte = Encoding.UTF8.GetBytes(modelIdStr);//modelId所有字符串二进制

            var fileContentStrByte = Encoding.UTF8.GetBytes(fileContentStr);//fileContent一些名称等信息的二进制(不包含文件本身)

            var updateTimeStrByte = Encoding.UTF8.GetBytes(updateTimeStr);//updateTime所有字符串二进制
            
            var encryptStrByte = Encoding.UTF8.GetBytes(encryptStr);//encrypt所有字符串二进制


            #endregion


            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = "POST";
            request.ContentType = "multipart/form-data;boundary=" + boundary; 

            Stream myRequestStream = request.GetRequestStream();//定义请求流

            #region 将各个二进制 安顺序写入请求流 modelIdStr -> (fileContentStr + fileContent) -> uodateTimeStr -> encryptStr

            myRequestStream.Write(modelIdStrByte, 0, modelIdStrByte.Length);

            myRequestStream.Write(fileContentStrByte, 0, fileContentStrByte.Length);
            myRequestStream.Write(fileContentByte, 0, fileContentByte.Length);

            myRequestStream.Write(updateTimeStrByte, 0, updateTimeStrByte.Length);

            myRequestStream.Write(encryptStrByte, 0, encryptStrByte.Length);

            #endregion

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();//发送

            Stream myResponseStream = response.GetResponseStream();//获取返回值
            StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));

            string retString = myStreamReader.ReadToEnd();

            myStreamReader.Close();
            myResponseStream.Close();
        }

转自:https://www.cnblogs.com/yinq/p/6045995.html
posted @ 2021-09-02 06:35  .net&new  阅读(99)  评论(0编辑  收藏  举报