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); ;
  • 相关阅读:
    k8s dashboard 配置使用kubeconfig文件登录
    Spring Cloud 专题之七:Sleuth 服务跟踪
    Spring Cloud 专题之六:bus 消息总线
    Spring Cloud专题之五:config 配置中心
    Docker Storage Driver:存储驱动
    Docker引擎升级教程
    Docker介绍及安装详解
    Autowired和Resource的区别和联系
    OLTP与OLAP
    转载-JAVA 关于JNI本地库加载
  • 原文地址:https://www.cnblogs.com/you000/p/4108611.html
Copyright © 2011-2022 走看看