最近要做一个检查远程文件是否存在的功能,但是有个别的检查就出错了,代码如下,请求解决
public static bool RemoteFileExists(string fileUrl) { bool result = false;//下载结果 WebRequest req = null; WebResponse res = null; try { req = WebRequest.Create(fileUrl); req.PreAuthenticate = false; req.Method = "HEAD"; res = req.GetResponse(); result = ( res != null); } catch (Exception ex) { result = false; } finally { if (res != null) { res.Close(); res = null; } if (req != null) { req.Abort(); req = null; } } return result; }
我使用了一些解决方法,但是仍然无效,如下
<system.net> <settings> <httpWebRequest useUnsafeHeaderParsing="true"/> </settings> </system.net>