MSXML2.XMLHTTP Request to validate entered URL in ASP Classic

我们两清 提交于 2019-12-11 09:26:10

问题


Thanks in advance for any help received.

I want to allow our client to enter a URL into a text field which then checks whether the URL exists and works.

There are 3 possible outcomes I want to check for: A status of 200 - OK, A status of 500 - Server Error, Or a status of 404 - page not found.

When executing the following code in ASP classic I get a status code of 12007 when I should be getting 404. Is this because it can't find a webserver to return a code of 404?

Function CheckURL(vURL)
    ON ERROR RESUME NEXT
    Set oXML=Server.CreateObject("MSXML2.XMLHTTP") : oXML.Open "POST",vURL,false : oXML.Send()
    CheckURL = oXML.status
    Set oXML = nothing
End Function

Or is something amiss here. What status codes am I likely to see other than the standard mentioned above.


回答1:


The 12007 is a Windows HTTP error which means name hasn't been resolved. You can't get a 200, 404, 500 or any such thing if the host name can't be resolved to an IP address or a connection can't be established to that IP address. In these cases you will get error codes in the 12000s range which aren't HTTP status codes but are windows exception numbers.

See this list for a list of these exception numbers.

BTW, XMLHTTP is not a safe item object to use in ASP. Also why are you using a POST? This is the code I would use:-

Function CheckURL(vURL)
    On Error Resume Next
    Set xhr = CreateObject("MSXML2.ServerXMLHTTP.3.0")
    xhr.Open "HEAD", vURL, false
    xhr.Send
    CheckURL = xhr.status
End Function

Using HEAD allows you test the URL without actually downloading a potentially large entity body.



来源:https://stackoverflow.com/questions/1542790/msxml2-xmlhttp-request-to-validate-entered-url-in-asp-classic

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!