Advertisement
Guest User

Untitled

a guest
Apr 8th, 2016
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.78 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Net;
  5. using System.IO;
  6. using System.Collections.Specialized;
  7.  
  8. class Post
  9. {
  10. //public Log Lg_Mail;
  11. //public Post(ref Log Logs_error)
  12. //{
  13. // Lg_Mail = Logs_error;
  14. //}
  15.  
  16. public void POST(string fin, string ruta2)
  17. {
  18. string archivo = ruta2;
  19. archivo = archivo.Replace("Base","Datos ");
  20. string url = "http://testing.electroquimica.cl/login";
  21. HttpWebRequest tokenRequest = (HttpWebRequest)WebRequest.Create(url);
  22. tokenRequest.CookieContainer = new CookieContainer();
  23. HttpWebResponse tokenResponse = (HttpWebResponse)tokenRequest.GetResponse();
  24. String token = tokenResponse.Cookies["csrftoken"].ToString().Split('=')[1];
  25.  
  26.  
  27. HttpWebRequest loginRequest = (HttpWebRequest)WebRequest.Create(url);
  28. loginRequest.Method = "post";
  29. loginRequest.CookieContainer = new CookieContainer();
  30. loginRequest.ContentType = "application/x-www-form-urlencoded";
  31.  
  32. string tempEmail = "Test_Post";
  33. string tempPass = "test_post";
  34.  
  35. loginRequest.CookieContainer.Add(new Uri("http://testing.electroquimica.cl"), tokenResponse.Cookies);
  36.  
  37. String postData = "csrfmiddlewaretoken=" + token;
  38. postData += "&username=" + tempEmail;
  39. postData += "&password=" + tempPass;
  40.  
  41. byte[] data = Encoding.ASCII.GetBytes(postData);
  42. loginRequest.ContentLength = data.Length; // +1;
  43. loginRequest.Timeout = 3000;
  44.  
  45. //String encoded = System.Convert.ToBase64String(System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes(tempEmail + ":" + tempPass));
  46. loginRequest.Headers.Add("Origin", "http://testing.electroquimica.cl");
  47.  
  48. loginRequest.GetRequestStream().Write(data, 0, data.Length);
  49. loginRequest.PreAuthenticate = true;
  50.  
  51.  
  52. try
  53. {
  54. HttpWebResponse response = (HttpWebResponse)loginRequest.GetResponse();
  55.  
  56. if (response.StatusCode == HttpStatusCode.OK)
  57. {
  58. string url1 = "http://testing.electroquimica.cl/mediciones/subir_archivo"; //mediciones/subir_archivo";
  59. HttpWebRequest tokenRequest1 = (HttpWebRequest)WebRequest.Create(url1);
  60. tokenRequest1.CookieContainer = new CookieContainer();
  61. HttpWebResponse tokenResponse1 = (HttpWebResponse)tokenRequest1.GetResponse();
  62. String token1 = tokenResponse1.Cookies["csrftoken"].ToString().Split('=')[1];
  63.  
  64. HttpWebRequest subirrequest = (HttpWebRequest)WebRequest.Create(url1);
  65. subirrequest.Method = WebRequestMethods.Http.Post;
  66. subirrequest.CookieContainer = loginRequest.CookieContainer;
  67. string boundaryString = "------WebKitFormBoundaryizR2U9c55ZBosjVC";
  68. subirrequest.ContentType = "multipart/form-data; boundary=" + boundaryString;
  69. subirrequest.Headers.Add("Origin", "http://testing.electroquimica.cl");
  70. subirrequest.KeepAlive = true; //false
  71. subirrequest.Credentials = System.Net.CredentialCache.DefaultCredentials;
  72.  
  73. subirrequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
  74. subirrequest.UserAgent = "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.114 Safari/537.36";
  75. subirrequest.Headers.Add("Accept-Encoding: gzip,deflate,sdch");
  76. subirrequest.Headers.Add("Accept-Language: es-ES,es;q=0.8");
  77. subirrequest.Headers.Add("Upgrade-Insecure-Requests: 1");
  78.  
  79. subirrequest.PreAuthenticate = true;
  80.  
  81. string fileUrl = @archivo;
  82.  
  83. MemoryStream postDataStream = new MemoryStream();
  84. StreamWriter postDataWriter = new StreamWriter(postDataStream);
  85.  
  86.  
  87. // Include the file in the post data
  88. postDataWriter.Write("--" + boundaryString + "");
  89. postDataWriter.Write("\r\nContent-Disposition: form-data;"
  90. + " name=\"{0}\"\r\n\r\n{1}",
  91. "csrfmiddlewaretoken",
  92. token1);
  93.  
  94. postDataWriter.Write("\r\n--" + boundaryString + "");
  95. postDataWriter.Write("\r\nContent-Disposition: form-data;"
  96. + " name=\"{0}\";"
  97. + " filename=\"{1}\""
  98. + "\r\nContent-Type: {2}\r\n\r\n",
  99. "file",
  100. Path.GetFileName(fileUrl),
  101. Path.GetExtension(fileUrl));
  102. postDataWriter.Flush();
  103.  
  104.  
  105. // postDataStream.WriteTo(postDataStream);
  106.  
  107. FileStream fileStream = new FileStream(fileUrl, FileMode.Open, FileAccess.Read);
  108. byte[] buffer = new byte[1024];
  109. int bytesRead = 0;
  110. while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
  111. {
  112. postDataStream.Write(buffer, 0, bytesRead);
  113. }
  114. fileStream.Close();
  115.  
  116. // string boundaryString = "----SomeRandomText";
  117. postDataWriter.Write("\r\n--" + boundaryString + "--\r\n");
  118. postDataWriter.Flush();
  119.  
  120. // Set the http request body content length
  121. subirrequest.ContentLength = postDataStream.Length;
  122. //subirrequest.ContentLength = postDataStream.Length;
  123.  
  124. // Dump the post data from the memory stream to the request stream
  125. using (Stream s = subirrequest.GetRequestStream())
  126. {
  127. postDataStream.WriteTo(s);
  128. }
  129.  
  130. // string vv = subirrequest.Headers[""]
  131. postDataStream.Close();
  132.  
  133.  
  134. }
  135. else
  136. {
  137. string hh = "no conectar";
  138. // serverMessenger.SendErrorMessage(0);
  139. //Debug.LogError("Cannot Find User. TryToLogin finished");
  140. }
  141. }
  142. catch (WebException e)
  143. {
  144.  
  145. using (WebResponse response = e.Response)
  146. {
  147. HttpWebResponse httpResponse = (HttpWebResponse)response;
  148. string jaja = "Error code: " + httpResponse.StatusCode;
  149. //using (Stream data = response.GetResponseStream())
  150. //{
  151. // string text = new StreamReader(data).ReadToEnd();
  152. // Console.WriteLine(text);
  153. //}
  154. }
  155. }
  156.  
  157. }
  158.  
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement