Managing a Network Connection Overview

Managing a Network Connection Using Hypertext Transfer Protocol (HTTP)

Overview

A network connection is used to manage communication between web browsers and web servers. Hypertext Transfer Protocol (HTTP) is one of the more popular ways to manage this connection, with the web browser using a Request Message to locate and provide (GET) a resource or to POST data to the web server, and the web server returning a Response Message which includes the requested resource, a success message, or an error message in some cases. When a user enters a Uniform Resource Locator (URL) in the address bar on a web browser, the browser converts that Uniform Resource Locator (URL) into a request message to send to the web server for interpretation and a response.

Request Message

A typical Request Message consists of a Request Line, Request Headers, a Blank Line (which must be included to separate the Request Headers from the Request Message Body), and a Request Message Body (which we will go over in another tutorial). The Request Line includes: Request Headers ask the server to respond with specific properties (such as in a specific language, or media type). These requested properties are listed as name:value pairs with the name and value separated by a colon ":" and multiple associated values separated by commas "," as shown in the following example:

  • Request Method : which are predefined by the Hypertext Transfer Protocol (HTTP), case sensitive, and must be typed in all uppercase letters (we will discuss a few of these, GET and POST, in a future tutorial).
  • Web Address : specifies the resource being requested from the server
  • HTTP Version : either HTTP/1.0 or HTTP/1.1 which is sent as a request to the server. If the server does not support the requested version it will respond informing the user to switch to the alternate version.

The Request Line is sent in the following format:

RequestMethod  WebAddress  HTTP-Version

To set the web address for a NetworkRequest object in Quorum, we start by using the "Libraries.Network.NetworkRequest" library. We will then create a NetworkRequest object "request" and set the web address for "request". Finally we will output the web address to the screen.

Here is the complete code section:

use Libraries.Network.NetworkRequest

class Main
   action Main
      NetworkRequest request
      request:SetWebAddress("https://quorumlanguage.com/GETtest.php")
      output request:GetWebAddress()
   end
end

The output should look something like this:

https://quorumlanguage.com/GETtest.php

Content-type: application/json
Connection: keep-alive
Accept: text/plain, text/html, image/gif, application/json
Accept-Encoding: gzip, deflate
Accept-Language: en-us

In order to add Request Headers to a NetworkRequest object "request" we will need to use the "Libraries.Network.NetworkRequest" and "Libraries.Containers.HashTable" libraries. Then we will create a HashTable object "headers" to store our headers as shown below:

use Libraries.Network.NetworkRequest
use Libraries.Containers.HashTable

class Main
   action Main
    //creates a HashTable object "headers" with text type key names and text type
    //values
   HashTable <text, text> headers
    //Adds the name, value pairs to be used as headers
   headers:Add("Connection", "keep-alive")
    //use comma’s "," to separate multiple values in a header
   headers:Add("Accept-Encoding", "gzip, deflate")

Next, we will create a NetworkRequest object "request" and set "request’s" Headers to the HashTable object "headers".

    //creates a NetworkRequest object "request"
   NetworkRequest request
    //sets the NetworkRequest object "request’s" Headers to the Headers we
    //created and set as the HashTable object "headers"
   request:SetHeaders(headers)

Finally we will output the associated value of "request’s" header by its key name as shown below:

    //will output the header value that is associated with the key name "Connection"
   output headers:GetValue("Connection")
    //will output the header values that are associated with the key name 
    //"Accept-Encoding". Since this header has more than one value the values will
    //be separated by a comma "," when they are output to the screen.
   output headers:GetValue("Accept-Encoding")

Here is the complete code section to set the NetworkRequest object "request’s" headers and then output the key name’s specific "header's" value(s) :

use Libraries.Network.NetworkRequest
use Libraries.Containers.HashTable

class Main
   action Main
       HashTable<text, text> headers
       headers:Add("Connection", "keep-alive")
       headers:Add("Accept-Encoding", "gzip, deflate")
       NetworkRequest request
       request:SetHeaders(headers)
       output headers:GetValue("Connection")
       output headers:GetValue("Accept-Encoding")
   end
end

The output should look like this:

keep-alive
gzip, deflate

Response Message

  • HTTP Version : either HTTP/1.0 or HTTP/1.1
  • Status Code : which provides the outcome of the request
  • Status Text : which provides a short, human-readable message describing the status code such as "200 OK", "404 Not Found", or "401 WWW-Authenticate".

The Status Line is returned in the following format:

HTTP-Version  StatusCode  StatusText

In order to view the Response Message’s Status Code and Status Text in Quorum we will start by adding the following libraries:

use Libraries.Network.NetworkConnection
use Libraries.Network.NetworkRequest
use Libraries.Network.NetworkResponseEvent
use Libraries.Network.NetworkRequestListener

We will then create a NetworkRequest object "request" and set its type and web address which are discussed in depth in the following tutorials.

class Main is NetworkRequestListener
  action Main
        //creates the NetworkRequest object "request"
      NetworkRequest request
        //sets "request’s" type to GET
      request:SetRequestTypeToGet()
       //sets "request’s" web address to "https://quorumlanguage.com/GETtest.php"
      request:SetWebAddress("https://quorumlanguage.com/GETtest.php")

Next, we will create a NetworkConnection object "http", add the NetworkListener which will be notified when a response is received from the server, and then Send a Request of the NetworkRequest object "request" as shown below:

       //creates a NetworkConnection object "http"
      NetworkConnection http
       //Adds a listener to the NetworkConnection object "http"
      http:AddListener(me)
       //Adds a SendRequest of the NetworkRequest object "request" to the
       //NetworkConnection object "http"
      http:SendRequest(request)
  end

Finally, we will view the response Status Code followed by the Status Text using the following code:

  action ResponseReceived(NetworkResponseEvent response)
      output response:GetStatusCode() + " " + response:GetStatusText()
  end

Here is the complete code section to create a NetworkRequest object "request" and view "request’s" response from the server:

use Libraries.Network.NetworkConnection
use Libraries.Network.NetworkRequest
use Libraries.Network.NetworkResponseEvent
use Libraries.Network.NetworkRequestListener
class Main is NetworkRequestListener
  action Main
      NetworkRequest request
      request:SetRequestTypeToGet()
      request:SetWebAddress("https://quorumlanguage.com/GETtest.php")
      NetworkConnection http
      http:AddListener(me)
      http:SendRequest(request)
  end
  action ResponseReceived(NetworkResponseEvent response)
      output response:GetStatusCode() + " " + response:GetStatusText()
  end
end

The output should look like this:

200 OK

Next Tutorial

In the next tutorial, we will discuss the GET Method, which describes an introduction to sending a request message using the Hypertext Transfer Protocol (HTTP) GET Method.