1

有一个带有 .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

4

1 回答 1

2

(已更新)使用您的 URL 进行了测试,并且正在返回登录被拒绝(我没有的凭据:))所以它肯定是有效的:

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);
} 
于 2013-08-21T14:32:55.607 回答