Results 1 to 6 of 6

Thread: Effective way to search a file occurrence on web server

  1. #1
    Join Date
    Jun 2009
    Posts
    1,205

    Effective way to search a file occurrence on web server

    I want a fast and effective way to search and check whether a file (whose name I know) is present on a web server (also known). I spent a lot of time in searching this on MSDN and others, but without much result. I have reviewed the My .Computer.Network and System.Net but in vain. The best I found was the system.net.webclient, but I just need a boolean return.

  2. #2
    Join Date
    Nov 2008
    Posts
    1,221

    Re: Effective way to search a file occurrence on web server

    There must be an HTTP "HEAD" (and not POST or GET) in order to retrieve only the headers. If you retrieve a code 200 (OK), the file exists. If no, its a 404 (Not found).

    The following method returns True if the URL exists (200), False if it does not (404), and throws an exception if another error occurs
    Code:
    Function CheckUrlExists(url As String) As Boolean
        Dim request As HttpWebRequest = DirectCast(WebRequest.Create(url), HttpWebRequest)
        request.Method = "HEAD"
        Try
            Using response As HttpWebResponse = DirectCast(request.GetResponse(), HttpWebResponse)
                If response.StatusCode = HttpStatusCode.OK Then
                    Return True
                Else
                    Return False
                End If
            End Using
        Catch ex As WebException
            Dim response As HttpWebResponse = TryCast(ex.Response, HttpWebResponse)
            If response IsNot Nothing AndAlso response.StatusCode = HttpStatusCode.NotFound Then
                Return False
            Else
                Throw
            End If
        End Try
    End Function

  3. #3
    Join Date
    Jun 2009
    Posts
    1,205

    Re: Effective way to search a file occurrence on web server

    I stopped on that famous System.Net.HttpWebRequest but it must be said that the doc gives me ... how to say ... perplexed. A huge thank you for this function that I'm going to test this step. I just added a timeout and specifications. For that, I wonder why throw?

  4. #4
    Join Date
    Nov 2008
    Posts
    1,221

    Re: Effective way to search a file occurrence on web server

    In fact, HttpWebRequest.GetResponse always raises an exception if the server returns an error code (which I find it very useful indeed ...), where the catch. If the exception is due to a 404 error its an error "expected", therefore it returns false to say that the file does not exist. But, if another error like access denied, can not determine if the file exists, so we raise the exception caught by the catch (that's what serves Throw without parameter, if when you asked)

  5. #5
    Join Date
    Jun 2009
    Posts
    1,205

    Re: Effective way to search a file occurrence on web server

    I just realized that the call is execute the file request. And that's not good! Another idea? These are files .cgi or .pl (all in perl) in chmod 755. The drawback is that most expect datas and run to thing which can cause other actions etc. I created several files to my tests, but if really it was a damn board. Just a quick example:

    As I control what comes in and go out of my cgi, so there call 1 (or lack there of datas), this causes a problem (I manage) and sent me (among others) mail alerts. In the worst cases, it can crash the server. Reflecting a bit, and if you have no other options to check the file (without executing), I can get around to calling another cgi/perl.

  6. #6
    Join Date
    Nov 2008
    Posts
    1,221

    Re: Effective way to search a file occurrence on web server

    Wait, you're scaring me here; these scripts are supposed to run in HTTP or not? Because if this is not the case, you would have interest in reviewing speed permissions for files and web server conf

    If they are scheduled to run in HTTP, there is no way to access it without the run (thankfully, otherwise we could get the source server from any site). But you can create a script that checks the server if the specified file exists, you call this script and you look at the result

Similar Threads

  1. Effective File Search is not working in Windows 7
    By Amma-Watson in forum Windows Software
    Replies: 5
    Last Post: 13-04-2010, 01:48 AM
  2. How to deploy Search Server 2008
    By Agneya in forum Windows Software
    Replies: 5
    Last Post: 12-03-2010, 04:24 AM
  3. Where to search Windows log file in XP
    By Aaron007 in forum Operating Systems
    Replies: 3
    Last Post: 14-08-2009, 05:08 PM
  4. Search for free SVN server
    By NAYASA in forum Networking & Security
    Replies: 4
    Last Post: 03-01-2009, 01:44 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,078,714.56635 seconds with 17 queries