Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public void uploadToDrive()
- {
- string[] files = Directory.GetFiles(Directory.GetCurrentDirectory(), "*.txt");
- for (int i = 0; i < files.Length; i++)
- {
- string boundary = "----------" + DateTime.Now.Ticks.ToString("x");
- ServicePointManager.ServerCertificateValidationCallback = MyRemoteCertificateValidationCallback;
- HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(FILEUPLOAD_BASE_URL);
- webrequest.ContentType = "multipart/form-data; boundary=" + boundary;
- webrequest.Method = "POST";
- // Build up the post message header
- StringBuilder sb = new StringBuilder();
- sb.Append("--");
- sb.Append(boundary);
- sb.Append("\r\n");
- sb.Append("Content-Disposition: form-data; name=\"");
- sb.Append("file"); // file form name
- sb.Append("\"; filename=\"");
- sb.Append(Path.GetFileName(files[i]));
- sb.Append("\"");
- sb.Append("\r\n");
- sb.Append("Content-Type: ");
- sb.Append("text/plain");
- sb.Append("\r\n");
- sb.Append("\r\n");
- string postHeader = sb.ToString();
- byte[] postHeaderBytes = Encoding.UTF8.GetBytes(postHeader);
- // Build the trailing boundary string as a byte array
- // ensuring the boundary appears on a line by itself
- byte[] boundaryBytes = Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");
- FileStream fileStream = new FileStream(files[i], FileMode.Open, FileAccess.Read);
- long length = postHeaderBytes.Length + fileStream.Length + boundaryBytes.Length;
- webrequest.ContentLength = length;
- Stream requestStream = webrequest.GetRequestStream();
- // Write out our post header
- requestStream.Write(postHeaderBytes, 0, postHeaderBytes.Length);
- // Write out the file contents
- byte[] buffer = new Byte[checked((uint)Math.Min(4096, (int)fileStream.Length))];
- int bytesRead = 0;
- while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
- requestStream.Write(buffer, 0, bytesRead);
- // Write out the trailing boundary
- requestStream.Write(boundaryBytes, 0, boundaryBytes.Length);
- try
- {
- WebResponse response = webrequest.GetResponse();
- Stream s = response.GetResponseStream();
- StreamReader sr = new StreamReader(s);
- Debug.Log("sr.ReadToEnd() ==== " + sr.ReadToEnd());
- }
- catch (Exception e)
- {
- }
- }
- }
- public bool MyRemoteCertificateValidationCallback(System.Object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
- {
- bool isOk = true;
- // If there are errors in the certificate chain, look at each error to determine the cause.
- if (sslPolicyErrors != SslPolicyErrors.None)
- {
- for (int i = 0; i < chain.ChainStatus.Length; i++)
- {
- if (chain.ChainStatus[i].Status != X509ChainStatusFlags.RevocationStatusUnknown)
- {
- chain.ChainPolicy.RevocationFlag = X509RevocationFlag.EntireChain;
- chain.ChainPolicy.RevocationMode = X509RevocationMode.Online;
- chain.ChainPolicy.UrlRetrievalTimeout = new TimeSpan(0, 1, 0);
- chain.ChainPolicy.VerificationFlags = X509VerificationFlags.AllFlags;
- bool chainIsValid = chain.Build((X509Certificate2)certificate);
- if (!chainIsValid)
- {
- isOk = false;
- }
- }
- }
- }
- return isOk;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement