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?