通过URL判断文件是否有存在

直接帖代码吧!有什么问题可以M我!
调用的命名空间是:using System.Net;

string URL = HttpUtility.UrlDecode("http://img.shengri.com/Resource/pic/21/530.gif");
   HttpWebRequest myWebRequest = (HttpWebRequest)HttpWebRequest.Create(URL);
   myWebRequest.AllowAutoRedirect = false;
   try
   {
    HttpWebResponse myWebResponse = (HttpWebResponse)myWebRequest.GetResponse();
    if (myWebResponse.StatusCode == HttpStatusCode.OK)  
    {
     Response.Write("True");
    }
    else
    {  
     Response.Write("False");
    }
   }
   catch(WebException webExcp)
   {
    WebExceptionStatus status =  webExcp.Status;
    if (status == WebExceptionStatus.ProtocolError)
    {
     HttpWebResponse httpResponse = (HttpWebResponse)webExcp.Response;
     if((int)httpResponse.StatusCode == 404)
     {
      Response.Write("The server returned protocol error:");
      Response.Write((int)httpResponse.StatusCode + " - " + httpResponse.StatusCode);
     }
    }
   }
posted @ 2007-05-31 10:44  亦续缘  阅读(559)  评论(0编辑  收藏  举报