打开登录页面,插入登录信息,然后单击“登录”按钮

时间:2011-05-20 09:35:34

标签: c# asp.net

我想打开像www.site.com/login.php这样的登录页面

插入用户名和密码

并调用登录按钮。

所有这一切都应该在服务器(而不是在浏览器中)中进行,

有可能做到吗?

4 个答案:

答案 0 :(得分:0)

您可以在登录按钮提交的任何位置发出POST请求。 只需在请求中传递用户名和密码即可。

答案 1 :(得分:0)

每个登录页面通常采用以下格式:

<form action="login.extention" method="post or get">
<input name="username" type="text" />
<input name="password" type="password" />
</form>

您必须通过获取或发布信息将您的信息发送到操作页面。(通常是发布)

答案 2 :(得分:0)

如果要以登录用户的身份触发请求,则必须在登录POST请求(可能设置登录cookie)和进一步请求之间保留CookieContainer实例。

        var cookieJar = new CookieContainer();            
        var loginData = new Dictionary<string,string>() {
            // be carefull to use the same keys/names as the login form
            { "username", "LightWing" },
            { "password", "l33tPwD" } 
        };
        var request = (HttpWebRequest)WebRequest.Create("http://.../login.php");
        request.CookieContainer = cookieJar;
        request.AllowAutoRedirect = true;
        request.Method = "POST";

        // build the POST data
        byte[] dataBuffer = Encoding.ASCII.GetBytes(string.Join("&", loginData.AllKeys.Select(x => string.Concat(HttpUtility.UrlEncode(x), "=", HttpUtility.UrlEncode(loginData[x])))));
        request.ContentLength = dataBuffer.LongLength;
        request.ContentType = "application/x-www-form-urlencoded";
        using (var requestStream = request.GetRequestStream())
        {
            requestStream.Write(dataBuffer, 0, dataBuffer.Length);
            requestStream.Flush();
        }
        var response = request.GetResponse();
        using (var responseStream = response.GetResponseStream())
        {
            // do some reading if you want..
        }

        // now _cookieJar should contain the login cookie...
        // you can trigger further requests as a logged in user:
        request = (HttpWebRequest)WebRequest.Create("http://.../restrictedPage.php");
        request.CookieContainer = cookieJar; // reuse them cookies!
        request.AllowAutoRedirect = true;
        request.Method = "GET";
        var response = request.GetResponse();
        using (var responseStream = response.GetResponseStream())
        {
            // read the restricted page response
        }

我还编写了一个简单的实用程序,可以帮助您抓取使用回发的Asp.net WebForms页面。你可以找到它here

答案 3 :(得分:0)

我为其中一个小项目做了同样的事情

我在.aspx页面中添加了超链接,点击后自动登录我到外部网站

page_load上的

调用以下代码

HyperLink1.NavigateUrl = "javascript:__doExternalPost(\"http://www.xyz.com/login.jsp\");";

if (Page.IsPostBack == false)
{
       this.Page.ClientScript.RegisterHiddenField("username", USERNAME);                                                        
       this.Page.ClientScript.RegisterHiddenField("password", PASSWORD);
}
<。>在.aspx页面添加以下javascript

function __doExternalPost(targetUrl) 
{
    theForm.__VIEWSTATE.value = "";
    theForm.encoding = "application/x-www-form-urlencoded";
    theForm.action = targetUrl;
    theForm.target = "_blank";
    theForm.submit();
}

希望它能提供一些帮助