C # Http/https File upload.
Category:. NET 2015-02-03 08:36 541 people Read reviews (0) Favorite reports
Method body
[CSharp] View plaincopy
- public static string Myuploader (String strfiletoupload, String strurl, String strfileformname, NameValueCollection QueryString, Cookiecontainer Cookies)
- {
- String PostData;
- PostData = "?";
- if (querystring! = null)
- {
- foreach (String key in QueryString. Keys)
- {
- PostData + = key + "=" + querystring. Get (Key) + "&";
- }
- }
- Uri uri = new Uri (strURL + postdata);
- Uri Ouri = new Uri (strURL + postdata);
- String strboundary = "----------" + DateTime.Now.Ticks.ToString ("x");
- The trailing boundary string
- byte[] Boundarybytes = Encoding.ASCII.GetBytes ("\r\n--" + strboundary + "\ r \ n");
- The Post message header
- StringBuilder sb = new StringBuilder ();
- Sb. Append ("--");
- Sb. Append (strboundary);
- Sb. Append ("\ r \ n");
- Sb. Append ("Content-disposition:form-data; Name=\ "");
- Sb. Append (Strfileformname);
- Sb. Append ("\"; filename=\ "");
- Sb. Append (Path.getfilename (strfiletoupload));
- Sb. Append ("\" ");
- Sb. Append ("\ r \ n");
- Sb. Append ("Content-type:");
- Sb. Append ("Application/octet-stream");
- Sb. Append ("\ r \ n");
- Sb. Append ("\ r \ n");
- String strpostheader = sb. ToString ();
- byte[] postheaderbytes = Encoding.UTF8.GetBytes (Strpostheader);
- The WebRequest
- HttpWebRequest owebrequest = (HttpWebRequest) webrequest.create (Ouri);
- If you are sending an HTTPS request
- if (Strurl.startswith ("https", StringComparison.OrdinalIgnoreCase))
- {
- Servicepointmanager.servercertificatevalidationcallback = new Remotecertificatevalidationcallback ( CheckValidationResult);
- Owebrequest = WebRequest.Create (Ouri) as HttpWebRequest;
- Owebrequest.protocolversion = Httpversion.version10;
- }
- Else
- {
- Owebrequest = WebRequest.Create (Ouri) as HttpWebRequest;
- }
- Owebrequest.contenttype = "Multipart/form-data; boundary= "+ strboundary;
- Owebrequest.method = "POST";
- This is important, otherwise the whole file would be a read to memory anyway ...
- Owebrequest.allowwritestreambuffering = false;
- Get a FileStream and set the final properties of the WebRequest
- FileStream Ofilestream = new FileStream (Strfiletoupload, FileMode.Open, FileAccess.Read);
- Long length = postheaderbytes.length + Ofilestream.length + boundarybytes.length;
- owebrequest.contentlength = length;
- Stream Orequeststream = Owebrequest.getrequeststream ();
- Write The post header
- Orequeststream.write (postheaderbytes, 0, postheaderbytes.length);
- Stream the file contents in small pieces (4096 bytes, max).
- byte[] buffer = new byte[checked (UINT) math.min (4096, (int) ofilestream.length)];
- int bytesread = 0;
- while (bytesread = ofilestream.read (buffer, 0, buffer. Length))! = 0)
- Orequeststream.write (buffer, 0, bytesread);
- Ofilestream.close ();
- ADD the trailing boundary
- Orequeststream.write (boundarybytes, 0, boundarybytes.length);
- WebResponse owresponse = Owebrequest.getresponse ();
- Stream s = Owresponse.getresponsestream ();
- StreamReader sr = new StreamReader (s);
- String sreturnstring = Sr. ReadToEnd ();
- Clean up
- Ofilestream.close ();
- Orequeststream.close ();
- S.close ();
- Sr. Close ();
- return sreturnstring;
- }
[CSharp] View plaincopy
- private static bool CheckValidationResult (object sender, X509Certificate certificate, X509chain chain, sslpolicyerrors Errors
- {
- return true; Always Accept
- }
Calling methods
[CSharp] View plaincopy
- Cookiecontainer cookies = new Cookiecontainer ();
- Add or use cookies
- NameValueCollection querystring = new NameValueCollection ();
- querystring["login_id"] = "your UserID";
- querystring["password"] = "Your password";
- String uploadfile = @ "C:\Test.zip";//set to file to upload
- String outdata = Myuploader (FilePath, UpdateUrl, "ZipFile", querystring, Cookies);
Personal collection--not organized-c# http/https upload download file