Advertisement
Guest User

Untitled

a guest
May 31st, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.41 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_Post;
  11. public Mail Mail_Send;
  12. public Post(ref Log Logs_error, ref Mail Mail_Sends)
  13. {
  14. Lg_Post = Logs_error;
  15. Mail_Send = Mail_Sends;
  16. }
  17.  
  18. public void POST(string fin, string ruta2)
  19. {
  20. try
  21. {
  22. Lg_Post.LogEx("Entra al Post");
  23. Lg_Post.LogEx(ruta2);
  24. string archivo = ruta2;
  25. //archivo = archivo.Replace("Base","Datos ");
  26. string url = "http://testing.electroquimica.cl/login";
  27. HttpWebRequest tokenRequest = (HttpWebRequest)WebRequest.Create(url);
  28. tokenRequest.CookieContainer = new CookieContainer();
  29. HttpWebResponse tokenResponse = (HttpWebResponse)tokenRequest.GetResponse();
  30. String token = tokenResponse.Cookies["csrftoken"].ToString().Split('=')[1];
  31.  
  32.  
  33. HttpWebRequest loginRequest = (HttpWebRequest)WebRequest.Create(url);
  34. loginRequest.Method = "post";
  35. loginRequest.CookieContainer = new CookieContainer();
  36. loginRequest.ContentType = "application/x-www-form-urlencoded";
  37.  
  38. string tempEmail = "Test_Post";
  39. string tempPass = "test_post";
  40.  
  41. loginRequest.CookieContainer.Add(new Uri("http://testing.electroquimica.cl"), tokenResponse.Cookies);
  42.  
  43. String postData = "csrfmiddlewaretoken=" + token;
  44. postData += "&username=" + tempEmail;
  45. postData += "&password=" + tempPass;
  46.  
  47. byte[] data = Encoding.ASCII.GetBytes(postData);
  48. loginRequest.ContentLength = data.Length; // +1;
  49. loginRequest.Timeout = 5000;
  50. Lg_Post.LogEx("envian credenciales");
  51.  
  52. //String encoded = System.Convert.ToBase64String(System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes(tempEmail + ":" + tempPass));
  53. loginRequest.Headers.Add("Origin", "http://testing.electroquimica.cl");
  54.  
  55. loginRequest.GetRequestStream().Write(data, 0, data.Length);
  56. loginRequest.PreAuthenticate = true;
  57.  
  58.  
  59.  
  60. HttpWebResponse response = (HttpWebResponse)loginRequest.GetResponse();
  61.  
  62. if (response.StatusCode == HttpStatusCode.OK)
  63. {
  64. //Lg_Post.LogEx("Conecta OK");
  65. string url1 = "http://testing.electroquimica.cl/mediciones/subir_archivo"; //mediciones/subir_archivo";
  66. HttpWebRequest tokenRequest1 = (HttpWebRequest)WebRequest.Create(url1);
  67. tokenRequest1.CookieContainer = loginRequest.CookieContainer;
  68. HttpWebResponse tokenResponse1 = (HttpWebResponse)tokenRequest1.GetResponse();
  69. String token1 = tokenResponse1.Cookies["csrftoken"].ToString().Split('=')[1];
  70. //Lg_Post.LogEx(token1);
  71. //Lg_Post.LogEx("saca token1");
  72.  
  73. HttpWebRequest subirrequest = (HttpWebRequest)WebRequest.Create(url1);
  74. subirrequest.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
  75. subirrequest.Method = WebRequestMethods.Http.Post;
  76. //Lg_Post.LogEx("aplica metodo post");
  77.  
  78. subirrequest.CookieContainer = loginRequest.CookieContainer;
  79. string boundaryString = "------WebKitFormBoundaryizR2U9c55ZBosjVC";
  80. subirrequest.ContentType = "multipart/form-data; boundary=" + boundaryString;
  81. subirrequest.Headers.Add("Origin", "http://testing.electroquimica.cl");
  82. subirrequest.KeepAlive = true; //false
  83. subirrequest.Credentials = System.Net.CredentialCache.DefaultCredentials;
  84.  
  85.  
  86. subirrequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
  87. subirrequest.UserAgent = "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.114 Safari/537.36";
  88. subirrequest.Headers.Add("Accept-Encoding: gzip,deflate,sdch");
  89. subirrequest.Headers.Add("Accept-Language: es-ES,es;q=0.8");
  90. subirrequest.Headers.Add("Upgrade-Insecure-Requests: 1");
  91. Lg_Post.LogEx("envia credenciales de headers");
  92.  
  93. subirrequest.PreAuthenticate = true;
  94.  
  95. string fileUrl = @archivo;
  96. Lg_Post.LogEx(fileUrl);
  97.  
  98. MemoryStream postDataStream = new MemoryStream();
  99. StreamWriter postDataWriter = new StreamWriter(postDataStream);
  100.  
  101.  
  102. // Include the file in the post data
  103. postDataWriter.Write("--" + boundaryString + "");
  104. postDataWriter.Write("\r\nContent-Disposition: form-data;"
  105. + " name=\"{0}\"\r\n\r\n{1}",
  106. "csrfmiddlewaretoken",
  107. token1);
  108. postDataWriter.Write("\r\n--" + boundaryString + "");
  109. postDataWriter.Write("\r\nContent-Disposition: form-data;"
  110. + " name=\"{0}\";"
  111. + " filename=\"{1}\""
  112. + "\r\nContent-Type: {2}\r\n\r\n",
  113. "file",
  114. Path.GetFileName(fileUrl),
  115. Path.GetExtension(fileUrl));
  116. postDataWriter.Flush();
  117.  
  118. Lg_Post.LogEx("envia los datos del post");
  119.  
  120. // postDataStream.WriteTo(postDataStream);
  121.  
  122. FileStream fileStream = new FileStream(fileUrl, FileMode.Open, FileAccess.Read);
  123. byte[] buffer = new byte[1024];
  124. int bytesRead = 0;
  125. while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
  126. {
  127. postDataStream.Write(buffer, 0, bytesRead);
  128. Lg_Post.LogEx("sube el archivo");
  129. }
  130. fileStream.Close();
  131.  
  132. // string boundaryString = "----SomeRandomText";
  133. postDataWriter.Write("\r\n--" + boundaryString + "--\r\n");
  134. postDataWriter.Flush();
  135.  
  136.  
  137. // Set the http request body content length
  138. subirrequest.ContentLength = postDataStream.Length;
  139. //subirrequest.ContentLength = postDataStream.Length;
  140.  
  141. // Dump the post data from the memory stream to the request stream
  142. using (Stream s = subirrequest.GetRequestStream())
  143. {
  144. postDataStream.WriteTo(s);
  145. }
  146. string result = null;
  147. using (HttpWebResponse resp = subirrequest.GetResponse() as HttpWebResponse)
  148. {
  149. StreamReader reader = new StreamReader(resp.GetResponseStream());
  150. result = reader.ReadToEnd(); //si es true graba si es falso no grabo
  151. bool existe = result.Contains("alert alert-danger");
  152. if (existe)
  153. {
  154. Lg_Post.LogEx("Error al subir archivo: Indica alert alert-danger");
  155. Mail_Send.Error("Error al subir archivo: Indica alert alert-danger");
  156. }
  157.  
  158. }
  159. postDataStream.Close();
  160. }
  161. else
  162. {
  163. string hh = "no conectar";
  164. Mail_Send.Error("No conecta ");
  165.  
  166. // serverMessenger.SendErrorMessage(0);
  167. //Debug.LogError("Cannot Find User. TryToLogin finished");
  168. }
  169. }
  170. catch (WebException e)
  171. {
  172.  
  173. using (WebResponse response = e.Response)
  174. {
  175. HttpWebResponse httpResponse = (HttpWebResponse)response;
  176. if (response == null)
  177. {
  178. // Lg_Post.LogEx(e);
  179. }
  180. else
  181. {
  182. string error_Code = "Error code: " + httpResponse.StatusCode;
  183. string error_Descripcion = "Error Descripcion: " + httpResponse.StatusDescription;
  184. Lg_Post.LogEx(error_Code + " - " + error_Descripcion);
  185. Mail_Send.Error(error_Code + " - " + error_Descripcion);
  186. }
  187. }
  188. }
  189.  
  190. }
  191.  
  192. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement