1

我正在尝试使用对 DownloadFile 的 Net.WebClient 调用来下载文件

Using client As New Net.WebClient()   
  Try
    client.DownloadFile(PDFURL, FullPDFFilePath)

然后我捕获异常并检查消息中是否存在 403、404 或 500 错误(我们调用的系统最常见的类型。

 Catch ex as exception
   If exceptionMessage.Contains("(403)") Then 'Forbidden
     LogInformation("403 returned on download for " + CRPOrderNum, "DownloadLabels")

   ElseIf exceptionMessage.Contains("(404)") Then 'Not Found
     LogInformation("404 returned on download for " + CRPOrderNum, "DownloadLabels")

   else
     'blah blah
   end if
 finally
 end try

有没有一种礼貌的方式可以让我请求文件而不是调用 DownloadFile 并处理异常?

提前致谢。

4

1 回答 1

2

“礼貌”的方式是发送 HEAD 请求。不幸的是,WebClient 本身不支持此功能,因此您要么必须自己动手,要么使用 HttpWebRequest 代替。

于 2011-05-11T15:36:54.203 回答