how to check if a remote file exists
-
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
-
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
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;
-
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
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.
Excellence is not an act, but a habit!
Aristotle
-
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;
-
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.
Excellence is not an act, but a habit!
Aristotle
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.
-
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.
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.Excellence is not an act, but a habit!
Aristotle