imroc / req

Simple Go HTTP client with Black Magic
https://req.cool
MIT License
4.12k stars 334 forks source link

关于重试设置代理 #296

Closed WyntersN closed 1 month ago

WyntersN commented 8 months ago

能第一次执行的时候也加上代理吗,这段代码好像只能触发重试才能触发代理

var crawlerClient = NewAutoChangeProxyClient()

func NewAutoChangeProxyClient() *req.Client {
  client := req.C()

  client.SetTimeout(5 * time.Second).
    EnableDumpEachRequest().
    SetCommonRetryCount(len(proxies)).
    SetCommonRetryCondition(func(resp *req.Response, err error) bool {
      return err != nil || resp.StatusCode == 403
    }).
    SetCommonRetryHook(func(resp *req.Response, err error) {
      c := client.Clone().SetProxyURL(getProxuyUrl()) // Create a client with proxy
      resp.Request.SetClient(c)                                             // Change the client of request dynamically.
    })
  return client
}
WyntersN commented 8 months ago

期望是 发送请求前 能自己自定义 Hook Request

imroc commented 8 months ago

可以在创建client时就调用一次 SetProxyURL