Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. how to check if a remote file exists

how to check if a remote file exists

Scheduled Pinned Locked Moved Web Development
csharpasp-netcomsysadmin
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Chandman
    wrote on last edited by
    #1

    I tried to figure out (for couple of days now) how to check to see if a remote file exists on a different server where the web application is running. If it was on a same server I would just use server.mappath and I would have no problem. But the files are on a totally different server on different domain. So, what I'd like to do is just say checkIfThisFileExistsOnRemoteServer(http://someserver.com/directory/file.pdf) Is there a asp.net (C# or VB.NET, does not matter) way to do this. Thank you, in advance. - Chandman

    G T 2 Replies Last reply
    0
    • C Chandman

      I tried to figure out (for couple of days now) how to check to see if a remote file exists on a different server where the web application is running. If it was on a same server I would just use server.mappath and I would have no problem. But the files are on a totally different server on different domain. So, what I'd like to do is just say checkIfThisFileExistsOnRemoteServer(http://someserver.com/directory/file.pdf) Is there a asp.net (C# or VB.NET, does not matter) way to do this. Thank you, in advance. - Chandman

      G Offline
      G Offline
      Guffa
      wrote on last edited by
      #2

      Unless the folder has directory browsing enabled (which is quite unusual today), there is no way that you can check if a file exists. You can request the file from the server and check the response that you are getting.

      --- single minded; short sighted; long gone;

      C 1 Reply Last reply
      0
      • C Chandman

        I tried to figure out (for couple of days now) how to check to see if a remote file exists on a different server where the web application is running. If it was on a same server I would just use server.mappath and I would have no problem. But the files are on a totally different server on different domain. So, what I'd like to do is just say checkIfThisFileExistsOnRemoteServer(http://someserver.com/directory/file.pdf) Is there a asp.net (C# or VB.NET, does not matter) way to do this. Thank you, in advance. - Chandman

        T Offline
        T Offline
        theJazzyBrain
        wrote on last edited by
        #3

        This depends on the protocol you will use to communicate with te remote host. For example, in HTTP if the page you request does not exist you get a 404 response.

        Jason Kataropoulos

        Excellence is not an act, but a habit!

        Aristotle

        C 1 Reply Last reply
        0
        • G Guffa

          Unless the folder has directory browsing enabled (which is quite unusual today), there is no way that you can check if a file exists. You can request the file from the server and check the response that you are getting.

          --- single minded; short sighted; long gone;

          C Offline
          C Offline
          Chandman
          wrote on last edited by
          #4

          Yes, that's what exactly I want to do: Request the file from the server and the check the response. My question was how to make this request and how to read the response inside asp.net.

          1 Reply Last reply
          0
          • T theJazzyBrain

            This depends on the protocol you will use to communicate with te remote host. For example, in HTTP if the page you request does not exist you get a 404 response.

            Jason Kataropoulos

            Excellence is not an act, but a habit!

            Aristotle

            C Offline
            C Offline
            Chandman
            wrote on last edited by
            #5

            Yes, I am using HTTP and I'd like to get that 404 response if the file does not exist. My question was how to make this request and how to read the response inside asp.net. In college I learnt application layer protocols and I'd just send the requests to remote server such as SMTP, HTTP and send me responses like 404, 500 or whatever. I just want to do the same thing in ASP.NET.

            T 1 Reply Last reply
            0
            • C Chandman

              Yes, I am using HTTP and I'd like to get that 404 response if the file does not exist. My question was how to make this request and how to read the response inside asp.net. In college I learnt application layer protocols and I'd just send the requests to remote server such as SMTP, HTTP and send me responses like 404, 500 or whatever. I just want to do the same thing in ASP.NET.

              T Offline
              T Offline
              theJazzyBrain
              wrote on last edited by
              #6

              Use the WebRequest object. Here is an example, this example though uses POST to send some XML and gets the response as string. play around with this and see what you get. HttpWebRequest wrWebRequest = WebRequest.Create(theUrl) as HttpWebRequest; wrWebRequest.Method = "POST"; wrWebRequest.ContentLength = theXml.Length; wrWebRequest.ContentType = "text/xml"; StreamWriter swRequestWriter = new StreamWriter(wrWebRequest.GetRequestStream()); swRequestWriter.Write(theXml); swRequestWriter.Close(); HttpWebResponse hwrWebResponse = (HttpWebResponse)wrWebRequest.GetResponse(); StreamReader srResponseReader = new StreamReader(hwrWebResponse.GetResponseStream()); responseResult = srResponseReader.ReadToEnd(); srResponseReader.Close(); Hope this helps.

              Jason Kataropoulos

              Excellence is not an act, but a habit!

              Aristotle

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups