开发者

C# - Throw an exception if can't connect to remove server

开发者 https://www.devze.com 2023-03-15 07:00 出处:网络
I am trying to get an exception thrown if the function in my method fails, my code is as follows so far:

I am trying to get an exception thrown if the function in my method fails, my code is as follows so far:

 if (sourceFile.Exists)
            // Would be nice to add ticker / spinner, while the file header on the remote server is being read!!
            {
                var request = (HttpWebRequest)WebRequest.Create(@"http://google.com/test.zip");
                request.Method = "HEAD";
                var response = (HttpWebResponse)request.GetResponse();

            开发者_StackOverflow社区    if (response.LastModified > sourceFile.LastWriteTime)
                {

                    Download_Click(sender, e);

                    // use response.GetStream() to download the file.
                }


According to the HttpWebRequest docs, a WebException is thrown from GetResponse if the request times out or another error occurs while processing it.

You should be able to catch that in your code.


try/catch HttpException

http://msdn.microsoft.com/en-us/library/system.web.httpexception.aspx

0

精彩评论

暂无评论...
验证码 换一张
取 消