它的工作對我來說:
(with-open-stream (stream (ext:open-http
"http://stackoverflow.com/questions/234242424242"
:if-does-not-exist nil))
(format t "~&Stream: ~A~%" stream))
輸出:
;; connecting to "http://stackoverflow.com/questions/234242424242"...connected...HTTP/1.1 404 Not Found
;; HTML source of Page not found
Stream: NIL
NIL
有一個延遲,以獲得連接,但它的工作原理。
如果頁面確實存在:
[7]> (with-open-stream (stream (ext:open-http
"http://stackoverflow.com/questions/36003343/clisp-open-http-example"
:if-does-not-exist nil))
(format t "~&Stream: ~A~%" stream))
;; connecting to "http://stackoverflow.com/questions/36003343/clisp-open-http-example"...connected...HTTP/1.1 200 OK
Stream: #<IO INPUT-BUFFERED SOCKET-STREAM CHARACTER stackoverflow.com:80>
NIL
維基百科我不能讓它工作,因爲Wikipedia.org它重定向到HTTPS和EXT:OPEN-HTTP
也不能直接處理HTTPS,也不能處理重定向:
這裏,如果HTTPS是直接使用:
[10]> (with-open-stream (stream (ext:open-http
"https://en.wikipedia.org/wiki/Common_Lisp"
:if-does-not-exist nil))
(format t "~&Stream: ~A~%" stream))
*** - OPEN-HTTP: "https://en.wikipedia.org/wiki/Common_Lisp" is not an HTTP URL
The following restarts are available:
ABORT :R1 Abort main loop
Break 1 [11]> :r1
如果 「https」 開頭是 「HTTP」 取代,CLISP不構建一個合適的地址:
[12]> (with-open-stream (stream (ext:open-http
"http://en.wikipedia.org/wiki/Common_Lisp"
:if-does-not-exist nil))
(format t "~&Stream: ~A~%" stream))
;; connecting to "http://en.wikipedia.org/wiki/Common_Lisp"...connected...HTTP/1.1 301 TLS Redirect --> "https://en.wikipedia.org/wiki/Common_Lisp"
;; connecting to "http://en.wikipedia.orghttps://en.wikipedia.org/wiki/Common_Lisp"...
*** - PARSE-INTEGER: substring "" does not have integer syntax at position 0
The following restarts are available:
ABORT :R1 Abort main loop
Break 1 [13]>
我想你在這個過程中會遇到其他問題:幾乎所有的網站現在都使用HTTPS,CLISP的OPEN-HTTP不支持HTTPS。另外,就我所見,維基百科的文章並不放在* .html文件中,其他文件的路徑更復雜。 – mobiuseng
相關:[是否有Wikipedia API?](http://stackoverflow.com/q/627594/124319)。另外,請看[Drakma](http://weitz.de/drakma/) – coredump
感謝迄今爲止的回覆,但請關注以下問題:如何防止在頁面未打開時掛起http存在,無論其網址。一個例子就足夠了。 – Leo