gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 19:01:06 24 4
gpt4 key购买 nike

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com