Politely checking to see if a file exists before downloading
I'm trying to download files using the Net.WebClient call to DownloadFile
Using client As New Net.WebClient()
Try
client.DownloadFile(PDFURL, FullPDFFilePath)
I then catch the exception and check the message for 403, 404, or 500 errors (the most common type for the system we are calling into.
Catch ex as exception
If exceptionMessage.Contains("(403)") Then 'Forbidden
LogInformation("403 returned on download for " + CRPOrderNum, "DownloadLabels")
ElseIf ex开发者_Python百科ceptionMessage.Contains("(404)") Then 'Not Found
LogInformation("404 returned on download for " + CRPOrderNum, "DownloadLabels")
else
'blah blah
end if
finally
end try
Is there a polite way I can ask for the file instead of calling DownloadFile, and handling the exception ?
Thanks in advance.
The "polite" way would be to send a HEAD request. Unfortunately WebClient doesn't support this natively so you'd either have to roll your own or use HttpWebRequest instead.
精彩评论