1

in my asp.net application, I would like to check to see if a file exist on an external server given the file address such as www.example.com/image.jpg. I tried File.exist and that does not seem to work. Thanks for any help.

3 Answers 3

4

You could use:

 bool exist = false;
 try
 {
      HttpWebRequest request = (HttpWebRequest)System.Net.WebRequest.Create("http://www.example.com/image.jpg");
      using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
      {
           exist = response.StatusCode == HttpStatusCode.OK;
      }
 }
 catch
 {
 }
Sign up to request clarification or add additional context in comments.

Comments

1

try

((HttpWebResponse)((HttpWebRequest) WebRequest.Create ("http://www.example.com/image.jpg")).GetResponse ()).StatusCode  == HttpStatusCode.OK

IF the above evaluates to true then the file exists...

Comments

0

One obvious answer I can think of is to issue a request for the resource, and then study the response code sent back to your application. The article found at http://madskristensen.net/post/Get-the-HTTP-status-code-from-a-URL.aspx has a concise example of how to do so.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.