• C# 使用HttpWebRequest通过PHP接口 上传文件


    1:上传文件实例

     public void UploadXMLLog(string xmlpath)
            {
                NameValueCollection nvc = new NameValueCollection();
                CookieContainer cookies = new CookieContainer();
                nvc.Add("", “”);
                ......
                string url = "UrlPath";
                string res = UploadFile(xmlpath, url, nvc, cookies);
            }

    2:UploadFile源码

      1  public string UploadFile(string uploadfile, string url, NameValueCollection querystring, CookieContainer cookies, string fileFormName = "file", string contenttype = "multipart/form-data")
      2         {
      3             if ((fileFormName == null) ||
      4                     (fileFormName.Length == 0))
      5             {
      6                 fileFormName = "file";
      7             }
      8 
      9             if ((contenttype == null) ||
     10                 (contenttype.Length == 0))
     11             {
     12                 contenttype = "application/octet-stream";
     13             }
     14             Uri uri = new Uri(url);
     15             string boundary = "----------" + DateTime.Now.Ticks.ToString("x");
     16             HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(uri);
     17             webrequest.CookieContainer = cookies;
     18             webrequest.ContentType = "multipart/form-data; boundary=" + boundary;
     19             webrequest.Method = "POST";
     20             StringBuilder sb = new StringBuilder();
     21             sb.Append("--");
     22             sb.Append(boundary);
     23             sb.Append("
    ");
     24             sb.Append("Content-Disposition: form-data; name="");
     25             sb.Append(fileFormName);
     26             sb.Append(""; filename="");
     27             sb.Append(uploadfile);
     28             sb.Append(""");
     29             sb.Append("
    ");
     30             sb.Append("Content-Type: ");
     31             sb.Append(contenttype);
     32             sb.Append("
    ");
     33             sb.Append("
    ");
     34 
     35             string postHeader = sb.ToString();
     36             byte[] postHeaderBytes = Encoding.UTF8.GetBytes(postHeader);
     37             byte[] boundaryBytes = Encoding.ASCII.GetBytes("--" + boundary + "
    ");
     38             byte[] br = Encoding.ASCII.GetBytes("
    ");
     39             FileStream fileStream = new FileStream(uploadfile, FileMode.Open, FileAccess.Read);
     40             long length = postHeaderBytes.Length + fileStream.Length + br.Length;
     41             if (querystring != null)
     42             {
     43 
     44                 StringBuilder sub = new StringBuilder();
     45                 foreach (string key in querystring.Keys)
     46                 {
     47                     sub.Append("--");
     48                     sub.Append(boundary);
     49                     sub.Append("
    ");
     50                     sub.Append("Content-Disposition: form-data; name="");
     51                     sub.Append(key);
     52                     sub.Append(""");
     53                     sub.Append("
    ");
     54                     sub.Append("
    ");
     55                     sub.Append(querystring[key]);
     56                     sub.Append("
    ");
     57                     byte[] formitembytes = Encoding.UTF8.GetBytes(sub.ToString());
     58                     length += formitembytes.Length;
     59                 }
     60             }
     61             length += boundaryBytes.Length;
     62             webrequest.ContentLength = length;
     63             Stream requestStream = webrequest.GetRequestStream();
     64             // Write out our post header 
     65             requestStream.Write(postHeaderBytes, 0, postHeaderBytes.Length);
     66 
     67             // Write out the file contents 
     68             byte[] buffer = new Byte[checked((uint)Math.Min(4096, (int)fileStream.Length))];
     69             int bytesRead = 0;
     70             while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
     71                 requestStream.Write(buffer, 0, bytesRead);
     72             requestStream.Write(br, 0, br.Length);
     73             if (querystring != null)
     74             {
     75                 StringBuilder sub = new StringBuilder();
     76                 foreach (string key in querystring.Keys)
     77                 {
     78                     sub.Append("--");
     79                     sub.Append(boundary);
     80                     sub.Append("
    ");
     81                     sub.Append("Content-Disposition: form-data; name="");
     82                     sub.Append(key);
     83                     sub.Append(""");
     84                     sub.Append("
    ");
     85                     sub.Append("
    ");
     86                     sub.Append(querystring[key]);
     87                     sub.Append("
    ");
     88                     byte[] formitembytes = Encoding.UTF8.GetBytes(sub.ToString());
     89                     requestStream.Write(formitembytes, 0, formitembytes.Length);
     90                 }
     91             }
     92             // Write out the trailing boundary   
     93             requestStream.Write(boundaryBytes, 0, boundaryBytes.Length);
     94             webrequest.Timeout = 1000000;
     95 
     96             WebResponse responce = webrequest.GetResponse();
     97 
     98             Stream s = responce.GetResponseStream();
     99 
    100             StreamReader sr = new StreamReader(s);
    101 
    102             string str = sr.ReadToEnd();
    103 
    104 
    105             fileStream.Close();
    106 
    107             requestStream.Close();
    108 
    109             sr.Close();
    110 
    111             s.Close();
    112 
    113             responce.Close();
    114 
    115             return str;
    116 
    117         }
    View Code


    感谢:外国友人http://blogs.msdn.com/b/johan/archive/2006/11/15/are-you-getting-outofmemoryexceptions-when-uploading-large-files.aspx

  • 相关阅读:
    对于在git上面拉代码报"error: RPC failed; curl 56 OpenSSL SSL_read: SSL_ERROR_SYSCALL, errno 10054"解决方法
    在vue项目中如何添加eslint
    vscode编辑如何保存时自动校准eslint规范
    css3动画
    JS中的深拷贝与浅拷贝
    JS中的防抖与节流
    金三银四求职季,前端面试题小梳理(HTML、CSS、JS)
    正则表达式元字符大整理
    常规正则表达式练习,一起来开心的掉发吧
    关于子元素的margin-top对父级容器无效
  • 原文地址:https://www.cnblogs.com/lewisli/p/4129107.html
Copyright © 2020-2023  润新知