http - 使用 Common Lisp usocket 发送 http 请求时出现 400 Bad Request

标签 http lisp httprequest common-lisp usocket

我正在使用以下代码获取 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 " ")))))

关于http - 使用 Common Lisp usocket 发送 http 请求时出现 400 Bad Request,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18009306/

相关文章:

java - 在Micronaut的声明式客户端中传递自定义 header

python - 在 Python 中制作一个 http 服务器

lisp - 比较 Common Lisp 和 Gambit w.r.t 他们的库访问和对象系统

azure - 使用 get 和 body 参数调用嵌套的 Azure 逻辑应用

Java servlet 处理应用程序/八位字节流用于图像上传

angular - 为什么 HttpErrorResponse 的实例不会触发使用 HttpClient 在 Angular 中订阅服务器响应的错误函数?

http - 如何为云端设置缓存 header 以便 Google Pagespeed 识别它?

scheme - 我如何评估 MIT Scheme 中的符号?

clojure - Lisp-1 和 Lisp-2 有什么区别?

java - 在 Servlet 中获取 HTTP 和 HTTPS 请求的完整 URL 和查询字符串