doukuipei9938
2016-11-29 03:09
浏览 329
已采纳

Golang HTTP响应错误可能是什么?

As in many golang net/http articles, a request returns two values: response and error:

resp, err := http.Get("http://example.com/")
if err != nil {
    // handle error
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)

For http-related errors, it will be returned in resp with status code like 502, 400 etc. What are the possible errors returned? I need to know them before I can handle them.

图片转代码服务由CSDN问答提供 功能建议

与许多golang net / http 文章一样,请求返回两个值:response和 错误:

  resp,err:= http.Get(“ http://example.com/”)
if err!= nil {
 //处理错误\  n} 
defer resp.Body.Close()
body,err:= ioutil.ReadAll(resp.Body)
   
 
 

对于与HTTP相关的错误,它将 以 resp 的形式返回,其状态码为502、400等。返回的可能错误是什么? 在处理它们之前,我需要了解它们。

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • doue2666 2016-11-29 03:26
    已采纳
    1. URL parse error
    2. too much redirect times
    3. tcp connect\writeead timeout
    4. 302 status, but null Location header

    and so on

    You can read the source code in http package. Then you can find all the errors returned by this function http.Get.

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • dparivln22034 2016-11-29 03:24

    Those errors will mostly network related errors, like network timeouts etc. There is no need to handle them differently. You may exit gracefully like,

    resp, err := http.Get("http://google.com/")
        if err != nil {
            fmt.Println(err.Error())
            os.Exit(1)
    }
    
    评论
    解决 无用
    打赏 举报
  • dongtun1683 2016-11-29 12:58

    there are many error responses like :-

    301 (Moved Permanently)
    302 (Found)
    303 (See Other)
    307 (Temporary Redirect) 
    

    better you go and read http.Get

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题