通过C#登录网站,导航到另一个网页,然后将源代码输出为字符串
我在C#中使用网页相对较新。 我想要做的是登录一个特定的网站( https://www15.swalife.com/PortalWeb/portal/cwaLogon.jsp )并允许将页面重定向到默认页面,然后从那里导航到( https://www15.swalife.com/csswa/ea/plt/accessELITT.do)并下载源代码并将其输出为字符串。
我已经想出了如何通过HTTPWebRequest和HTTPWebResponse下载源代码,但是在编写函数登录时遇到了麻烦。 我想我将不得不用POST做点什么? 我也查看了https://www.dreamincode.net/forums/topic/152297-c%23-log-in-to-website-programmatically/ 。
提前致谢!!
编辑:
jimmyjambles提供的代码完美无瑕,除了它不能完全得到我想要的页面的源代码。 该代码表明登录过程失败了,但我相信通过一些调整我可以让它工作……也适用于所有遇到问题的人:
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(AcceptAllCertifications);
尝试将“public string”和“public bool”函数分别更改为“public static string”和“public static bool”:)
编辑2:
回复HTML:
"nnnnnnnnnnnendSession.jsp nnt nttnnntfunction refreshParent()nt{nt if(window.parent)nt {nt if(window.parent.name == 'appMainFrame')nt window.parent.location = "/csswa/ea/plt/logout.do";nt // alert('Your session has expired. Please login again. ');nt }nt}nnnnn nt tn tttMessagen tttn ttt You Have Exited Out of Crew Web Access.
ttt n ttt n ttt n ttt Please Close this Window and Log Out of SWALife to Complete the Log Out Process. n ttn tt n ttttn ttn ttn tttn ttn tt tn ttn tn nnn"
为了在登录后使用HttpWebRequest访问辅助URL,您需要记住一些事情。
首先,正如Casperah所提到的,您需要检查登录表单并确定用于接收登录数据的控件的“名称”属性。
完成此操作后,您需要相应地格式化一个post字符串并将其提供给WebRequest。
最后一个考虑因素是,一旦您登录,您将需要存储和维护从您登录的服务器分配给您的cookie。
我从这篇msdn文章中获取了一个WebRequest片段,并对其进行了修改,以便在登录后执行第二页请求。
string loginurl = "https://www.gmail.com"; string secondurl = "https://mail.google.com/prefs"; string username = "bob@gmail.com"; string password = "12345"; GetSecondaryLoginPage(loginurl, secondurl, username, password); public string GetSecondaryLoginPage(string loginurl, string secondurl, string username, string password, string cookieName = null) { // Create a request using a URL that can receive a post. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(loginurl); // Set the Method property of the request to POST. request.Method = "POST"; CookieContainer container = new CookieContainer(); if (cookieName != null) container.Add(new Cookie(cookieName, username, "/", new Uri(loginurl).Host)); request.CookieContainer = container; // Create POST data and convert it to a byte array. Modify this line accordingly string postData = String.Format("username={0}&password={1}", username, password); ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(AcceptAllCertifications); byte[] byteArray = Encoding.UTF8.GetBytes(postData); // Set the ContentType property of the WebRequest. request.ContentType = "application/x-www-form-urlencoded"; // Set the ContentLength property of the WebRequest. request.ContentLength = byteArray.Length; // Get the request stream. Stream dataStream = request.GetRequestStream(); // Write the data to the request stream. dataStream.Write(byteArray, 0, byteArray.Length); // Close the Stream object. dataStream.Close(); // Get the response. WebResponse response = request.GetResponse(); // Get the stream containing content returned by the server. dataStream = response.GetResponseStream(); // Open the stream using a StreamReader for easy access. StreamReader reader = new StreamReader(dataStream); // Read the content. string responseFromServer = reader.ReadToEnd(); using (StreamWriter outfile = new StreamWriter("output.html")) { outfile.Write(responseFromServer.ToString()); } // Clean up the streams. reader.Close(); dataStream.Close(); response.Close(); request = (HttpWebRequest)WebRequest.Create(secondurl); request.CookieContainer = container; response = request.GetResponse(); // Get the stream containing content returned by the server. dataStream = response.GetResponseStream(); // Open the stream using a StreamReader for easy access. reader = new StreamReader(dataStream); // Read the content. responseFromServer = reader.ReadToEnd(); // Clean up the streams. reader.Close(); dataStream.Close(); response.Close(); return responseFromServer; } public bool AcceptAllCertifications(object sender, System.Security.Cryptography.X509Certificates.X509Certificate certification, System.Security.Cryptography.X509Certificates.X509Chain chain, System.Net.Security.SslPolicyErrors sslPolicyErrors) { return true; }
唯一添加的行是postData和cookies。
您需要修改该行
string postData = String.Format("username={0}&password={1}", username, password);
根据您在表单上的控件,因为您发布了您尝试使用的网站,我猜你可能正在寻找
string postData = String.Format("uid={0}&portalBase=cwa&password={1}", username, password);
在表单中使用WebBrowser相当容易。 首先导航到cwaLogon.jsp页面,找到输入控件,然后在提交按钮上调用“单击”。 使用HTTPWebRequest和Response进行相应的GET / POST。 使用Fiddler2来检查要发布的内容是一个很好的开始。
上述就是C#学习教程:通过C#登录网站,导航到另一个网页,然后将源代码输出为字符串分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/cdevelopment/1024664.html