Asp. Net 5 minutes to realize real time monitoring of web pages

  • 2021-10-15 10:22:42
  • OfStack

1. Why do you use real-time web monitoring

LZ recently bought a house in Wuxi. Although she works in Shanghai, the housing price in Shanghai is too high to bear, so she chose Wuxi, which is acceptable, as her place to settle down. Small partners who have bought a house may know the process of buying a house. There is a very important step in buying a house, that is, the need for commercial housing filing. Simply put, when you buy a house, you can check the filing situation of your purchase at the government commercial housing filing website. If the developer is still selling this house after filing, it must be that more than one house has been sold. Moreover, a very important point is that the next step of bank loans can only be made after a case has been prepared. In the current situation that bank interest is getting higher and higher, it is definitely better to file the loan as early as possible, so it is particularly important to know the filing situation of buying a house as quickly as possible.

Of course, you can also go to the website every day to inquire about your filing. Obviously, this is not the programmer's practice, let alone the. Net programmer's practice. The programmer's practice must be to put on record for 1 time, and the program will be honestly notified to the mobile phone. By the way, I spit at 1 o'clock. It's really slow to inquire about the website for filing.

2. Choose an windows service, a form (winform), an web, or a console program?

Of course, the most suitable service is definitely windows, winform and web are definitely OK, console program is not the most suitable, but definitely the development is the fastest. After comprehensive consideration, because it is a small monitoring project and will only take 1 or 2 months or 1 or 2 days, it has not changed to be so complicated, and windows service has to be installed on the server. The most important thing is that the console is the fastest developed and easiest to deploy, so the console program is finally selected.

STEP 3 Analyze needs

1. It is necessary to inquire about the filing situation every few minutes, so System. Timers. Timer is needed. Of course, if it is a very robust or complete project, it is recommended to use Quartz. NET, and of course you can choose Topshelf, Hangfire, FluentScheduler and so on.

2. Because it is necessary to inquire about the filing situation of the website, obtain the html of the filing webpage, and then judge whether the html contains the word "for sale". If not, it means that it has been filed. System. Net. WebClient is required. Query website. net is more, the most basic is HttpWebRequst, HttpClient, or RestSharp, are good choices.

3. Because of the need to notify the situation in real time, I chose SMS notification from Alibaba Cloud. Because other projects are used, I directly copied the code to use it. Of course, there are many notifications, such as email, app, and so on. Of course, we don't have to be so 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 SMS 
  for (int i = 0; i < 5; i++)
  {
  //  Send a text message 
  SmsMessage.Send("152****7178", "SMS_92310001", new { name = "Emrys", status = " Congratulations, congratulations, the house has been signed for sale! " });
  Thread.Sleep(5 * 1000);
  }
 }
 }
}

5. Deployment

Well, this one is meaningless and meaningful. Generate the code directly, copy the Debug folder in the bin directory and throw it directly to the server. Click to run xxxxxxx. exe. Deployment completed. ^ _ ^

6. Summary

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

2. Now. Net core open source cross-platform and running speed, C # "Beautiful Language", vs universe 1IDE, and nothing else to say, hope. Net is getting better and better.


Related articles: