c# - Checking if website is working and login -


i on project , part of should following; 1) should check website if working or not. if working,its okey.if not should send e-mail. 2)with username , password should login website , again if not logged in should send e-mail. console application want enter website,username,password on console , let program above. using these codes send e-mail , okey rest,i saw many codes(mostly windows application) none of them enough me. help!

for sending e-mails;

mailmessage mail = new mailmessage();                 smtpclient smtpserver = new smtpclient("smtp.gmail.com");                 mail.from = new mailaddress("blabla@gmail.com");                 mail.to.add("blabla2@yandex.com");                 mail.subject = "login";                 mail.body += "login failed";                 mail.isbodyhtml = true;                 smtpserver.port = 587;                 smtpserver.credentials = new system.net.networkcredential("blabla@gmail.com", "**********");                 smtpserver.enablessl = true;                 smtpserver.send(mail); 

if understand right goal login website in console application. achieving solely through code complex process (depending on website).

(1) assuming website want login not own, in case task made more easier, i'd advise check first how login process given site works.
if use firefox use tamper data. alternative tool fiddler. 1 of these programs running in background, login site using preferred browser.

(2) have analyse data browser sent server. data has send server in application.

since there no way generalize process, need these steps each page want target.

in end code login site (example code once used login website, example here):

using system; using system.io; using system.net; using system.text; using system.threading.tasks;  ... cookie = new cookiecontainer();  //encoding post data encoding iso = encoding.getencoding("iso-8859-1");  // initial needed cookie via head request httpwebrequest request = webrequest.createhttp(logon_url); request.method = "head";  httpwebresponse response = (httpwebresponse)(await request.getresponseasync()); cookie.setcookies(new uri(cookie_url, urikind.absolute), response.headers["set-cookie"]);  // create request submit username , password request = webrequest.createhttp(logon_url); request.method = "post"; request.contenttype = "application/x-www-form-urlencoded"; request.cookiecontainer = cookie; request.accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";  //prepare password password = preparepassword(password);  string postdatalogin = "login=anmelden&j_username=" + username + "&j_password=" + password + "&action=login"; byte[] databytes = iso.getbytes(postdatalogin);  // write post data request stream using (stream stream = await request.getrequeststreamasync()) {     stream.write(databytes, 0, databytes.length); }  // session data page response = (httpwebresponse)(await request.getresponseasync());  // read session data (username , encrypted password) byte[] sentdata = new byte[response.contentlength];  using (stream reader = response.getresponsestream()) {     reader.read(sentdata, 0, senddata.length); }  response.dispose();  string responsecontent = iso.getstring(sentdata, 0, sentdata.length);  // create request send data server request = webrequest.createhttp(security_check_url); request.method = "post"; request.cookiecontainer = cookie; request.contenttype = "application/x-www-form-urlencoded"; request.accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";  tuple<string, string> tuple = extractusernameandpassword(responsecontent); string postdatasession = "j_username=" + tuple.item1 + "&j_password=" + tuple.item2; byte[] databytes2 = iso.getbytes(postdatasession);  // write post data request stream using (stream stream = await request.getrequeststreamasync()) {     stream.write(databytes2, 0, databytes2.length); }  response = (httpwebresponse)(await request.getresponseasync()); response.dispose(); ... 

Comments

Popular posts from this blog

javascript - Karma not able to start PhantomJS on Windows - Error: spawn UNKNOWN -

c# - Display ASPX Popup control in RowDeleteing Event (ASPX Gridview) -

Nuget pack csproj using nuspec -