2014-01-20 8 views
2

Команды read и write в Реболе при передаче URL! параметр, может получить либо двоичный ответ, либо ошибку. Таким образом, в «одноразовом» стиле выполнения GET или POST нет нигде, чтобы получить заголовки ответов.Как получить заголовки HTTP-ответа в Rebol3?

result: read http://www.rebol.com 

В этом посте о том, как это сделать в Rebol2, предлагается, чтобы вы открыли порт! и получить его из/поле локального заголовкам порта:

Rebol - HTTP response headers

Это работает в Rebol2, например

hp: open http://www.rebol.com 
result: read hp 
probe hp/locals/headers/Content-Encoding 

Но в Rebol3, когда я открыть порт и попробовать это местные жители области порт пуст. Как добиться такого же поведения в Rebol3?

ответ

2

Это то, что вы ищете?

>> hp: open http://www.rebol.com/ 
>> to string! read hp 
== {<!doctype html> 
<html><head> 
<meta name="generator" content="REBOL WIP Wiki"/> 
<meta name="date" content="16-Feb-2014/15:58:59-8:00"/> 
<meta name="rebol-version" content="2.100.97.4.2"/> 
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/> 
<meta http-equiv="Pragma" content="no-cache" /> 
<meta http-equiv="Expires" content="-1" /> 
<meta http-equiv="Cache-Control" content="no-cache" /> 
<meta name="Description" content="REBOL: a lightweight computer language with ad 
vanced semantics. ... 

>> query hp 
== make object! [ 
    name: %/ 
    size: 7118 
    date: none 
    type: 'file 
    response-line: "HTTP/1.1 200 OK" 
    response-parsed: 'ok 
    headers: make object! [ 
     Content-Length: 7118 
     Transfer-Encoding: none 
     Last-Modified: "Sun, 16 Feb 2014 23:58:59 GMT" 
     Date: "Sat, 05 Apr 2014 08:19:34 GMT" 
     Server: "Apache" 
     Accept-Ranges: "bytes" 
     Connection: "close" 
     Content-Type: "text/html" 
    ] 
] 
+0

Прохладный ... не знал о запросе, спасибо! – HostileFork

2

Один (многословно) решение:

target: http://www.rebol.com/ 
port: make port! target 
; make alterations to port/spec here, e.g. 
; port/spec/method -- HTTP method 
; port/spec/headers -- [set-word value] block of headers 
; port/spec/content -- content 
port/awake: func [event][ 
    switch event/type [ 
     connect [read event/port false] 
     done [true] 
    ] 
] 
open port 
response: query port 
wait [port 1] 
close port 
probe response 
+0

Ожидание более сжатого ответа ... – rgchris