加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

Asp.net验证网络文件地址是否有效的方法

发布时间:2020-12-16 03:40:03 所属栏目:asp.Net 来源:网络整理
导读:1 public static bool CheckFileURLValidity( string URL) 2 { 3 bool IsValid = false ; 4 5 if (URL.Trim() != "" URL.Trim().Contains( " . " )) 6 { 7 try 8 { 9 Uri uri = new Uri(URL); 10 HttpWebRequest webRequest = HttpWebRequest.Create(uri) as
 1 public static bool CheckFileURLValidity(string URL)
 2 {
 3             bool IsValid = false;
 4 
 5             if (URL.Trim() != "" && URL.Trim().Contains("."))
 6             {
 7                 try
 8                 {
 9                     Uri uri = new Uri(URL);
10                     HttpWebRequest webRequest = HttpWebRequest.Create(uri) as HttpWebRequest;
11                     webRequest.Method = "HEAD";
12 
13                     HttpWebResponse webResponse = webRequest.GetResponse() as HttpWebResponse;
14                     if (webResponse.StatusCode == HttpStatusCode.OK)
15                     {
16                         IsValid = true;
17                     }
18                 }
19                 catch (WebException ex)
20                 {
21                     try
22                     {
23                         IsValid = ((ex.Response as HttpWebResponse).StatusCode != HttpStatusCode.NotFound);
24                     }
25                     catch
26                     {
27                         IsValid = (ex.Status == WebExceptionStatus.Success);
28                     }
29                 }
30                 catch (Exception ex)
31                 {
32 
33                 }
34             }
35 
36             return IsValid;
37 }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读