Xbasic

HTTP_GET Function

Syntax

Result as P = http_get(url as C [,cookie as C [,port as N [,timeout as N [,show_before_send as L [,validate_ssl_cert as L [,SSLCipherList as C]]]]]])

Arguments

urlCharacter

The URL of the page to retrieve.

cookieCharacter

Default = "". Cookie data. Limited to 8 MB (megabytes.)

portNumeric

Default = -1. The port to use.

timeoutNumeric

Default = 8000 milliseconds. The number of milliseconds to wait before timing out.

show_before_sendLogical

Default = .F.. When .T., displays the request before being sent. Useful for debugging.

validate_ssl_certLogical

Default = .T.. If the specified URL starts with "https://", this flag controls whether or not the certificate offered by the server will be validated. If set to .T., the request will always fail. If set to .F., the request may succeed, but the server's identity is not guaranteed.

SSLCipherListCharacter

SSL Cipher List.

Returns

ResultPointer

A dot variable containing the server's response.

error_textCharacter

The error message, if any. If no error occurs, error_text will be empty.

error_codeNumeric

The error number, if any. If no error occurs, error_code will be 0.

headersCharacter

Response headers. If error_code is 0, the result will contain a dot variable, parsed_headers, with a property representing each response header.

bodyCharacter

Response body.

parsed_headersPointer

Contains all the headers in the response split out into individual properties. If an error occurred when trying to communicate with the server, parsed_headers will be empty. parsed_headers will always contain the following properties in addition to the headers in the response:

http_versionCharacter

The HTTP version used.

reason_phraseCharacter

A description of the status code.

status_codeNumeric

The response status code. See Status Codes for a list of status codes.

Description

Use the HTTP method GET to retrieve the specified URL.

Discussion

The HTTP_GET() function downloads from a URL using HTTP/1.1 GET. The function supports cookies and returns a pointer with the parsed response from the server. If error_code is "0", parsed_response will contain the headers in the result:

result = http_get("www.alphasoftware.com")
? result.parsed_headers
= AcceptRanges = "none"
CacheControl = "private"
Connection = "close"
ContentLength = "47222"
ContentType = "text/html"
Date = "Thu, 22 Dec 2016 23:22:53 GMT"
http_version = "HTTP/1.1"
reason_phrase = "OK"
Server = "Microsoft-IIS/8.5"
SetCookie = "ASPSESSIONIDQSTBASCQ=BEABFDNBKOJHODCBLBBPAHIG; path=/"
status_code = 200
XPoweredBy = "ASP.NET"

Status code 200 indicates that the page exists. Status code 404 indicates that it does not. Other status codes you may encounter, along with the meanings of each, are documented in Status Codes.

 Handling Redirects

When the server responds with a 30* code, HTTP_GET() does not automatically use the new URL. The developer needs to examine result.parsed_headers.status_code, then if appropriate, try the URL provided in result.parsed_headers.location. For example:

dim url as C = "support.alphasoftware.com"
dim result as P
result = http_fetch(url)
if (result.parsed_headers.status_code = 302) then
    ' Page has been redirected; fetch page from
    ' new URL specified in parsed_headers.location:
    result = http_get(result.parsed_headers.Location)

    if (result.parsed_headers.status_code <> 200) then
        ' Request returned with an error.
        ui_msg_box("Error","The request returned with the following status code: " + result.parsed_headers.status_code,UI_STOP_SYMBOL)
        end
    else
        '...
    end if
end if

 HTTP GET vs HTTP POST

HTTP GET is different from HTTP POST in two significant ways. Some devices may restrict the total length of a URL to 128 characters, which may trim the arguments appended by a GET command. There is no such restriction with POST commands. You may save (bookmark) and refresh URLs formatted through HTTP GET. The arguments sent by HTTP POST are not saved in a bookmark.

 Example: Retrieve a Page

This example retrieves the Alpha Software home page:

dim alpha as P
alpha = http_get("http://www.alphasoftware.com/")
if (alpha.parsed_headers.status_code = 200) then
    a5_show_html(alpha.body)
end if

 Example: Search Google

In the example below, HTTP_GET is used to perform a search using www.google.com:

dim search_term as C
dim google as P
search_term = ui_get_text("Search for...","What are you looking for?")
google = http_get("http://www.google.com/search?hl=en&ie=UTF-8&oe=UTF-8&q=" + urlencode(search_term))

if (google.parsed_headers.status_code = 200) then
    a5_show_html(google.body)
end if

 Example: RSS Feed

In this example, RSS text is retrieved from Slashdot.org using HTTP_GET:

dim rss as P
rss = http_get("http://slashdot.org/index.rss")
if (rss.parsed_headers.status_code <> 200) then
    ui_msg_box("Error","The request returned with the following status code: " + rss.parsed_headers.status_code,UI_STOP_SYMBOL)
    end
end if

if .not. eval_valid("rss.body")
    ui_msg_box("Error","The RSS file was not downloaded from the server.",UI_STOP_SYMBOL)
else
    dim num_items as N
    num_items = count_textblocks(rss.body,"")
    dim item[num_items] as P
    dim i as N
    dim this_item as C
    for i = 1 to num_items
        this_item = extract_string(rss.body,"",i,.t.)
        item[i].title = extract_string(this_item,"","")
        item[i].link = extract_string(this_item,"","")
        item[i].description = extract_string(this_item, "", "")
        item[i].creator = extract_string(this_item, "", "")
        item[i].subject = extract_string(this_item, "", "")
        item[i].date = extract_string(this_item, "", "")
        item[i].section = extract_string(this_item, "", "")
        item[i].department = extract_string(this_item, "", "")
        item[i].comments = extract_string(this_item, "", "")
        item[i].hitparade = extract_string(this_item, "", "")
    next i
    'now do something with this, like put it into a table or build an output file.
    a5_show_variable(property_to_string(item))
end if

See Also