我正在尝试登录网站" https://my.freecycle.org/" 我尝试过各种方法,包括这个stackoverflow Login to website, via 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">
我真的很感激一些帮助,下面是我试过的代码。
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;
}
}
答案 0 :(得分:0)
对不起,如果我浪费了所有人的时间来研究这个问题。 在想到为什么没有错误之后,在考虑上述评论问题时,我意识到这一行。
string htmlSource = client.DownloadString(client.BaseAddress);
从初始登录页面获取源,而不是登录后我想要的源页面。
当我输入正确的登陆网址时,我得到了我期望的结果。
string htmlSource = client.DownloadString(@"https://my.freecycle.org/page/i/need");