0

I am trying to login to a website "https://my.freecycle.org/" I have tried various methods including this stackoverflow Login to website, via C# but with no luck, it does not appear to be a standard form.

<form method="post" class="cols_2" action="https://my.freecycle.org/login" id="loginform" accept-charset="utf-8"><fieldset>
<input type="hidden" name="referer" value="">

<label for="username">Username (or email address):</label>
<input type="text" name="username" id="username"><br>

<label for="pass">Password:</label>
<input type="password" name="pass" id="pass"><br>

<input type="submit" value="Log in" class="button defaultButton vgap ui-button ui-widget ui-state-default ui-corner-all" role="button" aria-disabled="false">

I would really appreciate some help with this, below is the code I tried.

private void button1_Click(object sender, EventArgs e)
    {
        CookieAwareWebClient client = new CookieAwareWebClient();
        client.BaseAddress = @"https://my.freecycle.org/";
        NameValueCollection loginData = new NameValueCollection();
        loginData.Add("username", @"myemail@gmail.com");
        loginData.Add("pass", "mypassword");
        client.UploadValues("login", "POST", loginData);

        //Now you are logged in and can request pages    
        string htmlSource = client.DownloadString(client.BaseAddress);
        textBox1.Text = htmlSource;
    }

public class CookieAwareWebClient : WebClient
{
    private CookieContainer cookie = new CookieContainer();

    protected override WebRequest GetWebRequest(Uri address)
    {
        WebRequest request = base.GetWebRequest(address);
        if (request is HttpWebRequest)
        {
            (request as HttpWebRequest).CookieContainer = cookie;
        }
        return request;
    }
}
Community
  • 1
  • 1
TEDSON
  • 191
  • 2
  • 13

1 Answers1

0

I'm sorry if I wasted anyone's time looking into this. After wondering why there was no error, when thinking about the above comment question, I realized that this line.

string htmlSource = client.DownloadString(client.BaseAddress);

was getting the source from the initial login page, not the page I wanted source from after logging in.

When I enter the correct landing url I get the results I expected.

string htmlSource = client.DownloadString(@"https://my.freecycle.org/page/i/need");
TEDSON
  • 191
  • 2
  • 13