简体   繁体   English

“ http:多重响应。WriteHeader调用”的不良影响是什么?

[英]What's the bad effect of “http: multiple response.WriteHeader calls”?

My server behaves well though I find the exception of "http: multiple response.WriteHeader calls". 我的服务器运行良好,尽管我发现“ http:多重响应.WriteHeader调用”例外。 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? 有人可以帮我找到原因,为什么“ http:多重响应.WriteHeader调用”是一个异常,它引起的不良影响是什么? Thanks in advance. 提前致谢。

UPDATE 更新

I have read the source code here : 在这里阅读了源代码:

在此处输入图片说明

When calling WriteHeader multitimes,it only prints a log and then does nothing. WriteHeader调用WriteHeader ,仅打印日志,然后不执行任何操作。 It seems that calling WriteHeader multitimes doesn't cause the server behaves weiredly. 似乎多次调用WriteHeader不会导致服务器异常运行。

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. 无论哪种方式,它都是一个错误。

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? 如果在第10行上调用WriteHeader(200) ,然后在第50行上调用,则在同一ResponseWriter上调用WriteHeader(404) ,您期望响应代码是什么? 404 , or 200 ? 404还是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. WriteHeader忽略后续调用,并通过记录该消息来告诉您有关此消息的信息,这样,当您获得200而不是您可能期望的404 ,您只需查看服务器日志即可知道发生了什么。 The message is there to help you. 该消息在那里可以为您提供帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM