I have a asp.net mvc route that is taking a url and doing a simple get and return the status code from the request.
<AcceptVerbs(HttpVerbs.Post)> _
Public Function ValidateUrlStatusCode(ByVal url As String) As ActionResult
Dim code As Integer = 0
Try
Dim request As HttpWebRequest = CType(WebRequest.Create(url), HttpWebRequest)
request.Method = "GET"
request.AllowAutoRedirect = True
Using response As HttpWebResponse = request.GetResponse
response.Close()
code = response.StatusCode
End Using
Catch ex As Exception
code = HttpStatusCode.InternalServerError
End Try
Return Content(code, "text/plain")
End Function
Now if I use firefox (using Firebug) and go to the url http://www.facebook.com/blah.html, I get the expected 404 returned. However if I use my application to call the mvc route via an ajax call, I get 200. If I set the request object’s AllowAutoRedirect to false, I get 302. I never get a 404. I am verifying this once again through Firebug. Can anyone point out what I am doing wrong?
Thanks!
If you use FaceBook make sure you set the user agent or the site will redirect you to a standard HTML page explaining you to do so (thus the 200 status code):
Also when a status code different than 200 is returned from the HttpWebRequest an exception will be thrown, and more specifically a WebException. So you need to trap this WebException and inside the Response property containing the HttpWebResponse you will find the 404 StatusCode.
Also I would probably use a WebClient to simplify the code:
And on the client: