简体   繁体   English

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

[英]400 Bad Request when sending a http request with Common Lisp usocket

I'm using the following code to grab the url http://brandonhsiao.com/essays.html : 我正在使用以下代码来获取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"))

This gives me a 400 Bad Request error, but when I visit http://brandonhsiao.com/essays.html with Firefox, everything is fine. 这给我一个400 Bad Request错误,但是当我使用Firefox访问http://brandonhsiao.com/essays.html时,一切都很好。 What am I doing wrong? 我究竟做错了什么?

It looks like I needed to include the host. 看来我需要包含主机。

(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 " ")))))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM