请求标题
表单数据
这是我的代码:
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);
答案 0 :(得分:0)
这是因为缺少__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);