.net - Logging into a website via C#, navigating to another web page, and then output the source code as a string -
i relatively new working web pages in c#. trying log particular website ( https://www15.swalife.com/portalweb/portal/cwalogon.jsp ) , allowing page redirected default page, , navigating there (https://www15.swalife.com/csswa/ea/plt/accesselitt.do) , downloading source code , output string.
i have figured out how download source code via httpwebrequest , httpwebresponse having trouble on coding logging in function. assume have post? have looked @ http://www.dreamincode.net/forums/topic/152297-c%23-log-in-to-website-programmatically/ also.
thanks in advance!!
edit:
the code supplied jimmyjambles works flawlessly, except doesn't quite me source code of page wanted. code suggests log-in process failed, believe little bit of tweaking work...also having issues with:
servicepointmanager.servercertificatevalidationcallback = new remotecertificatevalidationcallback(acceptallcertifications);
try changing "public string" , "public bool" functions "public static string" , "public static bool" respectively :)
edit 2:
response 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 window , <font size=\"+1\">log out of swalife</font> complete 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 need log in before continuing.</div> \t\n \t\t\n \t</div>\n \n</body>\n</html>\n"
in order use httpwebrequest access secondary url after login need keep in mind few things.
firstly casperah has mentioned need inspect login form , identify "name" attribute of controls used receive login data.
once have done need format post string accordingly , provide webrequest.
the last consideration once have logged in, need store , maintain cookie assigned server keeps logged in.
i have taken webrequest snippet this msdn article , modified perform second page request after login.
string loginurl = "http://www.gmail.com"; string secondurl = "http://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 request using url can receive post. httpwebrequest request = (httpwebrequest)webrequest.create(loginurl); // set method property of request 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 , convert byte array. modify 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 contenttype property of webrequest. request.contenttype = "application/x-www-form-urlencoded"; // set contentlength property of webrequest. request.contentlength = bytearray.length; // request stream. stream datastream = request.getrequeststream(); // write data request stream. datastream.write(bytearray, 0, bytearray.length); // close stream object. datastream.close(); // response. webresponse response = request.getresponse(); // stream containing content returned server. datastream = response.getresponsestream(); // open stream using streamreader easy access. streamreader reader = new streamreader(datastream); // read content. string responsefromserver = reader.readtoend(); using (streamwriter outfile = new streamwriter("output.html")) { outfile.write(responsefromserver.tostring()); } // clean streams. reader.close(); datastream.close(); response.close(); request = (httpwebrequest)webrequest.create(secondurl); request.cookiecontainer = container; response = request.getresponse(); // stream containing content returned server. datastream = response.getresponsestream(); // open stream using streamreader easy access. reader = new streamreader(datastream); // read content. responsefromserver = reader.readtoend(); // clean 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; }
the added lines postdata , cookies.
you need modify line
string postdata = string.format("username={0}&password={1}", username, password);
based on controls on form, since posted site trying work can guess looking for
string postdata = string.format("uid={0}&portalbase=cwa&password={1}", username, password);
Comments
Post a Comment