分享
 
 
 

Create a program the checks all the links in your web page whether they are active or not.

王朝other·作者佚名  2006-01-08
窄屏简体版  字體: |||超大  

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.

Advanced

VB, XP, W2K, Win9X

Posted 12 Nov 2001

Updated 13 Nov 2001

Articles by this author

Send to a friend

Printer friendly version

Views: 1,693

[This is an unedited reader contribution]

[Modify this article]

[Delete this article]

Articles Authors Titles Abstract

FAQ

What's New

Lounge

Contribute

Message Boards

6 users have rated this article. result:

2.33 out of 5.

Download source files - 11.2 Kb

Download demo project - 9.13 Kb

INTRODUCTION

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 ss_ahmed1@hotmail.com

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有