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.

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

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!