dtu15253 2011-09-02 14:29
浏览 58
已采纳

HTTP请求URL不是HTTP请求标头的一部分吗?

Here's an excerpt from a Wikipedia article:

In contrast to the GET request method where only a URL and headers are sent to the server, POST requests also include a message body.

Based on that, it looks like the URL is sent separately from the header, but if that's so, why do we use the header() method in PHP to set a URL to redirect to?

header("Location: http://google.com");
  • 写回答

3条回答 默认 最新

  • douzai8285 2011-09-02 14:41
    关注

    When you want to browse an URL from your browser you type an URL. The browser puts the url inside an HTTP REQUEST like this:

    GET /path/to/resource.php?var=data1&othervar=data2 HTTP/1.1
    Host: example.com
    Connection: keep-alive
    "empty line"
    

    Then a webserver gives you an answer like this:

    HTTP/1.0 200 OK
    Date: Fri, 02 Sep 2011 14:37:36 GMT
    Server: Apache
    Cache-Control: private, s-maxage=0, max-age=0, must-revalidate
    Content-Encoding: gzip
    Vary: Accept-Encoding
    Content-Length: 149
    Content-Type: text/javascript; charset=utf-8
    Connection: keep-alive
    "empty line"
    "149 bytes of Response data"
    

    Every line like this "Header-Name: header_value " is an header.
    PHP header function adds an header to the response before sending it to user's browser.
    In your example the header is:

    Location: http://google.com
    

    And it's added just after the last header before the "empty line" (which is a line which contains only a ).
    POST requests are different from GET requests because you have a request body after the "empty line"):

    POST /path/to/resource.php HTTP/1.1
    Host: example.com
    Connection: keep-alive
    Content-Length: "number of bytes in the body"
    "empty line"
    variable=data&othervar=data2
    

    In conclusion an HTTP request is made like this:

    1. Request/response row (POST or GET followed by url and http version for request, Http version followed by response code and response string for the response) ended with
    2. Request/response headers (header-name: header_value )
    3. empty row ( )
    4. Response/request body

    PS. Rows are ALWAYS closed by " " bytes ("empty lines" are made of just those two bytes).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 51寻迹小车定点寻迹
  • ¥15 谁能帮我看看这拒稿理由啥意思啊阿啊
  • ¥15 关于vue2中methods使用call修改this指向的问题
  • ¥15 idea自动补全键位冲突
  • ¥15 请教一下写代码,代码好难
  • ¥15 iis10中如何阻止别人网站重定向到我的网站
  • ¥15 滑块验证码移动速度不一致问题
  • ¥15 Utunbu中vscode下cern root工作台中写的程序root的头文件无法包含
  • ¥15 麒麟V10桌面版SP1如何配置bonding
  • ¥15 Marscode IDE 如何预览新建的 HTML 文件