4

使用预告片我试图在页面流式传输开始后设置一个 cookie。下面的代码尝试设置 cookie。它似乎返回了正确的响应,但没有在浏览器中设置 cookie(ff 和 chrome)。

var express = require('express'),
    http = require('http'),
    app = express(),
    server;

app.use(app.router);

app.all('*', function(req, res) {
    res.setHeader('Trailer', 'Set-Cookie');
    res.setHeader('Content-Type', 'text/plain');
    res.write("hi this is ogkla");
    res.addTrailers({
        'Set-Cookie': "val=ogkla"
    });
    res.end();
});
server = http.createServer(app).listen(80);
module.exports = server;

响应

curl -iv --raw 'http://localhost/'
* Adding handle: conn: 0x7fd87180aa89
* Adding handle: send: 0
* Adding handle: recv: 0
* Curl_addHandleToPipeline: length: 1
* - Conn 0 (0x7fd87180aa89) send_pipe: 1, recv_pipe: 0
* About to connect() to localhost port 80 (#0)
*   Trying ::1...
*   Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 80 (#0)
> GET / HTTP/1.1
> User-Agent: curl/7.30.0
> Host: localhost
> Accept: */*
> 
< HTTP/1.1 200 OK
HTTP/1.1 200 OK
< X-Powered-By: Express
X-Powered-By: Express
< Trailer: Set-Cookie
Trailer: Set-Cookie
< Content-Type: text/plain
Content-Type: text/plain
< Date: Wed, 26 Feb 2014 06:45:43 GMT
Date: Wed, 26 Feb 2014 06:45:43 GMT
< Connection: keep-alive
Connection: keep-alive
< Transfer-Encoding: chunked
Transfer-Encoding: chunked

< 
10
hi this is ogkla
0
Set-Cookie: val=ogkla

* Connection #0 to host localhost left intact

我在这里做错了什么。需要帮助才能找到它。

4

2 回答 2

3

据我了解,大多数用户代理会解析预告片,但不会处理它们。

于 2014-02-26T07:26:38.033 回答
1

Set-Cookie规范不允许的标头之一。

所以你的浏览器会解析它,但它被明确地忽略了。

于 2019-01-28T09:05:05.403 回答