• Home >> Visual Basic >>  Sample Chapter
  • create a program that checks all the links in your web page whether they are active or not.


  • Rating : Excellent[0]   Very Good[0]   Average[0]   Below Average[0]   Poor[0]
  • Pub Date: 10/8/2008
  • admin [ View Profile ]

  • Abstract
  • 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 applicationInternet 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. S......

  • Introduction
  •  Sponsored Links
  • sample image - linkchecker.jpg

    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 expose 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 where 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.

  • RATE THIS ARTICLE :
  •  
    • Latest Comments:
      • Add a comment
      • Title:
      • Comment
      •  
    Other popular Sample Chapter articles:
    • The .NET Architecture

      This article is a sample chapter from .NET Security Programming, written by Donis Marshall and published by Wiley.

    • Connecting to Oracle or Access from ASP.NET 2.0

      The SqlDataSource control is the data source control to use if your data is stored in a SQL Server, SQL Server Express, Oracle Server, ODBC data source, OLE DB data source, or Windows SQL CE Database. The control provides an easy-to-use wizard that walks you through the configuration process, or you

    • Using Master Pages

      The following is Chapter 3, "Using Master Pages," from Enhancing Microsoft Content Management Server with ASP.NET 2.0, published by Packt Publishing. Reprinted with the publisher's permission.Using Master Pages

    • Flags Enumerations

      Definition of Flags EnumeratorsA flags enumerator enables a series of boolean values to be tied to a single datatype. Instead of assigning one of a predefined series of values to a variable, it will instead store a byte that results from the concatenation of all possible values in the series.

    • embedding of vb6 form in .net applications

      Typically, the available tools that provide interoperation between .NET code and VB6 code are based on proxies, wrappers or other forms of middleware that allow VB6 applications to call .NET Winforms. So far, we haven't seen a solution that allows us to do the opposite. That is, to embed VB6 forms i

    • create vb add-ins

      Why should you create Add-Ins? We programmers always feel that we are short of several features while working with Microsoft tools, it seems that Microsoft hasn’t yet developed the tool we needed. In fact, Microsoft has done a wonderful job of adding new features to each release of its development t

    • set or clear "manager can update membership list" checkbox with vbscript

      This program checks or un-checks the "Manager can update membership list" check box for every group contained in the OU specified.(if there's a manager assigned.) BackgroundI recently migrated a bunch of distribution groups from a child domain to its parent using the active directory migra

    • cthread

      CThread makes it possible to work with threads in MFC pretty much the same way as you do in C#/Java. You derive your own class from CThread and implement Run(void* ). To start the thread, you have to call Start(). Now a new thread starts it's execution in Run(). When Run() is finished, the thread st

    • i/o completion port dll

      The purpose of this article is to explore the IO Completion Port mechanism provided in Windows and compare it with the other mechanisms available to wait for an IO completion.Large-scale software often needs tens of thousands of socket connections, and if one socket corresponds to one thread, meanin

    • vb6 save form image to file

      This is sample code to save image of the VB 6.0 form to a file. BackgroundNormally the Form.image property only provides the drawing and printedtext in the bitmap image. However if there are image controls, buttons,icons etc on the form then this code pictures those too.Using the codeThere is one si

    About Us |Contact us |Site Map |Csharp |Visual C / C++ |Visual basic |Java |SQL |Linux / Unix |Ajax
    ©2007-2018 CodeCoolest.com. Ptolive.cn Asp.net source code All Rights Reserved.