windows C# -如果无法连接到远程服务器,则抛出异常

kknvjkwl  于 2023-03-31  发布在  Windows
关注(0)|答案(2)|浏览(186)

我试图在我的方法中的函数失败时抛出一个异常,到目前为止,我的代码如下:

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();

                if (response.LastModified > sourceFile.LastWriteTime)
                {

                    Download_Click(sender, e);

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

e4eetjau1#

根据HttpWebRequest文档,如果请求超时或处理时发生其他错误,则从GetResponse抛出WebException
你应该能够在你的代码中捕捉到这一点。

相关问题