duankeng9477 2018-06-04 08:57
浏览 679
已采纳

http请求会自动重试吗?

I am trying to push my data to apache server using GoLang. Suppose my apache server is temporarily stopped. Then will my http request retry automatically. I am using this statement

resp, err := http.DefaultClient.Do(req)
    if err != nil {
        return errors.Wrap(err, "http request error")
    }

I am unable to proceed further coz what is think is my execution is stuck here. And I am repeatedly getting this error.

  • 写回答

1条回答 默认 最新

  • dongroufan6846 2018-06-04 09:24
    关注

    No, you will need to implement your own retry method, this is a basic example that could give you an idea:

    https://play.golang.org/p/_o5AgePDEXq

    package main
    
    import (
        "fmt"
        "io/ioutil"
        "log"
        "net/http"
    )
    
    func main() {
        var (
            err      error
            response *http.Response
            retries  int = 3
        )
        for retries > 0 {
            response, err = http.Get("https://non-existent")
            // response, err = http.Get("https://google.com/robots.txt")
            if err != nil {
                log.Println(err)
                retries -= 1
            } else {
                break
            }
        }
        if response != nil {
            defer response.Body.Close()
            data, err := ioutil.ReadAll(response.Body)
            if err != nil {
                log.Fatal(err)
            }
            fmt.Printf("data = %s
    ", data)
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?