简体   繁体   中英

Using proxies with net/http in GO

I'm trying to use proxies with the net/http package. My proxy is a rotating proxy with a Username, password, Proxy address and a port. I tried setting it as an environment variable using os.setEnv() as well as adding it in my windows 10 env variables but turns out maybe windows does not support user-pass authenticated proxies. I tried the http transport method too but could not get it to work

func SetProxy() *http.Client {
    cfg := GetConfig()
    if cfg.UseProxy {
        proxyUrl, err := url.Parse("http://" + cfg.Proxy)
        if err != nil {
            panic(err)
        }
        myClient := &http.Client{Transport: &http.Transport{Proxy: http.ProxyURL(proxyUrl)}}
        return myClient
    }
    return &http.Client{}
}

Please if someone could point me to a tutorial or some documentation to use proxies specifically proxies with user-pass auth!

note: I used this format everywhere: username:password@proxyaddress:port

As a debug attempt, you can print the client.Transport.Proxy function; if it's not nil, call it and print what it returns. If Proxy is not nil and returns not nil *URL , then proxy IS used.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM