我正在开展一个学校项目,我想通过带有通用应用程序的Windows8.1设备查看我们的时间表。因此,每次我想检查它时,我都不必登录。我需要一种方法将我登录到我们学校的网站,让我看到源代码,以便我可以看到课程。
网站我需要使用C#代码登录here。
源代码似乎是:
<form method="post" action="/adfs/ls/?SAMLRequest=nZJPb9swDMW/iqF7bMmt11SIU2QZihZosSBxd9hloG1m1WZJriglwT79lD9uM2DrYUeBj3yPP2py%0As9NdskFHypqSiZSzBE1jW2W%2Bl%2Bypuh2N2c10QqC7vJez4J/NEl8Ckk9ioyF5rJQsOCMtkCJpQCNJ%0A38jV7PFB5imXvbPeNrZjyYwInY9Wc2soaHQrdBvV4NPyoWTP3vcks0yrH4Z8aBWmv6D9qQympsug%0A77Mt1kSWJbfWNXgIU7I1dIQsuf9Usm8C4epacH6Zg/iQQ12PEQpx2dQXdcFbhCijBRCpDb41EgW8%0Aj4ZgfMlyLoqR4CN%2BUYmxFFzmeVoU119Zsjgt8VGZI5z3Nq6PIpJ3VbUYLT6vKpZ8GSBHATshlQd3%0Ad87y/cEwAGTTAdfACNvQBPoT1SQ7t3m94%2BmE2B4Yxlt43PlkbnUPTtE%2Bo4ad0kG/5jwXzruYYonr%0A/0q9l62xRQf7t4Q4F41XzfG5jdzslobYf3Odnor/2OKtfP5Zp78B&RelayState=Zadkine" id="MainForm">
<input name="ctl00$ContentPlaceHolder1$UsernameTextBox" type="text" id="ContentPlaceHolder1_UsernameTextBox" />
<input name="ctl00$ContentPlaceHolder1$PasswordTextBox" type="password" id="ContentPlaceHolder1_PasswordTextBox" />
<input type="submit" name="ctl00$ContentPlaceHolder1$SubmitButton" value="Aanmelden" id="ContentPlaceHolder1_SubmitButton" class="Resizable" />
</form>
基本上就是这样。 有一些__VIEWSTATE ,但我不知道它们是否重要。
我找到了两种解决方案。
Stack Overflow上的WebRequest示例无效:/
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://sts.zadkine.nl/adfs/ls/?SAMLRequest=nZJPb9swDMW/iqF7bMmt11SIU2QZihZosSBxd9hloG1m1WZJriglwT79lD9uM2DrYUeBj3yPP2py%0As9NdskFHypqSiZSzBE1jW2W%2Bl%2Bypuh2N2c10QqC7vJez4J/NEl8Ckk9ioyF5rJQsOCMtkCJpQCNJ%0A38jV7PFB5imXvbPeNrZjyYwInY9Wc2soaHQrdBvV4NPyoWTP3vcks0yrH4Z8aBWmv6D9qQympsug%0A77Mt1kSWJbfWNXgIU7I1dIQsuf9Usm8C4epacH6Zg/iQQ12PEQpx2dQXdcFbhCijBRCpDb41EgW8%0Aj4ZgfMlyLoqR4CN%2BUYmxFFzmeVoU119Zsjgt8VGZI5z3Nq6PIpJ3VbUYLT6vKpZ8GSBHATshlQd3%0Ad87y/cEwAGTTAdfACNvQBPoT1SQ7t3m94%2BmE2B4Yxlt43PlkbnUPTtE%2Bo4ad0kG/5jwXzruYYonr%0A/0q9l62xRQf7t4Q4F41XzfG5jdzslobYf3Odnor/2OKtfP5Zp78B&RelayState=Zadkine");
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
using (var requestStream = request.GetRequestStream())
using (var writer = new StreamWriter(requestStream))
{
writer.Write("ctl00$ContentPlaceHolder1$UsernameTextBox=" + yourusername + "&ctl00$ContentPlaceHolder1$PasswordTextBox=" + yourpassword);
}
using (var responseStream = request.GetResponse().GetResponseStream())
using (var reader = new StreamReader(responseStream))
{
var result = reader.ReadToEnd();
richTextBox1.Text = result;
}
在其他网站中,如果我尝试使用此代码,则会出现错误,例如“您必须允许Cookie登录”。但在我学校的网站上我不得到任何,甚至不是“错误的密码”。 (如果我在浏览器上输入了错误的密码,则会收到错误的密码错误。)
所以,问题是,如何使用C#登录website?
答案 0 :(得分:1)
WebBrowser文档中的示例代码已完成事件:
HtmlElement element;
// Filling the username
element = webBrowser.Document.GetElementById("ContentPlaceHolder1_UsernameTextBox");
if (element != null)
{
element.InnerText = "username";
}
// In case if there is no id of the input field you can get it by name
HtmlElementCollection elements = null;
elements = webBrowser.Document.All.GetElementsByName("pass");
element = elements[0];
element.InnerText = "password";
//login (click)
elements = webBrowser.Document.All.GetElementsByName("submit");
element = elements[0];
element.InvokeMember("CLICK");