0

I want to log in to a website setting.fun-freak.com just for learning purpose by using a windows desktop application. I used webrequest and tried as

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();
}

I assume that this pageSource object will have html of next page after login page (if login succesfully), but it contains the same login page in return.

How can I successfully log in to this website and get home page in response?

Moreover, this (setting.fun-freak.com) my own site, built in asp.net webforms. Here is button click event code (May be it help to dig out the problem)

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

Any help will be appreciated.

Lali
  • 2,816
  • 4
  • 30
  • 47

1 Answers1

0

I added this piece of code as it is, but it still not working . it is taken from 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();

The problem is that the object responseFromServer do not show then content of next to the login page. Its shows only login page contents.

Lali
  • 2,816
  • 4
  • 30
  • 47