doulianglou0898 2019-02-13 09:20
浏览 54

Web请求后,golang更改代理

I was able to make a get request with the net/http package using a specific proxy with the code below:

proxyURL, err := url.Parse("http://111.222.333.444:80")

    if err != nil {
        fmt.Println("Bad proxy URL", err)
        return
    }

client := &http.Client{Transport: &http.Transport{Proxy: http.ProxyURL(proxyURL)}}
req, _ := http.NewRequest("GET", "https://www.google.com", nil)
res, err := client.Do(req)
    if err != nil {
        fmt.Println("Error")
    }
else {
    defer res.Body.Close()
    respBody, _ := ioutil.ReadAll(res.Body) //read the response
    fmt.Println(string(respBody))
    }

Reading the docs is written that

ProxyURL returns a proxy function (for use in a Transport) that always returns the same URL.

But how should I make another get request after that one, in the same thread with a different proxy? am I supposed to create another Transport object with a different proxy setting? but I find this way to be very slow. What I'm looking for is to reuse the Transport and Client objects and changing only the proxy url.

What's the best way to do it?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
    • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
    • ¥20 有关区间dp的问题求解
    • ¥15 多电路系统共用电源的串扰问题
    • ¥15 slam rangenet++配置
    • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
    • ¥15 ubuntu子系统密码忘记
    • ¥15 保护模式-系统加载-段寄存器
    • ¥15 电脑桌面设定一个区域禁止鼠标操作
    • ¥15 求NPF226060磁芯的详细资料