Asp.Net Real time monitoring of web pages in 5 minutes

Time:2021-3-29

1、 Why real time Web monitoring

LZ recently bought a house in Wuxi. Although he works in Shanghai, the house price in Shanghai is too high to bear, so he chooses Wuxi as a place to live. A small partner who has bought a house may know the process of buying a house. One of the most important steps in buying a house is to put it on record. To put it simply, when you buy a house, you can check the record of your purchase at the government’s commercial housing record website. If it has been put on record and the developer is still selling the house, it must be more than one house. What’s more, it’s very important that only after preparing the case can we make the next bank loan. In the current situation of higher and higher bank interest, the earlier the record is, the earlier the loan is, the more advantageous it is. Therefore, it’s particularly important to know the record of your purchase of a house as soon as possible.

Of course, you can also go to the website every day to check your filing situation. Obviously, this is not the way of programmers, let alone. Net programmers. The programmer’s practice is certainly that once the record comes down, the program will be informed to the mobile phone honestly. By the way, make complaints about websites that are slow.

2、 Window, WinForm, or windows console?

Of course, the most appropriate is certainly windows services, WinForm and web can certainly, console program is not necessarily the most appropriate, but certainly the fastest development. After a comprehensive consideration, because it is a very small monitoring project, and it will only take a month or two, or a day or two, it is not as complicated as it needs to be done. Moreover, windows services have to be installed on the server. The most important thing is that the console is the fastest to develop and the easiest to deploy. So we finally chose the console program.

3、 Demand analysis

1. You need to check the record every few minutes, so you need to System.Timers.Timer . Of course, if it is a very robust or complete project, it is recommended to use Quartz.NET Of course, you can choose topshelf, hangfire, fluent scheduler, etc.

2. Because you need to query the filing situation of the website, get the HTML of the filing page, and then judge whether the HTML contains the word “for sale”. If not, it means that it has been filed. This is the time System.Net.WebClient . It’s more convenient to query the website. Net. The most basic ones are httpwebrequst, httpclient, or restsharp.

3. Because real-time notification is needed, Alibaba cloud’s SMS notification is selected. Because it is used in other projects, the code is copied directly. Of course, there are also many notifications, such as email, app, etc. Of course, we don’t have to be that complicated.

4、 Code module

Direct code, logic and code are simple.

class Program
{
 static System.Timers.Timer timer = null;
 static void Main(string[] args)
 {
 timer = new System.Timers.Timer(2 * 60 * 1000);
 timer.Elapsed += Timer_Elapsed; ;
 timer.Start();
 Console.ReadKey();
 }
 private static void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
 {
 WebClient client = new WebClient
 {
  Encoding = System.Text.Encoding.GetEncoding("utf-8")
 };
 var html = client.DownloadString("http://www.xxxx.com/xxxxx.html");
 Console.WriteLine ( DateTime.Now.ToString ("yyyy MM DD HH: mm: SS") + "..." + "not signed. ");
 if ( html.IndexOf ("for sale") = = - 1)
 {
  if (timer != null)
  timer.Stop();
  //Send 5 short messages
  for (int i = 0; i < 5; i++)
  {
  //Send SMS
  SmsMessage.Send ("152****7178", "SMS_ 92310001 ", new {name =" Emrys ", status =" Congratulations, the house has been signed! " });
  Thread.Sleep(5 * 1000);
  }
 }
 }
}

5、 Deployment

OK, this is meaningless. Generate code directly, copy the debug folder in the bin directory, throw it directly to the server, and click Run xxxxxxx.exe . Deployment is complete. ^_^

6、 Summary

1. Although it was just a whim at that time, and there was no specific statistical time, picking up vs was a surprise. All code and deployment should not take more than five minutes. It can be seen from this that. Net has done well in some aspects with vs.

2. Now. Net core is open source, cross platform and running speed, C # “beautiful language”, vs the first ide in the universe, not to mention others. I hope. Net is getting better and better.

The above is the introduction of Xiaobian Asp.Net 5 minutes to achieve real-time web monitoring, I hope to help you, if you have any questions, please leave me a message, Xiaobian will reply to you in time. Thank you very much for your support to developer!