使用C#以编程方式登录站点

时间:2014-01-16 21:42:10

标签: asp.net post webforms httpwebrequest webrequest

我想通过使用Windows桌面应用程序登录网站setting.fun-freak.com,仅用于学习目的。我使用了webrequest并尝试了

string formUrl = "http://setting.fun-freak.com/Account/Login.aspx"; 
string formParams = string.Format("ctl00$MainContent$txtUserName=loginId&ctl00$MainContent$txtPassword=password");
string cookieHeader;
WebRequest req = WebRequest.Create(formUrl);
req.ContentType = "application/x-www-form-urlencoded";
req.Method = "POST";
byte[] bytes = Encoding.ASCII.GetBytes(formParams);
req.ContentLength = bytes.Length;
using (Stream os = req.GetRequestStream())
{
     os.Write(bytes, 0, bytes.Length);
}

using (StreamReader sr = new StreamReader(req.GetResponse()))
{
   pageSource = sr.ReadToEnd();
}

我假设这个pageSource对象在登录页面后会有下一页的html(如果成功登录),但它包含相同的登录页面。

如何成功登录本网站并获取主页作为回应?

此外,这个(setting.fun-freak.com)我自己的网站,内置在asp.net webforms中。这是按钮点击事件代码(可能有助于挖掘问题)

UserContainer User = new UserProcessing().Authenticate(txtUserName.Text, txtPassword.Text);
        if (User != null)
        {
            Session["User"] = User;
            FormsAuthentication.RedirectFromLoginPage(txtUserName.Text, RememberMe.Checked);
        }
        else
        {
            lblMessage.Text = "Invalid credentials. Please try again";
        }

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

我按原样添加了这段代码,但它仍然无效。 它取自http://msdn.microsoft.com/en-us/library/debx8sh9.aspx

        // Create a request using a URL that can receive a post. 
        WebRequest request = WebRequest.Create("http://setting.fun-freak.com/Account/Login.aspx");
        // Set the Method property of the request to POST.
        request.Method = "POST";
        // Create POST data and convert it to a byte array.
        string postData = "ctl00$MainContent$txtUserName=UserName&ctl00$MainContent$txtPassword=Password";
        byte[] byteArray = Encoding.UTF8.GetBytes(postData);
        // Set the ContentType property of the WebRequest.
        request.ContentType = "application/x-www-form-urlencoded";
        // Set the ContentLength property of the WebRequest.
        request.ContentLength = byteArray.Length;
        // Get the request stream.
        Stream dataStream = request.GetRequestStream();
        // Write the data to the request stream.
        dataStream.Write(byteArray, 0, byteArray.Length);
        // Close the Stream object.
        dataStream.Close();
        // Get the response.
        WebResponse response = request.GetResponse();
        // Display the status.
        Console.WriteLine(((HttpWebResponse)response).StatusDescription);
        // Get the stream containing content returned by the server.
        dataStream = response.GetResponseStream();
        // Open the stream using a StreamReader for easy access.
        StreamReader reader = new StreamReader(dataStream);
        // Read the content.
        string responseFromServer = reader.ReadToEnd();
        // Display the content.
        Console.WriteLine(responseFromServer);
        // Clean up the streams.
        reader.Close();
        dataStream.Close();
        response.Close();

问题是对象responseFromServer没有显示登录页面旁边的内容。它仅显示登录页面内容。

相关问题