Trying the below code to catch 404 error page but seems not to work, I mean 404 error doesn't display in response. The response only displays if the file exists (200 http code) !! but if the file doesn't exist (404) like in the below example, not msgbox displays, and the exception also doesn't display anything that's why I insert a msgbox in there.
If you try to take out these "111" from the "index.html" and run it, it'll display 200 http code well.
Using VS 2013, don't know, if it's a bug, or because I'm going through a proxy.
Will check it later from home for proxy as at work we're using proxy server.
Can anybody help !!
Try
Dim request As System.Net.HttpWebRequest = System.Net.HttpWebRequest.Create("https://www.cisco.com/c/en/us/support/index111.html")
Dim response As System.Net.HttpWebResponse = request.GetResponse
If response.StatusCode = Net.HttpStatusCode.OK Then
MsgBox("File exist on Server")
MsgBox(response.StatusCode) '200 HTTP CODE
'Trying to catch 404 error here
ElseIf response.StatusCode = Net.HttpStatusCode.NotFound Then
MsgBox("FILE NOT FOUND ON THE SERVER")
MsgBox(response.StatusCode)
Else
MsgBox("NO FILE on the server")
End If
Catch ex As Exception
'Msgbox("Error")
End Try
答案 0 :(得分:2)
request.GetResponse
will throw an exception when the request fails, for whatever reason.
There is a Response
object in the exception through, on which you can ask what the status code was. Check ex.Response.StatusCode
in your Catch
block.