如何让浏览器停止缓存服务器响应?

发布于 2024-10-15 22:40:32 字数 1133 浏览 6 评论 0原文

我为自己构建的机器人开发了自己的自定义 Web 服务器(视频),但并不真正了解HTTP 的复杂性。我遇到的一个问题是我从浏览器发送一个请求,例如 http://192.168.2.10/r?cmd=doStuff 并且机器人做出响应。然后,我再次从浏览器发送相同的请求(通过单击我保存的书签),并且该请求永远不会发出,它只显示前一个请求的响应。

以下是来自浏览器(特别是 IE8)的内容:

GET /r?cmd=s HTTP/1.1
Accept: image/jpeg, application/x-ms-application, image/gif, application/xaml+xml, image/pjpeg,     application/x-ms-xbap, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*
Accept-Language: en-US
User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; Tablet PC 2.0; InfoPath.2; .NET4.0C; .NET4.0E; Zune 4.7)
Accept-Encoding: gzip, deflate
Host: 192.168.2.10
Connection: Keep-Alive

机器人响应如下:

HTTP/1.1 200 OK
Content-Type: text/html; charset=UTF-8
Server: The Little Robot That Could 1.0
Date: Thu, 01 Jan 2009 00:05:00 GMT
Content-Length: 4
X-Powered-By: Little Robot HTTP Server

我是否需要添加一些其他 HTTP 标头以防止浏览器缓存响应?

I developed my own custom web server for a robot I built (video), without really knowing the intricacies of HTTP. One issue I am running into is I send a request from a browser, say, http://192.168.2.10/r?cmd=doStuff and the robot responds. Then I send the same request again from the browser (by clicking on a bookmark I saved), and the request never goes out, it just shows the response from the previous request.

Here is what comes from the browser (IE8 in particular):

GET /r?cmd=s HTTP/1.1
Accept: image/jpeg, application/x-ms-application, image/gif, application/xaml+xml, image/pjpeg,     application/x-ms-xbap, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*
Accept-Language: en-US
User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; Tablet PC 2.0; InfoPath.2; .NET4.0C; .NET4.0E; Zune 4.7)
Accept-Encoding: gzip, deflate
Host: 192.168.2.10
Connection: Keep-Alive

The robot responds with the following:

HTTP/1.1 200 OK
Content-Type: text/html; charset=UTF-8
Server: The Little Robot That Could 1.0
Date: Thu, 01 Jan 2009 00:05:00 GMT
Content-Length: 4
X-Powered-By: Little Robot HTTP Server

Do I need to throw some other HTTP header into the mix to prevent the browser from caching the response?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

俯瞰星空 2024-10-22 22:40:32

是的,您需要缓存控制标头。

Cache-Control: no-cache;max-age=0

Yes, you need cache-control header.

Cache-Control: no-cache;max-age=0
飘逸的'云 2024-10-22 22:40:32

我会尝试添加一个 Pragma: no-cache。应该适用于任何现代浏览器。

http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html

I would try throwing in a Pragma: no-cache. Should work on any modern browser.

http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html

心凉 2024-10-22 22:40:32

出色地。

这里真正的问题是使用 GET 来“做某事”。

Well.

The real problem here is using GET for "do something".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文