Contents

Introduction

This package contains a HTTP client class, and related other classes.

The central class is http.HttpClient. Request objects are created (http.HttpRequest), passed to the client, and responses received (http.HttpResponse).

This is illustrated in the following simple program :-

Download http.icn

import net, http, io, lang

procedure main(a)
   local url, hreq, hc, hres, out

   a := a[1] | stop("URL expected")
   url := URL(a) | stop("Invalid url: " || a)
   url.scheme == ("http"|"https") | stop("Inappropriate url scheme: " || url.scheme)

   # A stream for the resulting data
   out := StringStream()

   # A request object
   hreq := HttpRequest().
      set_url(url).
      set_output_stream(out)

   hc := HttpClient()

   # Get the response object
   hres := hc.retrieve(hreq) | stop(&why)

   write("OK ", *out.str(), " bytes\n", to_string(hres, 3))
end

This produces output similar to the following :-

$ ./http http://www.google.com
OK 10611 bytes
object http.HttpResponse#2(
   url=object net.URL#3(http://www.google.co.uk/?gfe_rd=cr&ei=jHHJV421IYbDaJCmodgJ)
   status="HTTP/1.1 200 OK"
   previous_response=
      object http.HttpResponse#1(
         url=object net.URL#1(http://www.google.com/)
         status="HTTP/1.1 302 Found"
         previous_response=&null
         headers=
            object datastruct.SortTable#3(
               "Cache-Control"->list#111["private"]
               "Content-Length"->list#114["259"]
               "Content-Type"->list#112["text/html; charset=UTF-8"]
               "Date"->list#115["Fri, 02 Sep 2016 12:33:16 GMT"]
               "Location"->list#113["http://www.google.co.uk/?gfe_rd=cr&ei=jHHJV421IYbDaJCmodgJ"]
            )
      )
   headers=
      object datastruct.SortTable#4(
         "Cache-Control"->list#133["private, max-age=0"]
         "Content-Encoding"->list#136["gzip"]
         "Content-Length"->list#138["4515"]
         "Content-Type"->list#134["text/html; charset=ISO-8859-1"]
         "Date"->list#131["Fri, 02 Sep 2016 12:33:16 GMT"]
         "Expires"->list#132["-1"]
         "P3P"->list#135["CP=\"This is not a P3P policy! See https://www.google.com/support/accounts/answer/151657?hl=en for more info.\""]
         "Server"->list#137["gws"]
         "Set-Cookie"->list#141["NID=85=aerKk58-jVx9pLIo-Yq-W6pRf28JDj8mmP1OvSPg19CMvjDIqYtGIDqiG1f34v395bbyP2Cqy8mA9qS4sFBKqeexxx-V0yeaxhluXCJOxssVFRBwzyfoddmIZ3BY2YfH; expires=Sat, 04-Mar-2017 12:33:16 GMT; path=/; domain=.google.co.uk; HttpOnly"]
         "X-Frame-Options"->list#140["SAMEORIGIN"]
         "X-XSS-Protection"->list#139["1; mode=block"]
      )
)

Example programs

The examples directory contains two programs which use HttpClient. The first is geturl, which is a command-line tool to retrieve URLs. The second is browser, which is a simple web browser. It makes use of ipl.browser.Browser, which uses multiple asynchronous HttpClients (each running in an io.Task) to download a particular web page and its images.

Contents