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 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算