2014-01-16 72 views
0

我想通过使用Windows桌面应用程序登录到网站setting.fun-freak.com,仅用于学习目的。我用的WebRequest,并试图为使用C#以编程方式登录网站

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"; 
     } 

任何帮助将不胜感激。

回答

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不显示,然后旁边的登录页面的内容。它只显示登录页面的内容。