Clarification: Finding broken links

Results 1 to 2 of 2

Thread: Clarification: Finding broken links

  1. #1
    Lance Watanabe Guest

    Default Clarification: Finding broken links

    Hi, in my original posting, I stated that I wanted to automate the maintenance of broken links. A few of you responded quite quickly and very accurately with trying to trap a 404 error. However, I am trying to manage a list of EXTERNAL links.<BR><BR>I have a database containing an indefinite number of URLs that I use to create links on my pages. I want to create a process that requests a URL and removes it from my database so that I do not create a page with links that are no longer functional - in essence parsing the response from a remote web server, in advance, so that I do not need to list broken links nor would I need to redirect the user to a custom 404 page.<BR><BR>Thanks! =)

  2. #2
    PratQ Guest

    Default RE: Im in a good mood, heres code

    Heres some code to do what you want. Ive been wanting to muck with XMLHTTP, so your problem was a good place to start. It is using XMLHTTP to retrieve the page data, and check the status being returned. Although in some cases thats not enough. See notes in the code below for more details. <BR><BR>&#060;%@ Language=VBScript %&#062;<BR>&#060;%<BR>&#039 this test trys to retrieve a url and determine if it exists or not<BR><BR> option explicit<BR><BR> Response.Buffer = True<BR> Dim objXML, strURL<BR><BR> &#039strURL = ""<BR> &#039strURL = ""<BR> strURL = ""<BR> &#039strURL = ""<BR><BR> &#039 Create an xmlhttp object:<BR> Set objXML = Server.CreateObject("Microsoft.XMLHTTP")<BR> &#039 Or, for version 3.0 of XMLHTTP, use:<BR> &#039Set xml = Server.CreateObject("MSXML2.ServerXMLHTTP")<BR><BR ><BR> &#039Run through your list here <BR><BR> if IsBrokenLink(objXML, strURL) then<BR> Response.Write "&#060;P&#062;URL &#039" & strURL &"&#039 DOES NOT EXIST&#060;/P&#062;"<BR> &#039 DB Removal code here<BR> else<BR> Response.Write "&#060;P&#062;URL &#039" & strURL &"&#039 EXISTS&#060;/P&#062;"<BR> end if<BR> <BR> &#039unComment this to see the page displayed in your browser<BR> &#039Response.Write xml.responseText<BR> <BR> Set objXML = Nothing<BR><BR>Function IsBrokenLink(oXML,sURL)<BR> Dim iStatus<BR><BR> <BR> &#039 Opens the connection to the remote server.<BR> oxml.Open "GET", sURL, False <BR> <BR> on error resume next<BR> &#039 Actually Sends the request and returns the data:<BR> oxml.Send<BR><BR> If Err then<BR> &#039An error here, usually means the URL did not resolve, and connection<BR> &#039 could not be established, so link is obviously broken<BR> IsBrokenLink = true<BR> else<BR> &#039Do normal processing<BR> iStatus = oxml.status<BR> <BR> if iStatus = 404 then<BR> IsBrokenLink= true<BR> else <BR> if instr(ucase(oxml.responseText),"NOT FOUND") &#060;&#062; 0 then<BR> &#039Checking the status should be enough 95% of the time, but some sites (like 4guys) <BR> &#039 dont return a 404 as status, because they are doing a custom handle.<BR> &#039 You can check for TEXT on the page, although that is not as reliable.<BR> &#039 I would remove this condition entirely, unless there are a lot of URLs <BR> &#039 not being caught.<BR> IsBrokenLink= true<BR> else<BR> IsBrokenLink= false<BR> end if<BR> end if <BR> end if<BR> <BR> on error goto 0<BR>End Function<BR><BR><BR>%&#062;<BR>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts