首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >400使用Common Lisp usocket发送http请求时出现错误请求

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

Stack Overflow用户
提问于 2013-08-02 13:22:31
回答 1查看 256关注 0票数 0

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

代码语言:javascript
复制
(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错误,但是当我用火狐访问http://brandonhsiao.com/essays.html时,一切都很好。我做错了什么?

EN

回答 1

Stack Overflow用户

发布于 2013-08-02 13:44:53

看起来我需要包括主机。

代码语言:javascript
复制
(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 " ")))))
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18009306

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档