检查网站是否正常运行并登录 [英] Checking if website is working and login

查看:89
本文介绍了检查网站是否正常运行并登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个项目上,其中一部分应执行以下操作;
1)它应检查网站是否正常运行。如果工作正常,就可以了。否则,应该发送电子邮件。
2)使用用户名和密码,它应该登录该网站,如果未登录,则应再次发送电子邮件。
这是一个控制台应用程序,因此我想在控制台上输入网站,用户名,密码,然后让程序执行上述操作。
我正在使用这些代码发送电子邮件,这很不错,但对于其余的代码,我看到了那么多代码(主要是用于应用程序的Windows),但它们对我来说都不足够。谢谢您的帮助!

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!

用于发送电子邮件;

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 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)假设您要登录的网站是不是您自己的,在这种情况下可以简化此任务,我建议您首先检查如何给定站点的登录过程。

使用Firefox,您可以使用篡改数据。另一种工具是提琴手。在其中一个程序在后台运行的情况下,您可以使用首选的浏览器登录该网站。

(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)现在,您必须分析什么浏览器发送到服务器的数据。此数据必须发送到应用程序中的服务器。

(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();
...

这篇关于检查网站是否正常运行并登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