Webrequest via post method return 404 Not Found

Webrequest via post method return 404 Not Found

如标题所说,我试图通过 post 向某个页面发出请求。首先使用 google 开发人员工具检查请求参数,我可以弄清楚这一点(我不完全确定我是否将所有正确的参数放入 post 字符串中 - 查看图像以获取更多信息)。我在 www 周围找到了很多例子,但没有人使用这个,我不知道哪里出了问题,我会像往常一样提供一点帮助:)。

请求Headers

表单数据

这是我的代码:

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);

这是因为缺少 __ViewState 参数。这个参数是隐藏表单字段,每个请求都需要附加。

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);