Sample code for 30+ languages & platforms
Xbase++

HTTP GET -- Read Response from Stream

See more REST Examples

Demonstrates how to send an HTTP GET for a web page, and then read the response from a Stream. The purpose is to fulfill a situation such as the following: "I have a URL and just want the <head> </head> portion of the HTML. This would avoid having to download a potentially enormous web page just to get at header information, such as the <styles>."

Chilkat Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oRest
LOCAL oUrl
LOCAL nBAutoReconnect
LOCAL nResponseStatusCode
LOCAL cErrResponse
LOCAL oBodyStream
LOCAL oReadResponseBodyTask
LOCAL oSbBody
LOCAL nExitLoop
LOCAL cBodyText
LOCAL nMaxWaitMs

nSuccess := 0

//  This example requires the Chilkat API to have been previously unlocked.
//  See Global Unlock Sample for sample code.

oRest := CreateObject("Chilkat.Rest")

//  In this example, we'll get the web page at http://www.nytimes.com/pages/business/index.html
//  The domain is "www.nytimes.com", and the path is "/pages/business/index.html"

//  If we have only the full URL to begin with, it can be loaded into the Chilkat URL object to
//  access the parts:
oUrl := CreateObject("Chilkat.Url")
oUrl:ParseUrl("http://www.nytimes.com/pages/business/index.html")

nBAutoReconnect := 1
nSuccess := oRest:Connect(oUrl:Host, oUrl:Port, oUrl:Ssl, nBAutoReconnect)

//  Send the GET request (This sends the GET request, but does not read the response.)
nSuccess := oRest:SendReqNoBody("GET", oUrl:Path)
IF (nSuccess != 1)
    ? oRest:LastErrorText
    oRest:destroy()
    oUrl:destroy()
    RETURN
ENDIF

//  Read the response header.
nResponseStatusCode := oRest:ReadResponseHeader()
IF (nResponseStatusCode < 0)
    ? oRest:LastErrorText
    oRest:destroy()
    oUrl:destroy()
    RETURN
ENDIF

? "Response status code = " + Str(nResponseStatusCode)

//  We expect a 200 response status.
IF (nResponseStatusCode != 200)

    //  If the response status code is not 200, we could check for a redirect status code and 
    //  then follow it, read the entire response (as shown here), or just call rest.Disconnect
    cErrResponse := oRest:ReadRespBodyString()
    IF (oRest:LastMethodSuccess != 1)
        ? oRest:LastErrorText
    ELSE
        ? cErrResponse
    ENDIF

    oRest:destroy()
    oUrl:destroy()
    RETURN
ENDIF

oBodyStream := CreateObject("Chilkat.Stream")
//  Set a 10 second read timeout for the stream. 
//  (Give up if no data arrives within 10 seconds after calling a read method.)
oBodyStream:ReadTimeoutMs := 10000

//  Create a background thread task to read the response body (which feeds
//  it to the bodyStream object.)
oReadResponseBodyTask := oRest:ReadRespBodyStreamAsync(oBodyStream, 1)

//  Start the task. 
nSuccess := oReadResponseBodyTask:Run()

//  Read the HTTP response body until the "</head>" is seen, or until
//  the end-of-stream is reached.

oSbBody := CreateObject("Chilkat.StringBuilder")
nExitLoop := 0
DO WHILE .NOT. nExitLoop .AND. (oBodyStream:EndOfStream != 1)

    cBodyText := oBodyStream:ReadString()
    IF (oBodyStream:LastMethodSuccess == 1)
        oSbBody:Append(cBodyText)
        IF (oSbBody:Contains("</head>", 0))
            nExitLoop := 1
        ENDIF

    ELSE
        nExitLoop := 1
    ENDIF

ENDDO

//  Cancel the remainder of the task...
oReadResponseBodyTask:Cancel()

oReadResponseBodyTask:destroy()

//  Ensure we're disconnected from the server.
nMaxWaitMs := 50
oRest:Disconnect(nMaxWaitMs)

? "----"
? oSbBody:GetAsString()
? "----"
? "Successfully received the body up to the desired point."

oRest:destroy()
oUrl:destroy()
oBodyStream:destroy()
oSbBody:destroy()