Personal collection--not organized-c# http/https upload download file

Source: Internet
Author: User

C # Http/https File upload.

Category:. NET 2015-02-03 08:36 541 people Read reviews (0) Favorite reports

Method body

[CSharp] View plaincopy

  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. If you are sending an HTTPS request
  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 would be a 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

    1. private static bool CheckValidationResult (object sender, X509Certificate certificate, X509chain chain, sslpolicyerrors Errors
    2. {
    3. return true; Always Accept
    4. }

Calling methods

[CSharp] View plaincopy

    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);

Personal collection--not organized-c# http/https upload download file

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.