[REBOL] Re: HTTP question/obtaining HTTP headers from web server
From: jurgens::akcorp::net at: 28-Mar-2002 0:21
Re: obtaining HTTP response headers from a web server, I have posted a
function from a script which does that. It is more complicated than needed
for just that task alone because it was developed for a specific purpose and
has not been designed for generic applications. Hopefully you can easily
pick out the necessary parts. In short, instead of using the simple "read"
function, you need to use ports which requires both knowledge of how ports
work in Rebol and more specific knowledge about the HTTP protocol. Once
learned, it is pretty simple.
Burt
[jurgens--akcorp--net]
request-http: func[target [string!]
/host host-name [string!]
/port port-number [integer!]
/headers header-data [block!]
/default-headers default-header-data [block!]
/post post-data [block!]
/querystring query-data [block!]
/display
/local
connection
cookies
data
default-headers-string
header-block
headers
last-host
open-result
post-data-formatted
request
result
temp-filename
url
webenv][
last-host: ""
cookies: ""
default-header-string: ""
if default-headers [append clear head default-header-string format-header
default-header-data]
if querystring [append target rejoin ["?" format-post query-data]]
header-block: make block! []
if headers [header-block: header-data]
if post [
post-data-formatted: format-post post-data
insert header-block compose[
'Content-Type "application/x-www-form-urlencoded"
'Content-Length (length? post-data-formatted)
]
]
headers: does [rejoin [target " HTTP/1.0^/" default-header-string
either empty? header-block ["^/"] [format-header header-block] ]]
either error? open-result: try[
insert connection: open url: to-url
rejoin ["tcp://" either host [append clear head last-host host-name]
[last-host] ":" either port [port-number] [80]]
request: either post [
rejoin ["POST " headers post-data-formatted]
][
rejoin ["GET " headers]
]
][
print ["Error attempting to open" url newline "[" request "]"]
probe disarm open-result
return none
][
print ["Successfully opened" url newline "[" request "]"]
result: make string! 100000
while [data: copy connection] [append result data]
close connection
headers: copy/part result data: find result "^/^/"
data: skip data 2
probe headers
header-block: make block! []
parse/all headers [
copy status to "^/" (append header-block compose [status (status)])
skip
some [
copy header-field to ":" (append header-block to-word header-field)
skip
skip
copy value to "^/" (append header-block value)
skip
]
]
if display [
write temp-filename: join temp-file [page-count: page-count + 1 ".html"]
data
shell-exec windows-path temp-filename "" ""
]
return reduce [header-block data]
]
]