0

like the tittle says, I was trying to make a request via post to some page. First using google developer tool, to check parameters for request, and I can figure out this (I am not completly sure if I put all correct parameters in my post string - check images for more information). I found many examples around www but no one works with this and I don't know what is wrong, I will aprecciate like always a bit help :).

Request Headers

header_post

Form Data

form_data

This is my code:

string email = "xxxxx";
string password = "xxxxx";

string LOGIN_URL = "https://intranet.cibertec.edu.pe/LoginBolsa/LoginBolsaCIB.aspx";

CookieContainer cookies = new CookieContainer();

HttpWebRequest webRequest = WebRequest.Create(LOGIN_URL) as HttpWebRequest;
webRequest.CookieContainer = cookies;
StreamReader responseReader = new StreamReader(
      webRequest.GetResponse().GetResponseStream()
   );
string responseData = responseReader.ReadToEnd();
responseReader.Close();

string postString = string.Format("ctl00$ContentPlaceHolder1$Login1$UserName={0}&ctl00$ContentPlaceHolder1$Login1$Password={1}&ctl00$ContentPlaceHolder1$Login1$LoginButton={2}&hdnOrigen={3}&hdnLinea={4}&Pagina_Principal={5}", email, password, "Ingresar", "bolsa", "I", "LoginBolsaCIB.aspx");


webRequest = WebRequest.Create(LOGIN_URL) as HttpWebRequest;
webRequest.Method = "POST";
webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.CookieContainer = cookies;


StreamWriter requestWriter = new StreamWriter(webRequest.GetRequestStream());
requestWriter.Write(postString);
requestWriter.Close();

responseReader = new StreamReader(webRequest.GetResponse().GetResponseStream());
responseData = responseReader.ReadToEnd();
responseReader.Close();

Response.Write(responseData);
  • Possibly your request from Google Developer Tools gets called with SAME-ORIGIN but when you execute your code a different origin is present causing a problem with SAME-ORIGIN policy? – David Tansey Oct 04 '15 at 22:32
  • There is a way that I can set origin policy or some workaround? – Juan Ruiz de Castilla Oct 04 '15 at 22:35
  • You can only set the origin policy if you have control over the server-side. Can you clarify whether or not you were able to sucessfully issue a POST using Google Developer Tools? – David Tansey Oct 04 '15 at 22:39
  • you mean that if same-origin policy is true or something like this, there is no way that i can make a request via post since other webpage?. – Juan Ruiz de Castilla Oct 04 '15 at 22:42
  • @DavidTansey , I don't sure how make a request with developer tools, so i follow this http://stackoverflow.com/questions/14248296/making-http-requests-using-chrome-developer-tools, but a error happens "Uncaught SyntaxError: Unexpected string(…)" – Juan Ruiz de Castilla Oct 04 '15 at 22:48
  • Look at the following for some guidance on issuing the POST request manually as a step towards being able to test your code confidently (without bad assumptions): http://stackoverflow.com/questions/14248296/making-http-requests-using-chrome-developer-tools . I will also mention that I do not believe it is possible to issue a POST request manually using the Google Developer Tools in Chrome without the help of an add-in. – David Tansey Oct 04 '15 at 22:59
  • I'm using postman, and now I'm testing my request and same issue happens 404, not found :/. – Juan Ruiz de Castilla Oct 04 '15 at 23:11

1 Answers1

0

It's because a missing __ViewState param. This params which is hidden form field is needed to attach for every Request.

int start = responseData.IndexOf("value=\"", responseData.IndexOf("name=\"__VIEWSTATE\""));
int end = responseData.IndexOf("/>", start);
string viewState = responseData.Substring(start + 7, end - start - 9);
string postString = string.Format("ctl00$ContentPlaceHolder1$Login1$UserName={0}&ctl00$ContentPlaceHolder1$Login1$Password={1}&ctl00$ContentPlaceHolder1$Login1$LoginButton={2}&hdnOrigen={3}&hdnLinea={4}&Pagina_Principal={5}", email, password, "Ingresar", "bolsa", "I", "LoginBolsaCIB.aspx");
postString += String.Concat("&__VIEWSTATE=", viewState);
Orel Eraki
  • 11,940
  • 3
  • 28
  • 36
  • thank you bro, like you present it works, curiously i thought that when put cookies container was sending viewstate too, why this doesn't happen?. – Juan Ruiz de Castilla Oct 05 '15 at 02:50
  • Theres no way to get viewstate in a more "elegant" form?. – Juan Ruiz de Castilla Oct 05 '15 at 02:51
  • @JuanRuizdeCastilla, There is, by using 3rd Party like `HtmlAgilityPack`, but i wanted to keep it simple without complicating your code. Additionally, we can make the current extraction with a `Generic Tag Extractor` like function. `__ViewState` is a input type and belong to the data that should be send as part of the `Http Content` while `Cookies` are part of the `Http Headers` – Orel Eraki Oct 05 '15 at 13:10