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 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 }

猜你喜欢

转载自www.cnblogs.com/61007257Steven/p/10609350.html
今日推荐