首页 > 代码库 > C# 通过HttpWebRequest 创建连接获取文件大小 判断Http类型的文件是否存在

C# 通过HttpWebRequest 创建连接获取文件大小 判断Http类型的文件是否存在

 1 public void DisposeReportData(SynFileInfo item) 2         { 3             MReport report = new MReport(); 4             report.文件名 = item.DocName; 5             report.URL = item.DownPath; 6             HttpWebRequest request = null; 7             HttpWebResponse response = null; 8             try 9             {10                 request = WebRequest.Create(item.DownPath) as HttpWebRequest;11                 request.Proxy = null;//不使用代理 .NET4.0中的默认代理是开启的12                 request.KeepAlive = false;//不建立持久性连接13                 request.Timeout = 5000;//连接网址的超时时间14                 request.ReadWriteTimeout = 5000;//读取网址内容的超时时间      15                 response = request.GetResponse() as HttpWebResponse;16                 long length = response.ContentLength;17                 string size = FileOperate.GetAutoSizeString(length, 2);18                 report.大小 = size;19                 report.连接状态 = "有 效";20             }21             catch (WebException webEx)22             {23                 report.大小 = "0 MB";24                 report.连接状态 = "无 效";25                 Console.WriteLine("请求的文件不存在!" + webEx.Message);26             }27             finally28             {29                 lock (ConfigReader.ReportData)30                 {31                     ConfigReader.ReportData.Add(report);32                 }33                 if (response != null)34                 {35                     response.Close();//关闭连接36                 }37                 if (request != null)38                 {39                     request.Abort();//中止请求40                 }41                 request = null;42                 response = null;43                 System.GC.Collect();//强制垃圾回收,并释放资源44 45             }46         }
View Code

 WebRequest多线程 超时问题

System.Net.ServicePointManager.DefaultConnectionLimit = 20;//最大并发数,.net默认并发只有2个

 

C# 通过HttpWebRequest 创建连接获取文件大小 判断Http类型的文件是否存在