测试C#中是否存在图像

我正在为SiteScope编写诊断页面,我们需要测试的一个区域是,是否可以从Web服务器访问文件/媒体资产的连接。 我认为我可以这样做的一种方法是通过代码加载图像并测试以查看IIS状态消息是否为200。

所以基本上我应该能够在站点内导航到这样的文件夹:/media/1/image.jpg并查看它是否返回200 …如果不抛出exception。

我正在努力弄清楚如何编写这段代码。

任何帮助是极大的赞赏。

谢谢

只需使用HEAD。 如果您不需要,无需下载整个图像。 这里有一些样板代码。

 HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("url"); request.Method = "HEAD"; bool exists; try { request.GetResponse(); exists = true; } catch { exists = false; } 

您可能还想检查是否有一个OK状态代码(即HTTP 200),并且响应对象中的mime类型与您期望的相匹配。 你可以扩展,沿着,

 public bool doesImageExistRemotely(string uriToImage, string mimeType) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uriToImage); request.Method = "HEAD"; try { HttpWebResponse response = (HttpWebResponse)request.GetResponse(); if (response.StatusCode == HttpStatusCode.OK && response.ContentType == mimeType) { return true; } else { return false; } } catch { return false; } } 

我之前使用过类似的东西,但可能有更好的方法:

 try { HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://sofzh.miximages.com/c%23/picture.jpg"); request.Credentials = System.Net.CredentialCache.DefaultCredentials; HttpWebResponse response = (HttpWebResponse)request.GetResponse(); myImg.ImageUrl = "http://sofzh.miximages.com/c%23/picture.jpg"; } catch (Exception ex) { // image doesn't exist, set to default picture myImg.ImageUrl = "http://sofzh.miximages.com/c%23/default.jpg"; } 

你必须处理HTTPWebResponse对象,否则你会遇到问题……

  public bool DoesImageExistRemotely(string uriToImage) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uriToImage); request.Method = "HEAD"; try { using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { if (response.StatusCode == HttpStatusCode.OK) { return true; } else { return false; } } } catch (WebException) { return false; } catch { return false; } } 

如果您在请求期间收到exception,例如“远程服务器返回错误:(401)未经授权。”,

这可以通过添加以下行来解决

 request.Credentials = new NetworkCredential(username, password); 

如果图像存在于Intranet上,则从检查中添加问题和答案。

如果url像http:\ server.myImageSite.com一样存在,那么只有当imageSize> 0为真时答案才是假的。

  public static void GetPictureSize(string url, ref float width, ref float height, ref string err) { System.Net.HttpWebRequest wreq; System.Net.HttpWebResponse wresp; System.IO.Stream mystream; System.Drawing.Bitmap bmp; bmp = null; mystream = null; wresp = null; try { wreq = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(url); wreq.AllowWriteStreamBuffering = true; wresp = (HttpWebResponse)wreq.GetResponse(); if ((mystream = wresp.GetResponseStream()) != null) bmp = new System.Drawing.Bitmap(mystream); } catch (Exception er) { err = er.Message; return; } finally { if (mystream != null) mystream.Close(); if (wresp != null) wresp.Close(); } width = bmp.Width; height = bmp.Height; } public static bool ImageUrlExists(string url) { float width = 0; float height = 0; string err = null; GetPictureSize(url, ref width, ref height, ref err); return width > 0; } 

我会查看一个HttpWebRequest – 我认为之前的答案实际上会下载数据,而你应该能够在没有来自HttpWebRequest的数据的情况下获得响应。

http://msdn.microsoft.com/en-us/library/456dfw4f.aspx直到第4步应该可以解决问题。 如果需要,HttpWebResponse上还有其他字段用于获取数字代码……

杰克