问题
I am on a project and part of it should do the following; 1) it should check the website if it is working or not. If it is working,its okey.If not it should send a e-mail. 2)With username and password it should login the website and again if it is not logged in it should send a e-mail. It is a console application so i want to enter website,username,password on console and let the program do the above. I am using these codes to send e-mail and it is okey but for the rest,i saw so many codes(mostly they were for windows for application) but none of them were enough for me. Thanks for 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);
回答1:
If i understand you right your goal is to login to a website in your console application. Achieving this solely through code is a complex process (depending on the website).
(1) Assuming the website you want to login to is not your own, in which case this task could be made much more easier, i'd advise you to check first how the login process for the given site works.
If you use Firefox you could use Tamper Data. An alternative tool for would be Fiddler. With one of these programs running in background, you login to the site using your preferred browser.
(2) Now you have to analyse what data your browser sent to the server. This data has to be send to the server in your application.
Since there is no way to generalize this process, you would need to do these steps for each page you want to target.
In the end your code to login to a site could look like this (example code i once used to login to a website, another example here):
using System;
using System.IO;
using System.Net;
using System.Text;
using System.Threading.Tasks;
...
cookie = new CookieContainer();
//Encoding for the post data
Encoding iso = Encoding.GetEncoding("ISO-8859-1");
// Get initial needed cookie via a 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 to submit username and 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 to request stream
using (Stream stream = await request.GetRequestStreamAsync())
{
stream.Write(dataBytes, 0, dataBytes.Length);
}
// Get session data page
response = (HttpWebResponse)(await request.GetResponseAsync());
// Read session data (username and 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 that will send that data to the 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 to request stream
using (Stream stream = await request.GetRequestStreamAsync())
{
stream.Write(dataBytes2, 0, dataBytes2.Length);
}
response = (HttpWebResponse)(await request.GetResponseAsync());
response.Dispose();
...
来源:https://stackoverflow.com/questions/31585606/checking-if-website-is-working-and-login