登录.aspx web表单并查看http响应
本文关键字:http 响应 aspx web 表单 登录 | 更新日期: 2023-09-27 18:28:32
有一个网站的.aspx页面包含一个登录表单.
我想用我的凭据登录这个网站,检索http响应并在控制台中打印出来。
我对包含POST的标准html表单也做了同样的操作,但在这个页面源中没有POST。我不确定该怎么办。
以下是登录表单来源:
<td width="370" class="loginScreenBox">
<table width="100%" border="0" cellspacing="3" cellpadding="3">
<tr>
<td class="heading" colspan="2" style="text-align: center;">
Existing Partner
</td>
</tr>
<tr>
<td style="text-align: right;">
Enter group reference number:
</td>
<td>
<input name="_ctl1:userName" type="text" id="_ctl1_userName" />
</td>
</tr>
<tr>
<td style="text-align: right;">
Password:
</td>
<td>
<input name="_ctl1:passWord" type="password" id="_ctl1_passWord" />
</td>
</tr>
<tr>
<td colspan="2" style="text-align: center; padding-bottom: 15px;">
<input type="submit" name="_ctl1:btnLogin" value="Login" id="_ctl1_btnLogin" class="btn" />
</td>
</tr>
</table>
</td>
该网站http://hoteladmin.laterooms.com/en/SignIn.aspx?&ReturnUrl=%2fmain.aspx
var request = (HttpWebRequest)HttpWebRequest.Create("http://hoteladmin.laterooms.com/en/SignIn.aspx?ReturnUrl=%2fmain.aspx");
var container = new CookieContainer();
//
request.CookieContainer = container;
//
var response = request.GetResponse();
var bufferIntitial = new byte[512];
string responseTextInitial = "";
using (var responseStream = response.GetResponseStream())
{
while (responseStream.Read(bufferIntitial, 0, 512) > 0)
{
responseTextInitial += Encoding.UTF8.GetString(bufferIntitial);
}
}
/// NEW REQUEST
var loginRequest = (HttpWebRequest)HttpWebRequest.Create("http://hoteladmin.laterooms.com/en/SignIn.aspx?ReturnUrl=%2fmain.aspx");
// Resuse the cookie container containing the cookies received from our initial request.
loginRequest.CookieContainer = container;
var requestFormData = new StringBuilder();
requestFormData.Append("__VIEWSTATE=" + HttpUtility.HtmlEncode(ExtractViewState(responseTextInitial)) + "&");
requestFormData.Append("_ctl1:userName=" + HttpUtility.HtmlEncode("user123") + "&");
requestFormData.Append("_ctl1:userPass=" + HttpUtility.HtmlEncode("pass123") + "&");
requestFormData.Append("_ctl1:btnLogin=" + HttpUtility.HtmlEncode("Login"));
var requestFormDataByte = Encoding.ASCII.GetBytes(requestFormData.ToString());
loginRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.57 Safari/537.36";
loginRequest.Method = "POST";
loginRequest.ContentLength = requestFormDataByte.Length;
loginRequest.ContentType = "application/x-www-form-urlencoded";
using (var requestStream = loginRequest.GetRequestStream())
{
requestStream.Write(requestFormDataByte, 0, requestFormDataByte.Length);
}
var loginResponse = loginRequest.GetResponse();
var buffer = new byte[512];
string responseText = "";
using (var responseStream = loginResponse.GetResponseStream())
{
while (responseStream.Read(buffer, 0, 512) > 0)
{
responseText += Encoding.UTF8.GetString(buffer);
}
}
助手功能:
private static string ExtractViewState(string s)
{
string viewStateNameDelimiter = "__VIEWSTATE";
string valueDelimiter = "value='"";
int viewStateNamePosition = s.IndexOf(viewStateNameDelimiter);
int viewStateValuePosition = s.IndexOf(valueDelimiter, viewStateNamePosition);
int viewStateStartPosition = viewStateValuePosition + valueDelimiter.Length;
int viewStateEndPosition = s.IndexOf("'"", viewStateStartPosition);
return s.Substring(viewStateStartPosition, viewStateEndPosition - viewStateStartPosition);
}