英文:
What's the bad effect of "http: multiple response.WriteHeader calls"?
问题
我的服务器表现良好,尽管我发现了“http: multiple response.WriteHeader calls”异常。这个异常并不会导致我的服务器崩溃或表现异常。
我已经进行了大量搜索,但只找到了如何解决这个问题的方法。没有文档描述这个异常的不良影响。有人能帮我找出为什么“http: multiple response.WriteHeader calls”是一个异常,以及它引起的不良影响是什么吗?提前感谢。
更新
我已经阅读了源代码这里:
当多次调用WriteHeader
时,它只打印一条日志,然后什么都不做。看起来多次调用WriteHeader
不会导致服务器表现异常。
英文:
My server behaves well though I find the exception of "http: multiple response.WriteHeader calls". This exception doesn't cause my server panic or behaving weirdly.
I have searched a lot but only found how to solve this problem. There's no doc describing the bad effects of the exception. Could someone help me find why "http: multiple response.WriteHeader calls" is an exception and what the bad effects are that it causes? Thanks in advance.
UPDATE
I have read the source code here:
When calling WriteHeader
multitimes,it only prints a log and then does nothing. It seems that calling WriteHeader
multitimes doesn't cause the server behaves weiredly.
答案1
得分: 3
你只能在将响应发送给客户端之前写入一次响应头。因为你的代码试图在响应已发送给客户端后写入响应头,所以客户端将永远看不到你试图发送的最终响应。
要么你的代码存在错误,客户端收到了你稍后修改的错误响应,要么在正确的响应头被写入后,你执行了错误的代码。无论哪种情况,这都是一个错误。
英文:
You can only write a response header once before it is sent to the client. Because your code is attempting to write a response header after it has been sent to the client, the client will never see the final response you are trying to send.
You either have a bug where the client is getting the incorrect response which you later revised, or you have a bug where you are executing the wrong code after the correct response header has been written. Either way it's a bug.
答案2
得分: 2
坏处是实际结果很可能与你期望的不同。如果你在第10行调用WriteHeader(200)
,然后在第50行再次在同一个ResponseWriter
上调用WriteHeader(404)
,你期望的响应代码是什么?是404
还是200
?
WriteHeader
会忽略后续的调用,并通过记录该消息来告诉你,所以当你得到的是200
而不是你期望的404
时,你只需查看服务器日志,就能知道发生了什么。这个消息是为了帮助你的。
英文:
The bad effect is that the actual result will most probably be different from what you expect. If you call WriteHeader(200)
on line 10, and later on line 50 you call, on the same ResponseWriter
, WriteHeader(404)
what do you expect the response code to be? 404
, or 200
?
WriteHeader
ignores subsequent calls and tells you about it by logging that message, so that when you're getting 200
instead of 404
as you probably expect, you just take a look at the server logs and you know what's up. The message is there to help you.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论