END_FUNCTION;
FUNCTION build_http_get : STRING;
VAR_INPUT
host : STRING;
page : STRING;
END_VAR;
VAR
cmd : STRING;
END_VAR;
cmd := "GET ";
cmd := cmd + page;
cmd := cmd + " HTTP/1.1$N";
cmd := cmd + "Host: ";
cmd := cmd + host;
cmd := cmd + "$N";
cmd := cmd + "$N";
build_http_get := cmd;
END_FUNCTION;
FUNCTION GetPage : INT;
VAR_INPUT
host : STRING;
port : DINT := 80;
iface : SINT;
page : STRING;
filename : STRING;
https : BOOL;
END_VAR;
VAR
handle : SYSHANDLE;
sent : DINT;
len : INT;
rc : INT;
request : STRING;
END_VAR;
request := build_http_get(host := host, page := page);
DebugMsg(message := "Connecting...");
rc := server_open(host := host, port := port, iface := iface, https := https, handle:= handle);
IF rc < 0 THEN
DebugFmt(message := "server_open=\1", v1 := rc);
GetPage := -1;
RETURN;
END_IF;
DebugMsg(message := "Send request...");
len := strLen(str := request);
strToMemory(dst := ADDR(buf), str := request, len := len);
rc := soSend(socket := handle, data := ADDR(buf), size := len, sent := sent);
IF rc < 1 THEN
DebugFmt(message := "soSend=\1", v1 := rc);
soClose(socket := handle);
GetPage := -2;
RETURN;
END_IF;
DebugMsg(message := "Wait for reply...");
rc := reply_header(handle := handle);
IF rc < 1 THEN
DebugFmt(message := "reply_header=\1", v1 := rc);
soClose(socket := handle);
GetPage := -3;
RETURN;
END_IF;
DebugFmt(message := "Reply = \1", v1 := rc);
rc := reply_body(handle := handle, filename := filename);
IF rc < 1 THEN
DebugFmt(message := "reply_body=\1", v1 := rc);
soClose(socket := handle);
GetPage := -3;
RETURN;
END_IF;
soClose(socket := handle);
END_FUNCTION;
PROGRAM web_client;
VAR
rc : INT;
END_VAR;
DebugMsg(message := "----------------------------------------");
DebugMsg(message := "Initialize...");
fsMediaOpen(media := 1);
Sleep(delay := 2000);
fsDirChange(path := "B:/");
netOpen(iface := _NET_IFACE_LAN1);
WHILE NOT netConnected(iface := _NET_IFACE_LAN1) DO
DebugMsg(message := "Wait for connection...");
Sleep(delay := 3000);
END_WHILE;
DebugMsg(message := "----------------------------------------");
rc := GetPage(
host := "www.google.dk"
,https := ON
,port := 443
,iface := _NET_IFACE_LAN1
,page := "/"
,filename := "index.htm"
);
DebugFmt(message := "GetPage=\1", v1 := rc);
BEGIN
END;
END_PROGRAM;
|