Skip to content

Updated

Golang HTTP Proxy:
net/http Client With Authentication

  1. Build a *url.URL with url.UserPassword(login, password), pass it to http.ProxyURL and set it as Transport.Proxy.
  2. The scheme picks the protocol: http:// for an HTTP proxy, socks5:// for SOCKS5; both are supported by net/http itself.
  3. On a rotating gateway a new IP arrives with a new connection, so keep-alive decides how often it changes.

Check it right after buyingExample output

curl -x http://USER:PASS@HOST:PORT https://ipinfo.io/json

ip
203.0.113.24
country
US
Matches the country you picked
Day 15
203.0.113.24
Same address for the whole term
Example addresses (203.0.113.0/24), not real exits
01 / 12The short answer: Transport.Proxy

A short guide to Transport.Proxy: where the login goes, how SOCKS5 works in the standard library, and why keep-alive decides when a rotating IP changes.

The short answer: Transport.Proxy

Go's http.Transport has a Proxy field. The Go source says the proxy type follows the URL scheme: http, https, socks5 and socks5h are supported, a missing scheme means http, and a username and password in the URL are sent to the proxy (as a Proxy-Authorization header for HTTP proxies). No extra package is needed for SOCKS5.

Building the URL with url.UserPassword has one benefit over pasting LOGIN:PASSWORD into a string: characters such as @ or : in a password are escaped for you.

HTTP proxy with login

package main

import (
	"fmt"
	"io"
	"net/http"
	"net/url"
)

func main() {
	proxyURL := &url.URL{
		Scheme: "http",
		User:   url.UserPassword("LOGIN", "PASSWORD"),
		Host:   "HOST:2222",
	}
	client := &http.Client{
		Transport: &http.Transport{Proxy: http.ProxyURL(proxyURL)},
	}
	resp, err := client.Get("https://ipinfo.io/json")
	if err != nil {
		panic(err)
	}
	defer resp.Body.Close()
	body, _ := io.ReadAll(resp.Body)
	fmt.Println(string(body))
}

Replace HOST, LOGIN and PASSWORD with the values from My assets. 2222 is the HTTP port.

SOCKS5 proxy with login

proxyURL := &url.URL{
	Scheme: "socks5",
	User:   url.UserPassword("LOGIN", "PASSWORD"),
	Host:   "HOST:3333",
}
client := &http.Client{
	Transport: &http.Transport{Proxy: http.ProxyURL(proxyURL)},
}

Same client, SOCKS5 scheme and port 3333. In the Go source, socks5 is treated the same as socks5h: the target name is resolved by the proxy.

Environment variables

http.DefaultTransport uses http.ProxyFromEnvironment, which reads HTTP_PROXY, HTTPS_PROXY and NO_PROXY (or the lower-case names) and picks the one that matches the request scheme. A request to localhost is never proxied. Once you set Transport.Proxy yourself, the variables are no longer consulted for that client.

http.ProxyURL, by its documentation, returns a function that always gives the same URL, so every request on that client uses that one gateway.

Connection reuse decides when the IP changes

A Transport keeps idle connections and reuses them; the default for MaxIdleConnsPerHost is 2. On a rotating gateway the IP is chosen when a connection is opened, so requests that ride one reused connection leave from the same IP. DisableKeepAlives makes the Transport use a connection for a single request only.

Fresh connection or reused connection

// A new connection for every request: on a rotating gateway this asks for a new IP each time
fresh := &http.Client{Transport: &http.Transport{
	Proxy:             http.ProxyURL(proxyURL),
	DisableKeepAlives: true,
}}

// One shared client: connections are kept and reused, so the IP stays the same while they live
kept := &http.Client{Transport: &http.Transport{Proxy: http.ProxyURL(proxyURL)}}

Use the fresh client when each request should ask for a new IP, the kept client when a group of requests must stay on one IP. Test with ipinfo.io before relying on either.

Proxy URL schemes in net/http

Proxy URL schemes in net/http
ItemPort on proxy.proxy302.comHow the login reaches the proxy
http://2222Username and password from the URL go in a Proxy-Authorization header
socks5:// or socks5h://3333Username and password from the URL are used in the SOCKS5 login step
No schemeTreated as http, so use 2222Same as http://

