zoukankan      html  css  js  c++  java
  • 模拟浏览器多文件上传

    string responseStr = null; 
                string boundary = "----------------------" + DateTime.Now.Ticks.ToString("x"); 
                byte[] boundarybytes = System.Text.Encoding.ASCII.GetBytes("
    --" + boundary + "
    ");
    
                HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("目标服务器地址");
                request.Method = "POST";
                request.ContentType = "multipart/form-data; boundary=" + boundary;
                request.KeepAlive = true; 
                request.Timeout = 95000;
                request.Credentials = System.Net.CredentialCache.DefaultCredentials;
                request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36";
                
                System.IO.Stream rs = request.GetRequestStream();
                string headerTemplate = "Content-Disposition: form-data; name="{0}"; filename="{1}"
    Content-Type: {2}
    
    ";   
                for (int k = 0; k < Request.Files.Count; k++)
                {
                    System.Web.HttpPostedFile hp = Request.Files[k];
                    string filename = hp.FileName;
                    //文件分隔符
                    rs.Write(boundarybytes, 0, boundarybytes.Length); 
                    //文件头 
                    byte[] headerbytes = System.Text.Encoding.UTF8.GetBytes(string.Format(headerTemplate,"file"+k, filename,hp.ContentType));
                    rs.Write(headerbytes, 0, headerbytes.Length);
                    headerbytes = new byte[hp.InputStream.Length];
                    //文件数据
                    hp.InputStream.Read(headerbytes,0,headerbytes.Length);
                    rs.Write(headerbytes, 0, headerbytes.Length); 
                }
                //结束分隔符
                byte[] trailer = System.Text.Encoding.ASCII.GetBytes("
    --" + boundary + "--
    ");
                rs.Write(trailer, 0, trailer.Length);
                rs.Close(); 
                WebResponse wresp = null;
                try
                {
                    wresp = request.GetResponse();
                    System.IO.Stream stream2 = wresp.GetResponseStream();
                    System.IO.StreamReader reader2 = new System.IO.StreamReader(stream2);
                    responseStr = reader2.ReadToEnd(); 
                }
                catch
                { 
                }
                finally
                {
                    request = null;
                } 
                Response.Write(responseStr); ;
  • 相关阅读:
    sql编码造成的安全问题(基于mysql8.0版本)
    [HY000] [2054] php连接mysql时错误
    jetbrains(phpstrom,webstorm等)破解
    XSS(二)
    XSS(一)
    ctf中rsa攻击方法
    扩展欧几里得算法证明及求乘法逆元
    mac泛洪攻击&arp欺骗(python脚本)
    Flutter 中 实现 单选对话框 和页面中实现单选框
    解决Flutter混合开发时 is not a readable directory 问题
  • 原文地址:https://www.cnblogs.com/you000/p/4108611.html
Copyright © 2011-2022 走看看