Consultor Eletrônico



Kbase P159707: How to load a HTTP resource into a memptr
Autor   Progress Software Corporation - Progress
Acesso   Público
Publicação   04/02/2010
Status: Unverified

GOAL:

How to load a HTTP resource into a memptr

GOAL:

How to read a file from a Webserver and load it into a memptr

GOAL:

How to get a file from a Webserver using sockets and load it into a memptr

GOAL:

How to use GET method from HTTP protocol using 4GL

FACT(s) (Environment):

All Supported Operating Systems
OpenEdge 10.x

FIX:

define var h as handle.
create socket h.
h:set-read-response-procedure("myReadProc").
h:connect("-H <yourServer> -S 80"). /* change it here to be your server and port if different from 80*/
def var m as memptr.
def var c as char.
c = "GET <resource name> HTTP/1.1" + "~nHost:<your host again>n~n". /* change it here to match your exact location */
set-size(m) = 0.
set-size(m) = length(c) + 1.
put-string(m,1) = c.
h:write(m,1, length(c)).
def var hWholeFile as memptr.
disp h:get-bytes-available().
set-size(hWholeFile) = 0.
wait-for read-response of h.

procedure myReadProc:
def var iBytes as int.
def var hBuffer as memptr.

set-size(hBuffer) = 0.
iBytes = h:get-bytes-available().
def var hAux as memptr.
set-size(hAux) = 0.
do while iBytes > 0:
if (iBytes > 1000) then
iBytes = 1000.


set-size(hBuffer) = 0.
set-size(hBuffer) = iBytes.
h:read(hBuffer, 1,iBytes).
if (get-size(hWholeFile) = 0) then do:
set-size(hWholeFile) = iBytes.
put-bytes(hWholeFile,1) = get-bytes(hBuffer,1, iBytes ).
end.
else do:
set-size(hAux) = 0.
set-size(hAux) = get-size(hWholeFile).
put-bytes(hAux,1) = get-bytes(hWholeFile,1,get-size(hWholeFile)).
set-size(hWholeFile) = 0.
set-size(hWholeFile) = get-size(hAux) + iBytes.
put-bytes(hWholeFile,1) = get-bytes(hAux,1, get-size(hAux)).
put-bytes(hWholeFile, get-size(hAux) + 1) = get-bytes(hBuffer,1,get-size(hBuffer)).
end.
iBytes = h:get-bytes-available().


end.

end.
After running this program, the memptr hWholeFile will have all the contents of the resource. In order to use it, NEVER forget to remove the headers that comes along with the message, in case this message has to be parsed by a XML for instance.