简体   繁体   English

使用C#提交HTML表单提交

[英]Submit an HTML form submit using C#

I'm working on a school project which I want to see our timetable from our Windows8.1 devices with an universal app. 我正在做一个学校项目,我想从带有通用应用程序的Windows8.1设备上查看时间表。 So that I don't have to log in every time I want to check it. 这样我就不必每次都要检查时都登录。 I need a method that logs me in to our school's website and lets me see the source code so I can see the lessons. 我需要一种方法来登录我学校的网站,并让我查看源代码,以便可以查看课程。

Website I need to log in with C# codes is here . 我需要使用C#代码登录的网站在这里

Source code seems like: 源代码看起来像:

<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&amp;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>

This is it, basically. 基本上就是这样。 There are some __VIEWSTATE's but I don't know if they matter. 有一些__VIEWSTATE,但我不知道它们是否重要。

I found 2 type of solutions. 我发现了两种类型的解决方案。

  1. WebRequest examples here on Stack Overflow which didn't work :/ 此处的WebRequest示例在Stack Overflow上不起作用:/

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

In another websites, if I try this code, I get an error like "You have to allow cookies to login." 在另一个网站上,如果尝试使用此代码,则会收到类似“您必须允许cookie登录” 的错误 but on the website of my school I don't get any, not even "Wrong password". 但是在我学校的网站上我什么也没得到,甚至没有“密码错误”的信息。 (If I type wrong password on a browser, I get the wrong password error.) (如果在浏览器中输入错误的密码,则会收到错误的密码错误。)

  1. Duplicating the form to an .HTML file and use WebView to log in with JavaScript. 将表单复制到.HTML文件,然后使用WebView通过JavaScript登录。 If I try this, I get redirected to another page and get a very weird error like "User null couldn't recognized". 如果尝试这样做,我将重定向到另一个页面,并收到一个非常奇怪的错误,例如“无法识别用户null”。 So these 2 type of solutions didn't work for me. 因此,这两种类型的解决方案对我不起作用。

So, the question is, how can I log in to website with C# ? 因此,问题是,如何使用C#登录网站

Example code in WebBrowser document completed event: 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");

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

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