Advertisement
Guest User

post

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