:Class HTTP ⍝ Provides a base class for sending HTTP requests and receiving HTTP responses from a resource identified by a URI. ⍝ List of Methods: ⍝ GET = Sends a GET request to the specified Uri and returns the response as Bytes in an asynchronous operation. ⍝ .Net assemblies: :Using :Using System,System.dll :Using System.Net,System.dll :Using System.Net.Http,System.Net.Http.dll (⎕IO ⎕ML ⎕WX)←1 3 3 ⍝ System variables :Field Public _Uri :Field Public _TimeOut←5000 ⍝ Default Time-out value in milliseconds ∇ Init0 :Access Public :Implements Constructor _Uri←⎕NULL ∇ ∇ r←BuildUri url ⍝ Build a Uniform Resource Identifier (URI) ⍝ url = IP Address as numbers (192 168 1 1) or characters ('192.168.1.1' or 'www.mysite.com') ⍝ r[1] = 1 for Success, 0 for Failure ⍝ r[2] = URI if Successfull, Literal Error if Failure :Trap 0 :If 0=↑1↑0⍴url ⍝ Numbers ? url←(⎕NEW IPAddress(⊂,url)).ToString _Uri←(⎕NEW UriBuilder(⊂,url)).Uri r←1 _Uri :Else ⍝ Characters _Uri←(⎕NEW UriBuilder(⊂,url)).Uri r←1 _Uri :EndIf :Else ⍝ Show the error. :If 90=⎕EN ⍝ .Net Error r←0('URI: ',⎕EXCEPTION.GetBaseException.Message) :Else ⍝ APL Error r←0(1⊃⎕DM),': ',{(' '=1↑⍵)↓((1↓a,0)∨a←' '≠⍵)/⍵}(2⊃⎕DM) :EndIf :EndTrap ∇ ∇ r←{timeOut}GET(url get);http;task;uri :Access Public ⍝ Sends a GET request to the specified Uri and returns the response as Bytes in an asynchronous operation. ⍝ url = IP Address as numbers (192 168 1 1) or characters ('192.168.1.1' or 'www.mysite.com') ⍝ get = GET request ⍝ timeOut = Optional delay to wait for the completion of the GET request ⍝ If not specify, the value of the Field _TimeOut will be used ⍝ ⍝ r[1] = 1 for Success, 0 for Failure ⍝ r[2] = Response as bytes if Successful, Literal Error if Failure ⍝ The response can be transform to characters by using ⎕UCS with the proper encoding. ⍝ ⍝ TYPICAL EXAMPLE: ⍝ ⍝ http ← ⎕NEW HTTP ⍝ request ← http.GET 'www.rfc-editor.org' '/rfc/rfc793.txt' ⍝ request ← ⎕UCS 2⊃request ⍝ Convert bytes to characters with no encoding ⍝ request ← ⊃{⎕ML←3 ⋄ (⍵≠⎕UCS 10)⊂⍵}request ⍝ Format the request as text ⍝ ⍝ request ← http.GET 'http://www.google.com' '/search?q=dyalog+apl' ⍝ request ← ⎕UCS 2⊃request ⍝ Convert bytes to characters with no encoding ⍝ request ← '<',⊃{⎕ML←3 ⋄ (⍵≠'<')⊂⍵}request ⍝ Format the request as HTML ⍝ Parse the url :If 0=1↑r←BuildUri url ⍝ Failure to build the URI. Return the error as result. →0 :Else ⍝ Success uri←2⊃r :End ⍝ Set timeOut from field if not present :If 0=⎕NC'timeOut' timeOut←_TimeOut :End ⍝ Make the GET request :Trap 0 http←⎕NEW HttpClient http.Timeout←⎕NEW TimeSpan(0,0,0,timeOut) http.BaseAddress←uri task←http.GetByteArrayAsync(⊂,get) ⍝ Wait no more than _TimeOut for the task to complete :If ⎕TSYNC{task.Wait ⍵}&(_TimeOut) ⍝ Success r←1 task.Result task.Dispose :Else ⍝ Failure r←0('HTTP GET Task did not complete: ',task.GetBaseException.Message) :EndIf :Else ⍝ Show the error. :If 90=⎕EN ⍝ .Net Error r←0('HTTP GET: ',⎕EXCEPTION.GetBaseException.Message) :Else ⍝ APL Error r←0(1⊃⎕DM),': ',{(' '=1↑⍵)↓((1↓a,0)∨a←' '≠⍵)/⍵}(2⊃⎕DM) :EndIf :EndTrap ⍝ Clean-up :Trap 0 http.Dispose :EndTrap ∇ :EndClass