asp.net how to check if a file exist on external server given web address
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/ima开发者_如何学Pythonge.jpg. I tried File.exist and that does not seem to work. Thanks for any help.
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
{
}
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...
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.
精彩评论