Skip to content

HTTP Client Example

This is a very basic use of the httpclientlib library to create a program that connects to a website and then displays several important bits of HTML related to that website. This program will also cover some basic string manipulation.

The first thing we will have to do after creating a new project is to add the httpclientliblibrary to the project, it can be found in the lib folder of your SIMPOL installation httpclientlib.sml

Base Program

The base program we will be making consists of two functions the first of these is a function that returns a new line constructor, given below:

function new_line()
end function "{d}{a}"

Variable Declaration

The second function will contain all our logic but first, we have to declare all the variables we will use

string result;
string url;
httprequest request;
httpresponse response;

This alone would not do a lot because all we have done is declare two string variables: one as an output, the other to store the URL. We have also declared our httprequest and httpresponse variables, these will be required later on


But before we can get to reading or manipulating a websites HTTP we have to set up the variables that will allow us to get that information

result = "Start tls test application" + new_line();
url = "localhost";
result = result + "Try to connect to " + url + new_line();
request =@;
request.url =@ parseurl(url);
request.requesttype = "GET";

end function result

The first notable thing we have done here is set the URL to localhost this means that you will need to have a server running on your local machine, how to do this is explained in more detail in the IDE Quick Start Guide although if you already have a something running this step is not necessary. Any manipulation of the result variable up to this point is not important.

We then get onto the first important statement request =@; this sets up request with the default values for a httprequest variable, we will manipulate a few of these later on. We then do two things at once in the next line

request.url =@ parseurl(url);

The first of these is parseurl, this function checks whether the URL you are passing into it is valid, it will return .nul if it is not a valid URL otherwise it will return the original URL in a more useful format. The second thing this line does is then set the URL of the request variable to whatever is returned from parseurl. The last thing we will do is set the requestype of our variable to "GET"


At this point, we have completed the request side and now have to actually request information from the server so we can display it nicely. The code to do this is as follows

response =@ httpsendreceive(request); if response.statuscode != .nul     result = result + "Already connected to server" + new_line();    result = result + "status code: " + .tostr(response.statuscode, 10) + new_line();     result = result + "full header: " + response.fullheader + new_line();  else     result = "Do not connect to server";  end if

The first key bit is response =@ httpsendreceive(request); this bit of code sends the request we have created and outputs the server’s response. This means we can now manipulate the various bits of information into a format of our choosing. We here have decided to just display the status code and full header although of course any information passed can be requested. For a full list consult the relevant wiki page for the httpclientlib (linked at the end of the document).

The first thing we are doing here is to check if the server has actually returned any information, we are just using a rudimentary if statement that checks if a status code has been returned by the website. But this could be elaborated to deal with the various error codes.

At this point there are two more important lines to look at:

result = result + "status code: " + .tostr(response.statuscode, 10) + new_line();  result = result + "full header: " + response.fullheader + new_line();

We only care about middle two elements here, in the first line it converts the status code into a string in base 10 and adds this to the output. The second adds the full HTML header for the website into the output.