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):
request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:2.0) Gecko/20100101 Firefox/4.0";
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:
Public Function ValidateUrlStatusCode(url As String) As ActionResult
Dim code = 0
Try
Using client = New WebClient()
client.Headers(HttpRequestHeader.UserAgent) = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:2.0) Gecko/20100101 Firefox/4.0"
Dim response = client.DownloadString(url)
End Using
Catch ex As WebException
Dim httpResponse = TryCast(ex.Response, HttpWebResponse)
If httpResponse IsNot Nothing Then
code = CInt(httpResponse.StatusCode)
End If
End Try
Return Content(code.ToString(), "text/plain")
End Function
And on the client:
<script type="text/javascript">
$.ajax({
url: '@Url.Action("ValidateUrlStatusCode")',
type: 'POST',
data: { url: 'http://www.facebook.com/blah.html' },
success: function (result) {
alert(result);
}
});
</script>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.