HTTP – Hypertext Transfer Protocol, an application layer protocol on top of TCP / IP.
This is the core protocol that runs the internet.
HTTP is a request – response protocol. The client makes a TCP / IP connection to the server, sends the request, which is a text message with a simple format, and the server sends back the response. The connection can be closed at this point.
A request is made of the following parts:
- Request line: method + url + HTTP version. For example: GET /index.html HTTP/1.1
- Headers: includes information such as: authorization, cookies, content type, custom headers and more.
- Message body: optional message body. There are all kinds of common formats for the message body, such as multipart, form-urlencoded, json and others.
The response message is quite similar and it includes:
- Status line – includes the status code:
- 200 – 299 – successful.
- 300 – 399 – redirection.
- 400 – 499 – client error (includes the famous 404 – missing page error).
- 500 – 599 – server error.
- Headers: includes new cookies, content type and other information.
- Message body: optional message body.
There are several types of HTTP methods. The two most common methods are GET and POST.
GET is a request with no message body. When you browse to a site you are making a GET request.
The request can include parameters in the query string: https://www.example.com?query=abc
POST is a request with a message body. Most of the html forms are submitted with POST requests. POST requests, unlike GET requests, are considered not idempotent. This means that repeating the same POST request is not safe as it can ‘do something’. For example make another order.
The browser will show a warning before trying to repeat the same POST request.
HTTPS – most of the communication, nowadays, is done over secured TLS connections (which replaced the older SSL connections). This is the same protocol as HTTP.
Making HTTP requests is very simple in B4X. It is done with B4A OkHttpUtils2, B4J jOkHttpUtils2 or B4i iHttpUtils2. The libraries are identical.
These libraries are designed to work with the Wait For keyword: https://www.b4x.com/android/forum/threads/b4x-okhttputils2-with-wait-for.79345/#content
Note that HttpJob.Download makes a GET request.
Making HTTPS requests is done exactly in the same way as non-TLS requests. If the server certificate is not recognized, you will need to configure the library to accept all certificates.
Newer versions of iOS and Android discourage the usage of non-TLS requests (cleartext requests) and will require disabling the ATS feature in iOS or allowing cleartext communication in Android.
B4J has very good support for building http servers with jServer library. It also support WebSocket communication, which provides a full duplex channel between the client and server (unlike the request – response model).