Sometimes, we need to know whether a network resource is valid and available, but we do not want to open or download this resource, because this resource may be large (such as a file to be downloaded)
An effective method is to use the status code returned by the HTTP header to determine the resource availability. We usually use get and post for Web access. Here we use the head method.
Static Bool Iswebresourceavailable ( String Webresourceaddress ){ Try { Httpwebrequest req = ( Httpwebrequest ) Webrequest . Createdefault ( New Uri ( Webresourceaddress )); REQ . Method = "Head" ; REQ . Timeout = 1000 ; Httpwebresponse res = ( Httpwebresponse ) REQ . Getresponse (); Return ( Res . Statuscode = Httpstatuscode . OK );} Catch ( Webexception Wex ){ System . Diagnostics . Trace . Write ( Wex . Message ); Return False ;}}
In the preceding function, webresourceaddress is the resource address,
If the resource is available, true is returned; otherwise, false is returned.