C # Create a connection through HttpWebRequest to get the file size determine whether a file of the HTTP type exists

Source: Internet
Author: User

1  Public voidDisposereportdata (synfileinfo Item)2         {3Mreport report =Newmreport ();4Report. File name =item. DocName;5Report. URL =item. Downpath;6HttpWebRequest request =NULL;7HttpWebResponse response =NULL;8             Try9             {TenRequest = WebRequest.Create (item. Downpath) asHttpWebRequest; OneRequest. Proxy =NULL;//do not use proxies. The default proxy in NET4.0 is turned on ARequest. KeepAlive =false;//do not establish a persistent connection -Request. Timeout = the;//time-out for connection URLs -Request. Readwritetimeout = the;//time-out for reading URL content theResponse = Request. GetResponse () asHttpWebResponse; -                 LongLength =Response. ContentLength; -                 stringSize = fileoperate.getautosizestring (length,2); -report. Size =size; +Report. Connection Status ="effective"; -             } +             Catch(WebException webEx) A             { atreport. Size ="0 MB"; -Report. Connection Status ="Invalid"; -Console.WriteLine ("The requested file does not exist!"+webex.message); -             } -             finally -             { in                 Lock(Configreader.reportdata) -                 { to CONFIGREADER.REPORTDATA.ADD (report); +                 } -                 if(Response! =NULL) the                 { *Response. Close ();//Close Connection $                 }Panax Notoginseng                 if(Request! =NULL) -                 { theRequest. Abort ();//Abort Request +                 } ARequest =NULL; theResponse =NULL; +System.GC.Collect ();//force garbage Collection, and release resources -  $             } $}
View Code

WebRequest Multithreading Timeout problem

System.Net.ServicePointManager.DefaultConnectionLimit = ; the maximum number of concurrent,. NET default concurrency is only 2

C # Create a connection through HttpWebRequest to get the file size determine whether a file of the HTTP type exists

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.