Advertisement
Guest User

unanemd

a guest
Apr 15th, 2018
1,596
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.00 KB | None | 0 0
  1. public void uploadToDrive()
  2.     {
  3.         string[] files = Directory.GetFiles(Directory.GetCurrentDirectory(), "*.txt");              
  4.         for (int i = 0; i < files.Length; i++)
  5.         {
  6.             string boundary = "----------" + DateTime.Now.Ticks.ToString("x");
  7.             ServicePointManager.ServerCertificateValidationCallback = MyRemoteCertificateValidationCallback;
  8.             HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(FILEUPLOAD_BASE_URL);            
  9.             webrequest.ContentType = "multipart/form-data; boundary=" + boundary;
  10.             webrequest.Method = "POST";            
  11.  
  12.             // Build up the post message header  
  13.             StringBuilder sb = new StringBuilder();
  14.             sb.Append("--");
  15.             sb.Append(boundary);
  16.             sb.Append("\r\n");
  17.             sb.Append("Content-Disposition: form-data; name=\"");
  18.             sb.Append("file"); // file form name
  19.             sb.Append("\"; filename=\"");
  20.             sb.Append(Path.GetFileName(files[i]));
  21.             sb.Append("\"");
  22.             sb.Append("\r\n");
  23.             sb.Append("Content-Type: ");
  24.             sb.Append("text/plain");
  25.             sb.Append("\r\n");
  26.             sb.Append("\r\n");
  27.  
  28.             string postHeader = sb.ToString();
  29.             byte[] postHeaderBytes = Encoding.UTF8.GetBytes(postHeader);
  30.  
  31.             // Build the trailing boundary string as a byte array  
  32.             // ensuring the boundary appears on a line by itself  
  33.             byte[] boundaryBytes = Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");
  34.  
  35.             FileStream fileStream = new FileStream(files[i], FileMode.Open, FileAccess.Read);
  36.             long length = postHeaderBytes.Length + fileStream.Length + boundaryBytes.Length;
  37.             webrequest.ContentLength = length;
  38.  
  39.             Stream requestStream = webrequest.GetRequestStream();
  40.  
  41.             // Write out our post header  
  42.             requestStream.Write(postHeaderBytes, 0, postHeaderBytes.Length);
  43.  
  44.             // Write out the file contents  
  45.             byte[] buffer = new Byte[checked((uint)Math.Min(4096, (int)fileStream.Length))];
  46.             int bytesRead = 0;
  47.             while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
  48.                 requestStream.Write(buffer, 0, bytesRead);
  49.  
  50.             // Write out the trailing boundary  
  51.             requestStream.Write(boundaryBytes, 0, boundaryBytes.Length);
  52.             try
  53.             {
  54.                 WebResponse response = webrequest.GetResponse();
  55.                 Stream s = response.GetResponseStream();
  56.                 StreamReader sr = new StreamReader(s);
  57.                 Debug.Log("sr.ReadToEnd() ====  " + sr.ReadToEnd());
  58.             }
  59.             catch (Exception e)
  60.             {
  61.  
  62.             }                                    
  63.         }        
  64.     }
  65.  
  66.     public bool MyRemoteCertificateValidationCallback(System.Object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
  67.     {
  68.         bool isOk = true;
  69.         // If there are errors in the certificate chain, look at each error to determine the cause.
  70.         if (sslPolicyErrors != SslPolicyErrors.None)
  71.         {
  72.             for (int i = 0; i < chain.ChainStatus.Length; i++)
  73.             {
  74.                 if (chain.ChainStatus[i].Status != X509ChainStatusFlags.RevocationStatusUnknown)
  75.                 {
  76.                     chain.ChainPolicy.RevocationFlag = X509RevocationFlag.EntireChain;
  77.                     chain.ChainPolicy.RevocationMode = X509RevocationMode.Online;
  78.                     chain.ChainPolicy.UrlRetrievalTimeout = new TimeSpan(0, 1, 0);
  79.                     chain.ChainPolicy.VerificationFlags = X509VerificationFlags.AllFlags;
  80.                     bool chainIsValid = chain.Build((X509Certificate2)certificate);
  81.                     if (!chainIsValid)
  82.                     {
  83.                         isOk = false;
  84.                     }
  85.                 }
  86.             }
  87.         }
  88.         return isOk;
  89.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement