go - How to set which IP to use for a HTTP request? -
i dont know if it's possible std lib not state current address being used:
http://golang.org/pkg/net/http/
resp, err := http.get("http://example.com/") if err != nil { // handle error } defer resp.body.close() body, err := ioutil.readall(resp.body) what i'm trying set source address http request, why? because don't want use primary ip address kind of stuff...
you can set custom dialer in client's transport.
// create transport http.defaulttransport, specified localaddr transport := &http.transport{ proxy: http.proxyfromenvironment, dialcontext: (&net.dialer{ timeout: 30 * time.second, keepalive: 30 * time.second, localaddr: localaddr, dualstack: true, }).dialcontext, maxidleconns: 100, idleconntimeout: 90 * time.second, tlshandshaketimeout: 10 * time.second, expectcontinuetimeout: 1 * time.second, } client := &http.client{ transport: transport, }
Comments
Post a Comment