我试图在我的方法中的函数失败时抛出一个异常,到目前为止,我的代码如下:
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.
}
2条答案
按热度按时间e4eetjau1#
根据HttpWebRequest文档,如果请求超时或处理时发生其他错误,则从
GetResponse
抛出WebException。你应该能够在你的代码中捕捉到这一点。
idfiyjo82#
try/catch HttpException
http://msdn.microsoft.com/en-us/library/system.web.httpexception.aspx