Create a program the checks all the links in your web page whether they are active or not.
发表于2002/3/11 9:19:00 943人阅读
|Create a program the checks all the links in your web page whether they are active or not.
By S.S. Ahmed
This article will show you how to test your web page for the active links. You will learn how to use the Internet Transfer Control. This is a real world example of inet usage.
In this article I will show you how to create a program that checks whether the links in your web application are active or not. We will use Internet Transfer Control to accomplish this task. This is a real world example where you will see the real use of Internet Transfer Control.
About the application:
Internet Transfer Control is a very handy control. The program will check each link in the web page to see if it’s functioning or not. Suppose, I add web page addresses of different people on my web page, now the web pages usually move to different locations or people simply put their sites off the internet, and all of a sudden my links are dead. If I have more than 100 links on my page, then to check all those pages manually will be a hectic task and we can’t check the URLs on a regular basis. So, we need to automate this process. An easy way is to keep a links database either in Access or in Excel and then check all the links in the database whether they are functioning. You can populate your web page with the live links from your database.
What this program does?
The program we are going to create will perform the following tasks:
1. The program would open a worksheet.
2. It would use OLE Automation to read the first URL and see whether it’s functioning.
3. Write data back to the worksheet, indicating the result for the URL.
4. Repeat the preceding steps for all the URLs in the list.
5. Save and close the worksheet.
What is OLE?
OLE stands for Object Linking and Embedding. It is a technology for transferring and sharing information among applications. Different applications expose their objects that are related to the kind of data the application works with. Automation client is an application that exposed objects belonging to another application. In our case, our VB program will act as an Automation client. An Automation server is an application that exposes programmable objects to other applications. In our case, Excel will act as an Automation server. Excel exposes Automation objects. These objects have properties and methods as their external interface. Properties are named attributes of the Automation object. Methods are functions that work on an Automation object.
More about the application
Let's discuss the layout of the Excel worksheet in the data will be stored. When the program has completed the task, each record in the Excel worksheet will contain an entry specifying the current status of the URL. The application will run minimized in the background while your other applications work normally. The number of links already checked is displayed in the program's title bar and therefore also displayed on the taskbar icon when the program is minimized. This enables the user to keep track of the progress. When the URLs have been checked, the program closes the worksheet and displays a summary report.
URL will be saved in column C, the third cell from the left, and the information as to whether the link is okay is kept in column E. Data begins in row 4.
I have included detailed comments in the code so that you can see how it works. You will have to edit the code before running it. Change the path of the Excel file in the code.
'Save the filename in a variable Const FILE = "C:/hrd_links.xls" (This variable should contain the exact path of the file)
Here is the complete code of the application:
'Create the Excel object Set objExcel = CreateObject("Excel.Application") 'Open the worksheet objExcel.Workbooks.Open MYFILE 'Set the Transfer Protocol Inet1.Protocol = icHTTP 'This is the main function!! Public Sub Check_Links() 'This function will be called to check all the links in the 'worksheet. 'Declare variables Dim var_row As Integer Dim var_URL As String Dim var_buffer As String Dim var_msg As String Dim var_file_not_found As Integer Dim var_server_not_found As Integer Dim var_timeout As Integer Dim var_OK As Integer 'Catch the time-out errors On Error Resume Next 'Set the row variable to the cell where the data starts var_row = STARTROW 'Initialize the variables var_timeout = 0 var_file_not_found = 0 var_OK = 0 var_server_not_found = 0 'Minimize the form frmmain.WindowState = 1 'Loop through all the URLs Do 'Get the URL var_URL = objExcel.Cells(var_row, URL_COL) 'Check whether the first cell is empty If var_URL = "" Then Exit Do 'Open the URL Text1.Text = Inet1.OpenURL(var_URL) 'Avoid tying up the system DoEvents 'Errors messages are found in the first 50 characters 'returned by the openurl method If Len(Text1.Text) > 50 Then var_buffer = Left(Text1.Text, 50) Else var_buffer = Text1.Text End If 'Catch a time-out error If Err = 35761 Then var_msg = "Timed Out" var_timeout = var_timeout + 1 Err.Clear 'If nothing is returned, it means that the server was 'not found ElseIf Text1.Text = "" Then var_msg = "Server not found" var_server_not_found = var_server_not_found + 1 'If error 404 is returned from the URL, it means the 'server was found but he file was not found ElseIf InStr(1, var_buffer, "404") Then var_msg = "File not found" var_file_not_found = var_file_not_found + 1 'else, the link is OK Else var_msg = "OK" var_OK = var_OK + 1 End If 'Save the result back to the worksheet objExcel.Cells(var_row, STATUS) = var_msg 'Move to the next row var_row = var_row + 1 'Display the current status. frmmain.Caption = var_OK + var_file_not_found + var_server_not_found + var_timeout 'Display the results on the form Label1.Caption = "OK: " & var_OK Label2.Caption = "File not found: " & var_file_not_found Label3.Caption = "Server not found: " & var_server_not_found Label4.Caption = "Timed out: " & var_timeout Loop While True 'If all the links have been checked, restore the form frmmain.WindowState = 0 'Close the Worksheet objExcel.Workbooks.Close 'Remove the object from the memory Set objExcel = Nothing 'Display the results var_buffer = "OK: " & var_OK & vbCrLf var_buffer = var_buffer & "Server not found: " & var_server_not_found & vbCrLf var_buffer = var_buffer & "File not found: " & var_file_not_found & vbCrLf var_buffer = var_buffer & "Timed out: " & var_timeout MsgBox var_buffer
Open the Excel workbook and add the links you want the program to check. Close the workbook and run the program. Make sure you are connected to the internet. That’s it.
About S.S. Ahmed
S.S. Ahmed is a senior software engineer and works for a web and software development firm. Ahmed specializes in creating database driven dynamic web sites. He uses ASP and VB for most of what he develops. Ahmed likes to hop into other tools as well. Ahmed has worked in Assembly language, C, C++, FoxPro, QBasic, All version of Visual Basic (currently experimenting with VB.NET), Visual C, Java, ASP, etc. Ahmed enjoys travelling and has been to many parts of the world. Ahmed can be reached at email@example.com
- 打包时字符串多国语言问题（Checks for incomplete translations where not all strings are translated）
- We are unable to complete the review of your app since one or more of your In App Purchases have not
- How To Check Whether Physical Standby is in Sync with the Primary or Not?
- Retrieve Web page or file (including all HTTP headers and me
- YumRepo Error: All mirror URLs are not using ftp, http[s] or file. Eg. </HTML>/
- Whether mandatory donation is acceptable or not?
- （详解）Write operations are not allowed in read-only mode (FlushMode.NEVER/MANUAL): Turn your Session
- Write operations are not allowed in read-only mode (FlushMode.MANUAL): Turn your Session into FlushM
- Write operations are not allowed in read-only mode (FlushMode.NEVER/MANUAL): Turn your Session into
- Create&Debug Your First Web Application In Eclipse For BlackBerry SmartPhones