我对在 C# 中处理网页比较陌生。我要做的是登录特定网站(https://www15.swalife.com/PortalWeb/portal/cwaLogon.jsp)并允许将页面重定向到默认页面,然后从那里导航到( https://www15.swalife.com/csswa/ea/plt/accessELITT.do)并下载源代码并将其输出为字符串。
我已经想出了如何通过 HTTPWebRequest 和 HTTPWebResponse 下载源代码,但是在编写登录功能时遇到了麻烦。我想我将不得不对 POST 做点什么?我也看过http://www.dreamincode.net/forums/topic/152297-c%23-log-in-to-website-programmatically/。
提前致谢!!
编辑:
jimmyjambles 提供的代码完美无缺,但它并没有完全得到我想要的页面的源代码。该代码表明登录过程失败,但我相信通过一些调整我可以让它工作......也适用于每个有问题的人:
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(AcceptAllCertifications);
尝试将“公共字符串”和“公共布尔”函数分别更改为“公共静态字符串”和“公共静态布尔”:)
编辑2:
响应 HTML:
"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\n<HTML>\n<HEAD>\n\n\n\n\n\n\n<META http-equiv=\"Content-Type\" content=\"text/html; charset=ISO-8859-1\">\n<META name=\"GENERATOR\" content=\"IBM WebSphere Studio\">\n<TITLE>endSession.jsp</TITLE>\n<LINK rel=\"stylesheet\" href=\"eipPortletStyles/swalife.css\" type=\"text/css\">\n\t<script type=\"text/javascript\" language=\"JavaScript\" \n\t\tsrc=\"eipCommonJavaScript/eipGeneralFunctions.js\"/> </script>\n\t\t\n<script type=\"text/javascript\">\n\n\tfunction refreshParent()\n\t{\n\t if(window.parent)\n\t {\n\t if(window.parent.name == 'appMainFrame')\n\t window.parent.location = \"/csswa/ea/plt/logout.do\";\n\t // alert('Your session has expired. Please login again. ');\n\t }\n\t}\n\n</script>\n</HEAD>\n<BODY onload=\"refreshParent();\">\n \n\t \t<div class=\"eipErrors\">\n \t\t\t<div class=\"legendLabel\">Message</div>\n \t\t\t\n \t\t\t <div class=\"errorsHeader formTitle\">You Have Exited Out of Crew Web Access.<br> \t\t\t \n \t\t\t </div>\n \t\t\t \n \t\t\t<div class=\"errorsHeader formTitle\"> Please Close this Window and <font size=\"+1\">Log Out of SWALife</font> to Complete the Log Out Process. </div>\n \t\t<div class=\"errorsText\">\n \t\t \n \t\t\t\t\n \t\t</div>\n \t\t\n \t\t\t\n \t\t\n \t\t<div class=\"errorsFooter\">You will need to log back in before continuing.</div> \t\n \t\t\n \t</div>\n \n</BODY>\n</HTML>\n"