From the comment on Transport.Proxy and the SOCKS5 branch in net/http transport.go, read on 20 November 2026; ports from Proxy302 product facts.

How to do it with Proxy302

Each snippet needs four values from a Proxy302 proxy: host, port, login and password. For scraping, rotating residential traffic is billed per GB (Budget line US$1.50 per GB, Premium line US$5 per GB); HTTP is on port 2222 and SOCKS5 on port 3333 of proxy.proxy302.com, and the same login works for both. Without a session, each new connection gets a new IP, and a connection you keep open keeps its IP.

When the other side needs one fixed address, a datacenter IP lease costs US$0.25 for 1 day or US$3.99 for 30 days, and you choose HTTP or SOCKS5 when you buy it. There is no free trial; the smallest purchase is 1 GB of rotating residential traffic or one IP for one day.

How to do it with Proxy302: steps

  1. Step 1

    Open the rotating residential page, choose the line, then click Review cost and generate. For a fixed IP, buy a datacenter IP on the datacenter pay-per-IP page instead.

  2. Step 2

    Open My assets and copy HOST, PORT, USERNAME and PASSWORD exactly as they are shown there.

  3. Step 3

    Paste them into the code in this guide, using the port that matches the protocol: 2222 for HTTP, 3333 for SOCKS5.

  4. Step 4

    Run the code against https://ipinfo.io/json and check that the country and org fields describe the proxy, not your own connection.

Troubleshooting

407 Proxy Authentication Required

Check LOGIN and PASSWORD are exactly as in My assets; the login is in the proxy URL, not set with Request.SetBasicAuth, which would address the target site.

Fix Move the credentials into the proxy URL with url.UserPassword.

Stop when If curl with the same login also returns 407, contact Support with the order number.

proxyconnect tcp: dial tcp ... connect: connection refused, or a timeout

Check HTTP scheme with port 2222, or socks5 with port 3333; HOST copied correctly.

Fix Fix the scheme and port pair, then run again; see the connection guide for the other checks.

Stop when If all ports time out from your network, test from another network before changing code.

ipinfo shows your own IP

Check The Transport was built without Proxy, or a different client is used for the request; NO_PROXY matches the host.

Fix Use the client you configured for the call and print the Transport settings.

Stop when If DefaultTransport is used by a library, give that library your client instead of relying on variables.

FAQ

How do I set a proxy in Go without environment variables?

Set Transport.Proxy to http.ProxyURL(proxyURL) on the http.Transport you give to http.Client. The documentation says ProxyURL returns a function that always returns the same URL.

Where do the proxy username and password go?

In the proxy URL, for example through url.UserPassword. The Go source says a userinfo part in the proxy URL is passed on as the proxy login.

Does Go support SOCKS5 proxies in net/http?

Yes. The Transport.Proxy comment lists socks5 and socks5h, and the source contains a SOCKS5 username and password step, so no extra package is needed for a plain SOCKS5 proxy.

Why do several requests leave from the same IP on a rotating proxy?

They share a keep-alive connection, and the IP is chosen when the connection opens. Create a Transport with DisableKeepAlives set to true, or a new client, when you want a new connection.

Further reading

Setup guides Proxy302 publishes on GitHub on the same topic:

Reference links

3
  1. Go source: net/http transport.go

    Official documentation · Confirmed on:

    Read on 20 November 2026: the comment on Transport.Proxy (schemes http, https, socks5, socks5h; userinfo sent as Proxy-Authorization), ProxyFromEnvironment (HTTP_PROXY, HTTPS_PROXY, NO_PROXY, localhost exception), DisableKeepAlives, DefaultMaxIdleConnsPerHost = 2, and the SOCKS5 username and password branch.

  2. Go package documentation: net/http

    Official documentation · Confirmed on:

    Read on 20 November 2026: ProxyFromEnvironment, ProxyURL ("always returns the same URL") and DefaultMaxIdleConnsPerHost.

  3. Proxy302 product facts and price list

    Product observation · Confirmed on:

    Product facts file of the site (values checked against the console, the backend and the price table): products, prices in US$ (1 PTC = US$1), lease terms, protocols and ports, quantity limits, no free trial.

Ready to try it?

Create an account, top up once, then pick rotating residential, static ISP or datacenter IPs and have one running in your browser within minutes.