Advertisement
Guest User

Untitled

a guest
Apr 12th, 2016
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.13 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 = loginRequest.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.  
  66. subirrequest.Method = WebRequestMethods.Http.Post;
  67.  
  68. subirrequest.CookieContainer = loginRequest.CookieContainer;
  69. string boundaryString = "------WebKitFormBoundaryizR2U9c55ZBosjVC";
  70. subirrequest.ContentType = "multipart/form-data; boundary=" + boundaryString;
  71. subirrequest.Headers.Add("Origin", "http://testing.electroquimica.cl");
  72. subirrequest.KeepAlive = true; //false
  73. subirrequest.Credentials = System.Net.CredentialCache.DefaultCredentials;
  74.  
  75. subirrequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
  76. subirrequest.UserAgent = "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.114 Safari/537.36";
  77. subirrequest.Headers.Add("Accept-Encoding: gzip,deflate,sdch");
  78. subirrequest.Headers.Add("Accept-Language: es-ES,es;q=0.8");
  79. subirrequest.Headers.Add("Upgrade-Insecure-Requests: 1");
  80.  
  81. subirrequest.PreAuthenticate = true;
  82.  
  83. string fileUrl = @archivo;
  84.  
  85. MemoryStream postDataStream = new MemoryStream();
  86. StreamWriter postDataWriter = new StreamWriter(postDataStream);
  87.  
  88.  
  89. // Include the file in the post data
  90. postDataWriter.Write("--" + boundaryString + "");
  91. postDataWriter.Write("\r\nContent-Disposition: form-data;"
  92. + " name=\"{0}\"\r\n\r\n{1}",
  93. "csrfmiddlewaretoken",
  94. token1);
  95.  
  96. postDataWriter.Write("\r\n--" + boundaryString + "");
  97. postDataWriter.Write("\r\nContent-Disposition: form-data;"
  98. + " name=\"{0}\";"
  99. + " filename=\"{1}\""
  100. + "\r\nContent-Type: {2}\r\n\r\n",
  101. "file",
  102. Path.GetFileName(fileUrl),
  103. Path.GetExtension(fileUrl));
  104. postDataWriter.Flush();
  105.  
  106.  
  107. // postDataStream.WriteTo(postDataStream);
  108.  
  109. FileStream fileStream = new FileStream(fileUrl, FileMode.Open, FileAccess.Read);
  110. byte[] buffer = new byte[1024];
  111. int bytesRead = 0;
  112. while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
  113. {
  114. postDataStream.Write(buffer, 0, bytesRead);
  115. }
  116. fileStream.Close();
  117.  
  118. // string boundaryString = "----SomeRandomText";
  119. postDataWriter.Write("\r\n--" + boundaryString + "--\r\n");
  120. postDataWriter.Flush();
  121.  
  122.  
  123. // Set the http request body content length
  124. subirrequest.ContentLength = postDataStream.Length;
  125. //subirrequest.ContentLength = postDataStream.Length;
  126.  
  127. // Dump the post data from the memory stream to the request stream
  128. using (Stream s = subirrequest.GetRequestStream())
  129. {
  130. postDataStream.WriteTo(s);
  131. }
  132. string result = null;
  133. using (HttpWebResponse resp = subirrequest.GetResponse() as HttpWebResponse)
  134. {
  135. StreamReader reader = new StreamReader(resp.GetResponseStream());
  136. result = reader.EndOfStream.ToString(); //si es true graba si es falso no grabo
  137.  
  138. }
  139.  
  140. postDataStream.Close();
  141.  
  142.  
  143. }
  144. else
  145. {
  146. string hh = "no conectar";
  147. // serverMessenger.SendErrorMessage(0);
  148. //Debug.LogError("Cannot Find User. TryToLogin finished");
  149. }
  150. }
  151. catch (WebException e)
  152. {
  153.  
  154. using (WebResponse response = e.Response)
  155. {
  156. HttpWebResponse httpResponse = (HttpWebResponse)response;
  157. string jaja = "Error code: " + httpResponse.StatusCode;
  158. //using (Stream data = response.GetResponseStream())
  159. //{
  160. // string text = new StreamReader(data).ReadToEnd();
  161. // Console.WriteLine(text);
  162. //}
  163. }
  164. }
  165.  
  166. }
  167.  
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement