zoukankan      html  css  js  c++  java
  • 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 = "
    ";
    
                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

    本文来自博客园,作者:.net&new,转载请注明原文链接:https://www.cnblogs.com/wugh8726254/p/15217108.html

  • 相关阅读:
    解决adb的"more than one device and emulator"错误
    Swoole实现简单的http服务器
    Unity实现物体位置变换
    Unity实现放大或缩小某个物体
    如何使用WEBSOCKET实现前后端通信
    实体类接收Date类型
    easyExce输出Excel只有表头没有数据问题解决
    报餐统计
    使用 EasyExcel 写Excel数据(表头动态)
    bladex分页
  • 原文地址:https://www.cnblogs.com/wugh8726254/p/15217108.html
Copyright © 2011-2022 走看看