Advertisement
Guest User

post3

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