简体   繁体   English

使用C#桌面应用程序登录网站

[英]Log in to website with c# desktop app

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. 我正在尝试登录到网站“ https://my.freecycle.org/ ”。我尝试了多种方法,包括此stackoverflow 通过C#登录到网站,但没有运气,它似乎不是标准形式。

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM