How to use HTTP protocol to get documents ?

Discussion in 'ASP General' started by fniles, Mar 19, 2008.

  1. fniles

    fniles Guest

    I need to send a request using HTTP Protocol and it will return to me some
    replies, which can be an XML or HTML file. In ASP, how can I GET/POST
    documents using the HTTP protocol ?
    Thank you.
    fniles, Mar 19, 2008
    #1
    1. Advertising

  2. "fniles" <> wrote in message
    news:%...
    > I need to send a request using HTTP Protocol and it will return to me some
    > replies, which can be an XML or HTML file. In ASP, how can I GET/POST
    > documents using the HTTP protocol ?



    Function GetText(sURL)

    Dim oXHR : Set oXHR = Server.CreateObject("MSXML2.ServerXMLHTTP.3.0")
    oXHR.open "GET", sURL, False
    oXHR.send
    If oXHR.status = 200 Then
    GetText = oXHR.responseText
    Else
    GetText = Null
    ' Or if you prefer throw an error here
    End If

    End Function

    This simply returns a string containing the response from the server.

    You could create a similar GetXML function that an XML DOM. By changing
    responseText to responseXML. It will only do so if the server responds with
    a content type header indicating xml.

    Function PostText(sURL, vntData, sContentType)

    Dim oXHR : Set oXHR = Server.CreateObject("MSXML2.ServerXMLHTTP.3.0")
    oXHR.open "POST", sURL, False
    If Not IsNull(sContentType) Then oXHR.setRequestHeader "ContentType",
    sContentType
    oXHR.send vntData
    If oXHR.status = 200 Then
    PostText= oXHR.responseText
    Else
    PostText= Null
    ' Or if you prefer throw an error here
    End If

    End Function

    Again you can mod to create a PostXML version.


    --
    Anthony Jones - MVP ASP/ASP.NET
    Anthony Jones, Mar 20, 2008
    #2
    1. Advertising

  3. fniles

    fniles Guest

    Thank you.
    The MSXML2 only works with XML file, right ?
    How about if I need to get other file other than XML file ?

    Thanks

    "Anthony Jones" <> wrote in message
    news:%...
    > "fniles" <> wrote in message
    > news:%...
    >> I need to send a request using HTTP Protocol and it will return to me
    >> some
    >> replies, which can be an XML or HTML file. In ASP, how can I GET/POST
    >> documents using the HTTP protocol ?

    >
    >
    > Function GetText(sURL)
    >
    > Dim oXHR : Set oXHR = Server.CreateObject("MSXML2.ServerXMLHTTP.3.0")
    > oXHR.open "GET", sURL, False
    > oXHR.send
    > If oXHR.status = 200 Then
    > GetText = oXHR.responseText
    > Else
    > GetText = Null
    > ' Or if you prefer throw an error here
    > End If
    >
    > End Function
    >
    > This simply returns a string containing the response from the server.
    >
    > You could create a similar GetXML function that an XML DOM. By changing
    > responseText to responseXML. It will only do so if the server responds
    > with
    > a content type header indicating xml.
    >
    > Function PostText(sURL, vntData, sContentType)
    >
    > Dim oXHR : Set oXHR = Server.CreateObject("MSXML2.ServerXMLHTTP.3.0")
    > oXHR.open "POST", sURL, False
    > If Not IsNull(sContentType) Then oXHR.setRequestHeader "ContentType",
    > sContentType
    > oXHR.send vntData
    > If oXHR.status = 200 Then
    > PostText= oXHR.responseText
    > Else
    > PostText= Null
    > ' Or if you prefer throw an error here
    > End If
    >
    > End Function
    >
    > Again you can mod to create a PostXML version.
    >
    >
    > --
    > Anthony Jones - MVP ASP/ASP.NET
    >
    >
    fniles, Mar 20, 2008
    #3
  4. "fniles" <> wrote in message
    news:...
    > Thank you.
    > The MSXML2 only works with XML file, right ?
    > How about if I need to get other file other than XML file ?
    >


    No it will fetch any type of file.

    Use responseText to retrieve a text based resource such as html.

    Use responseBody to get an array of bytes to get a binary resource.

    Use responseStream to get an implementation of IStream to pull large
    resources

    XMLHTTP only gets XML when 1) the response type is XML and it will build a
    XML DOM which it exposes as responseXML and 2) when posting you supply an
    XML DOM to the send method in which case it automatically adds the text/xml
    content type and streams the xml from the DOM.



    --
    Anthony Jones - MVP ASP/ASP.NET
    Anthony Jones, Mar 20, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Helen
    Replies:
    0
    Views:
    651
    Helen
    Nov 28, 2003
  2. Replies:
    1
    Views:
    469
    Juan T. Llibre
    Oct 18, 2006
  3. serge calderara

    HTTP GET vs HTTP POST protocol

    serge calderara, Nov 4, 2005, in forum: ASP .Net Web Services
    Replies:
    1
    Views:
    561
  4. Matt
    Replies:
    1
    Views:
    118
    Ray at
    Jan 11, 2004
  5. Matt
    Replies:
    1
    Views:
    141
    Brynn
    Jan 18, 2004
Loading...

Share This Page