个人收藏--未整理—C# http/https 上传下载文件

c# HTTP/HTTPS 文件上传。

分类: .net 2015-02-03 08:36 541人阅读 评论(0) 收藏 举报

方法主体

[csharp] view plaincopy在CODE上查看代码片派生到我的代码片

  1. public static string MyUploader(string strFileToUpload, string strUrl, string strFileFormName, NameValueCollection querystring, CookieContainer cookies) 
  2.         { 
  3. string postdata; 
  4.             postdata = "?"; 
  5. if (querystring != null) 
  6.             { 
  7. foreach (string key in querystring.Keys) 
  8.                 { 
  9.                     postdata += key + "=" + querystring.Get(key) + "&"; 
  10.                 } 
  11.             } 
  12. //Uri uri = new Uri(strUrl + postdata);
  13.             Uri oUri = new Uri(strUrl + postdata); 
  14. string strBoundary = "----------" + DateTime.Now.Ticks.ToString("x"); 
  15. // The trailing boundary string
  16. byte[] boundaryBytes = Encoding.ASCII.GetBytes("\r\n--" + strBoundary + "\r\n"); 
  17. // The post message header
  18.             StringBuilder sb = new StringBuilder(); 
  19.             sb.Append("--"); 
  20.             sb.Append(strBoundary); 
  21.             sb.Append("\r\n"); 
  22.             sb.Append("Content-Disposition: form-data; name=\""); 
  23.             sb.Append(strFileFormName); 
  24.             sb.Append("\"; filename=\""); 
  25.             sb.Append(Path.GetFileName(strFileToUpload)); 
  26.             sb.Append("\""); 
  27.             sb.Append("\r\n"); 
  28.             sb.Append("Content-Type: "); 
  29.             sb.Append("application/octet-stream"); 
  30.             sb.Append("\r\n"); 
  31.             sb.Append("\r\n"); 
  32. string strPostHeader = sb.ToString(); 
  33. byte[] postHeaderBytes = Encoding.UTF8.GetBytes(strPostHeader); 
  34. // The WebRequest
  35.             HttpWebRequest oWebrequest = (HttpWebRequest)WebRequest.Create(oUri); 
  36. //如果是发送HTTPS请求 
  37. if (strUrl.StartsWith("https", StringComparison.OrdinalIgnoreCase)) 
  38.             { 
  39.                 ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult); 
  40.                 oWebrequest = WebRequest.Create(oUri) as HttpWebRequest; 
  41.                 oWebrequest.ProtocolVersion = HttpVersion.Version10; 
  42.             } 
  43. else
  44.             { 
  45.                 oWebrequest = WebRequest.Create(oUri) as HttpWebRequest; 
  46.             } 
  47.             oWebrequest.ContentType = "multipart/form-data; boundary=" + strBoundary; 
  48.             oWebrequest.Method = "POST"; 
  49. // This is important, otherwise the whole file will be read to memory anyway...
  50.             oWebrequest.AllowWriteStreamBuffering = false; 
  51. // Get a FileStream and set the final properties of the WebRequest
  52.             FileStream oFileStream = new FileStream(strFileToUpload, FileMode.Open, FileAccess.Read); 
  53. long length = postHeaderBytes.Length + oFileStream.Length + boundaryBytes.Length; 
  54.             oWebrequest.ContentLength = length; 
  55.             Stream oRequestStream = oWebrequest.GetRequestStream(); 
  56. // Write the post header
  57.             oRequestStream.Write(postHeaderBytes, 0, postHeaderBytes.Length); 
  58. // Stream the file contents in small pieces (4096 bytes, max).
  59. byte[] buffer = new Byte[checked((uint)Math.Min(4096, (int)oFileStream.Length))]; 
  60. int bytesRead = 0; 
  61. while ((bytesRead = oFileStream.Read(buffer, 0, buffer.Length)) != 0) 
  62.                 oRequestStream.Write(buffer, 0, bytesRead); 
  63.             oFileStream.Close(); 
  64. // Add the trailing boundary
  65.             oRequestStream.Write(boundaryBytes, 0, boundaryBytes.Length); 
  66.             WebResponse oWResponse = oWebrequest.GetResponse(); 
  67.             Stream s = oWResponse.GetResponseStream(); 
  68.             StreamReader sr = new StreamReader(s); 
  69.             String sReturnString = sr.ReadToEnd(); 
  70. // Clean up
  71.             oFileStream.Close(); 
  72.             oRequestStream.Close(); 
  73.             s.Close(); 
  74.             sr.Close(); 
  75. return sReturnString; 
  76.         } 

[csharp] view plaincopy在CODE上查看代码片派生到我的代码片

  1. private static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors) 
  2.         { 
  3. return true; //总是接受 
  4.         } 

调用方法

[csharp] view plaincopy在CODE上查看代码片派生到我的代码片

  1. CookieContainer cookies = new CookieContainer(); 
  2. //add or use cookies 
  3. NameValueCollection querystring = new NameValueCollection(); 
  4. querystring["login_id"] = "your userid"; 
  5. querystring["password"] = "your password"; 
  6. string uploadfile = @"C:\Test.zip";// set to file to upload 
  7. string outdata = MyUploader(filePath, updateUrl, "zipfile", querystring, cookies);

转载于:https://www.cnblogs.com/MatureGinger/p/4776363.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值