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("\r\n");  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("\r\n");  30 sb.Append("Content-Type: ");  31  sb.Append(contenttype);  32 sb.Append("\r\n");  33 sb.Append("\r\n");  34  35 string postHeader = sb.ToString();  36 byte[] postHeaderBytes = Encoding.UTF8.GetBytes(postHeader);  37 byte[] boundaryBytes = Encoding.ASCII.GetBytes("--" + boundary + "\r\n");  38 byte[] br = Encoding.ASCII.GetBytes("\r\n");  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("\r\n");  50 sub.Append("Content-Disposition: form-data; name=\"");  51  sub.Append(key);  52 sub.Append("\"");  53 sub.Append("\r\n");  54 sub.Append("\r\n");  55  sub.Append(querystring[key]);  56 sub.Append("\r\n");  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("\r\n");  81 sub.Append("Content-Disposition: form-data; name=\"");  82  sub.Append(key);  83 sub.Append("\"");  84 sub.Append("\r\n");  85 sub.Append("\r\n");  86  sub.Append(querystring[key]);  87 sub.Append("\r\n");  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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM