douchuang4181 2014-11-16 20:34
浏览 415
已采纳

多个响应。Go中的WriteHeader调用

My Go server is handling requests I first make a call to response.WriteHeader() in order to set the status code for my response. After that I begin writing bytes into the response body. If the browser cancels the request while i'm copying the bytes, I get an error:

write tcp [::1]:52319: broken pipe

My code detects this error, then calls http.Error(). This calls response.WriteHeader() again.

This appears to be a problem, but I'm not sure. Can this be avoided? How do I avoid calling response.WriteHeader() again when an error occurs while writing to to the response body?

Thanks!

  • 写回答

1条回答

  • duan33360 2015-04-22 07:47
    关注

    The call to .WriteHeader() starts sending the response to the client over the net. Once the response is on its way, there is no way to back. The only thing you can do is to log the error locally (to let the server administrator know) or maybe to just fail silently.

    The 'Error()' function is used to send a complete HTTP (error) response, so you can only use this to replace sending your own response, not in addition to it.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器