asp-classicserverxmlhttp

How do I use MSXML2.ServerXMLHTTP to grab data from another site?


We have the folowing link: http://mvp.sos.state.ga.us/

Rather than create a db to replicate information that MVP page, we would like to use our own form, and then behind the scenes, send information to the site above to get results back using component called MSXML2.ServerXMLHTTP.

Unfortunately, I know nothing about this component or how to use it.

Would someone be kind enough to please give me pointers on how use our own ... to send information to the site above and get results back to our form?

We are basically trying to get users to enter first initial, lastname, county, date of birth.

Thanks


Solution

  • You can use this component for http-requests like "POST", "GET", "DELETE" etc.

    To create the object:

    <%
        Set objXML = Server.CreateObject("MSXML2.ServerXMLHTTP")
    %>
    

    To send data using method "GET":

    <%
        objXML.Open "GET", "http://mvp.sos.state.ga.us/?some=querystring", false 
        objXML.Send ""
        Response.Write objXML.responseText
    %>
    

    Note that Open method has 3 parameters: HTTP method, URL, asynchronous call.

    Note that Send method on a "GET" ignores its parameter. (In this case we are passing parameters via the URL.)

    To send data using method "POST":

    <%
        objXML.Open "POST", "http://mvp.sos.state.ga.us/", false 
        objXML.Send "username=htbasaran&password=somepassword"
        Response.Write objXML.responseText
    %>
    

    Note for "POST" that Send method passes parameters in key-value pairs format like: key1=value1&key2=value2&so=on... or any other data like XML, JSON, etc.)

    These are the basics of this component. If you need more information, you can check microsoft's docs page out.

    An example code for getting form values and sending them using xmlhttp post method.

    <%
        ' getting form values
        my_uname = Request.Form("username")
        my_pword = Request.Form("password")
    
        ' creating object
        Set objXML = Server.CreateObject("MSXML2.ServerXMLHTTP")
    
        ' sending variables to an external site
        objXML.Open "POST", "http://www.sitename.com/login.asp", false
        objXML.Send "username=" & my_uname & "&password=" & my_pword
    
        ' Assuming that successful login will return response "Ok"
        ' writing the result to the client.
        if objXML.responseText="Ok" then
            Response.Write "Login Successful!"
        else
            Response.Write "Login Failed!"
        end if
    %>