400使用Common Lisp usocket发送http请求时出现错误请求



我使用以下代码获取url http://brandonhsiao.com/essays.html:

(defparameter *new-line* '(#Return #Newline))
(defun read-url (host port path)
  (let ((sock (usocket:socket-connect host port)))
    (format (usocket:socket-stream sock) "~A"
            (concatenate 'string
                         "GET " path " HTTP/1.1" *new-line*
                         "Connection: close" *new-line* *new-line*))
    (force-output (usocket:socket-stream sock))
    (do ((line
           (read-line (usocket:socket-stream sock) nil)
           (read-line (usocket:socket-stream sock) nil))
         (all ""))
      ((not line) all)
      (setf all (concatenate 'string all line *new-line*)))))
(print (read-url "brandonhsiao.com" 80 "/essays.html"))

这给了我一个400 Bad Request错误,但当我使用Firefox访问http://brandonhsiao.com/essays.html时,一切都很好。我做错了什么?

看起来我需要包含主机。

(defparameter *new-line* '(#Return #Newline))
(defun read-url (host port path)
  (let ((sock (usocket:socket-connect host port)))
    (format (usocket:socket-stream sock) "~A"
            (concatenate 'string
                         "GET " path " HTTP/1.1" *new-line*
                         "Host: " host *new-line* *new-line*))
    (force-output (usocket:socket-stream sock))
    (do ((line
           (read-line (usocket:socket-stream sock) nil)
           (read-line (usocket:socket-stream sock) nil))
         (all ""))
      ((not line) all)
      (setf all (concatenate 'string all line " ")))))

相关内容

  • 没有找到相关文章

最新更新