Use ASP.NET1.1 server-side Timer Timer to read RSS information to the database in a virtual host

xiaoxiao2021-04-04  246

Some time ago, a blog promotion website: Chinese blog group (http://www.bokequn.cn), there is a function in the inside to read the articles of the blog to the database, and then display it for user query. And handle Tag. Because it is a virtual host, just think of using the server-side Timer to solve.

At that time, it was more in a hurry to optimize, and the level is also very limited. If you feel that some places are not alive or have a better way to solve, please do not enlighten me.

I think the whole process two focuses:

How to deal with Timer will read RSS modularization, more convenient to call

1. About Timer, of course, is written in Global.asax, first establish a variable:

System.Timers.Timer T

=

New

System.timers.Timer

1000

*

Convert.TOINT16 (System.Configuration.configurationSettings.AppSettings [

"

Do_time

"

]));

Where do DO_TIME in Web.config to access how long

Next, in Application_Start:

T. /ToreSet

=

True

; t.enabled

=

True

;

IF

(Common.func.get_Key)

"

Can_do

"

)

==

"

1

"

)

//

Also in web.config, regular processing, where common.func.get_key is a function of custom acquisition settings

T.ELAPSED

=

New

System.Timers.ElapseDeventhandler (FUN);

Function FUN code:

Private

Void

Fun (

Object

Sender, System.Timers.ELAPSEDEventArgs E)

{Try {system.datetime DT = system.datetime.now; common.func.write_log ("Start Read Remote XML"); // Write Log RSS2SQL RL = New RSS2SQL (); rl.readall (); System. DateTime DT2 = system.datetime.now; common.func.write_log ("XML file import to database success" Convert.TOSTRING (DT2-DT)); // Write this execution time to log,} catch (Exception EX) ) {Common.func.write_log ("Read the remote XML file error:" ex.Message); // Write log}}

Global.asax in China

2. Next is to get the RSS list from the database in the RSS2SQL class. Read the RSS file according to the last read RSS time and the modification time of the RSS file and write it into the database, because it is used to use too many custom classes and methods. So don't list all the code, interested friends can go to the last download, in which important functions:

/ ** /

///

/// Read the RSS content of the specified link to the database /// /// corresponding blog ID //// RSS address /// last read time public

Void

Read_From_url

String

ID,

String

URL, System.DateTime DT)

{op_db.db_class db1 = new op_db.db_class (); try {rssfeed feed = new {r/ (URL, DT); // Establish an RSS read class instance feed.read (); // Start reading if (feed.) CHANNEL.ITEMS.COUNT> 0) // If the number of articles is greater than 0, began to read to the database {for (int i = 0; i

Where rssfeed is the class that handles the processing of RSS. For this class, I have used the lyvolved RSS.NET for a while, but in the actual process, because of the various RSS addresses, even many don't quite compliant, so I will encounter Many problems, and read the source code, what he wants to deal with too much, the code volume is also very large, I estimate that it is estimated that I will change it for a while, so I have made a piece of control, and I don't count. The upper control is a few classes, of course, the content I have dealt with is relatively simple, only the title of RSS and the introduction, Item title, introduction, time, link.

The code is passed too much, interested friends can go to the last download

Usage is quite simple, you can use the compiled DLL reference to use:

Rssfeed feed = new rssfeed (url, dt); // establish an RSS read class instance feed.read (); // Start reading feed.channel.titlefeed.channel.description proper.channel.items.countfeed.channel.Items [i] .titlefeed.channel.items [i] .descriptionfeed.channel.Items [i] .linkfeed.channel.Item.Items [i] .pubdate

The DT in the constructor indicates the last read time, because the modification time of the RSS file is obtained in the class, if it is more than the last reading time, otherwise it will not be processed, others can believe you can I understand that there is still a problem in RSS reading, that is, if there is 16 credit characters in the XML file (of course this appearance is not big), you can't read it. Which person knows how to solve it, please tell it.

Some chaos say, or want to help with friends who need this feature.

Related download: http://www.cnblogs.com/files/guanvee/code.rar

Http://guanvee.cnblogs.com/archive/2006/06/17/428329.html

转载请注明原文地址:https://www.9cbs.com/read-131814.html

New Post(0)