http请求会自动重试吗?

编程入门 行业动态 更新时间:2024-10-28 01:14:12
本文介绍了http请求会自动重试吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试使用GoLang将数据推送到apache服务器.假设我的apache服务器暂时停止了.然后,我的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.

推荐答案

否,您将需要实现自己的重试方法,这是一个基本示例,可以使您有所了解:

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

play.golang/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("non-existent") // response, err = http.Get("google/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\n", data) } }

更多推荐

http请求会自动重试吗?

本文发布于:2023-10-13 14:18:51,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1488188.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:重试   http

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!