检查网站是否正常工作并登录

时间:2015-07-23 11:08:22

标签: c# web console-application

我参与了一个项目,其中一部分应该做到以下几点; 1)它应检查网站是否有效。如果它正在工作,它的okey.If它不应该发送电子邮件。 2)使用用户名和密码,它应该登录网站,如果没有登录,它应该再发送一封电子邮件。 它是一个控制台应用程序,所以我想在控制台上输入网站,用户名和密码,让程序执行上述操作。 我正在使用这些代码来发送电子邮件,但是对于其他代码,我看到了很多代码(大多数是用于应用程序的窗口),但对我来说都不够。谢谢你的帮助!

发送电子邮件;

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 个答案:

答案 0 :(得分:0)

如果我理解你,你的目标是登录控制台应用程序中的网站。仅通过代码实现这一过程是一个复杂的过程(取决于网站)。

(1)假设您要登录的网站不是您自己的网站,在这种情况下可以更轻松地完成此任务,我建议您先检查如何给定网站的登录过程有效 如果您使用Firefox,则可以使用Tamper Data。另一种工具是Fiddler。其中一个程序在后台运行,您可以使用首选浏览器登录该站点。

(2)现在您必须分析浏览器发送给服务器的数据。必须将此数据发送到应用程序中的服务器。

由于无法概括此过程,因此您需要为要定位的每个页面执行这些步骤。

最后,您登录网站的代码可能如下所示(我曾用于登录网站的示例代码,另一个示例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();
...