This is a quick example of a simple HTTP client program using the network-socket API that is provided as a part of mbed-os. The program brings up an underlying network interface, and uses it to perform an HTTP transaction over a TCPSocket.

Committer:
mbed_official
Date:
Wed Mar 08 22:00:06 2017 +0000
Revision:
13:ed9e4aa00044
Parent:
12:2e7466eba9a3
Child:
40:afef93b6d854
Merge pull request #22 from ARMmbed/example-200

Changed example to use non-HTTPS site to avoid 301 errors
.
Commit copied from https://github.com/ARMmbed/mbed-os-example-sockets

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 0:17bd84fc5087 1 #include "mbed.h"
mbed_official 5:3e952c60d705 2 #include "EthernetInterface.h"
mbed_official 0:17bd84fc5087 3
mbed_official 5:3e952c60d705 4 // Network interface
mbed_official 5:3e952c60d705 5 EthernetInterface net;
mbed_official 0:17bd84fc5087 6
mbed_official 0:17bd84fc5087 7 // Socket demo
mbed_official 5:3e952c60d705 8 int main() {
mbed_official 5:3e952c60d705 9 // Bring up the ethernet interface
mbed_official 5:3e952c60d705 10 printf("Ethernet socket example\n");
mbed_official 5:3e952c60d705 11 net.connect();
mbed_official 0:17bd84fc5087 12
mbed_official 0:17bd84fc5087 13 // Show the network address
mbed_official 5:3e952c60d705 14 const char *ip = net.get_ip_address();
mbed_official 13:ed9e4aa00044 15 const char *netmask = net.get_netmask();
mbed_official 13:ed9e4aa00044 16 const char *gateway = net.get_gateway();
mbed_official 13:ed9e4aa00044 17 printf("IP address: %s\n", ip ? ip : "None");
mbed_official 13:ed9e4aa00044 18 printf("Netmask: %s\n", netmask ? netmask : "None");
mbed_official 13:ed9e4aa00044 19 printf("Gateway: %s\n", gateway ? gateway : "None");
mbed_official 0:17bd84fc5087 20
mbed_official 0:17bd84fc5087 21 // Open a socket on the network interface, and create a TCP connection to mbed.org
mbed_official 5:3e952c60d705 22 TCPSocket socket;
mbed_official 5:3e952c60d705 23 socket.open(&net);
mbed_official 13:ed9e4aa00044 24 socket.connect("api.ipify.org", 80);
mbed_official 13:ed9e4aa00044 25 char *buffer = new char[256];
mbed_official 0:17bd84fc5087 26
mbed_official 12:2e7466eba9a3 27 // Send an HTTP request
mbed_official 13:ed9e4aa00044 28 strcpy(buffer, "GET / HTTP/1.1\r\nHost: api.ipify.org\r\n\r\n");
mbed_official 13:ed9e4aa00044 29 int scount = socket.send(buffer, strlen(buffer));
mbed_official 13:ed9e4aa00044 30 printf("sent %d [%.*s]\n", scount, strstr(buffer, "\r\n")-buffer, buffer);
mbed_official 0:17bd84fc5087 31
mbed_official 13:ed9e4aa00044 32 // Recieve an HTTP response and print out the response line
mbed_official 13:ed9e4aa00044 33 int rcount = socket.recv(buffer, 256);
mbed_official 13:ed9e4aa00044 34 printf("recv %d [%.*s]\n", rcount, strstr(buffer, "\r\n")-buffer, buffer);
mbed_official 13:ed9e4aa00044 35
mbed_official 13:ed9e4aa00044 36 // The api.ipify.org service also gives us the device's external IP address
mbed_official 13:ed9e4aa00044 37 const char *payload = strstr(buffer, "\r\n\r\n")+4;
mbed_official 13:ed9e4aa00044 38 printf("External IP address: %.*s\n", rcount-(payload-buffer), payload);
mbed_official 0:17bd84fc5087 39
mbed_official 0:17bd84fc5087 40 // Close the socket to return its memory and bring down the network interface
mbed_official 0:17bd84fc5087 41 socket.close();
mbed_official 13:ed9e4aa00044 42 delete[] buffer;
mbed_official 0:17bd84fc5087 43
mbed_official 5:3e952c60d705 44 // Bring down the ethernet interface
mbed_official 5:3e952c60d705 45 net.disconnect();
mbed_official 0:17bd84fc5087 46 printf("Done\n");
mbed_official 0:17bd84fc5087 47 }