By GlobalProxyIP Editorial Team
In Go, proxy routing belongs to the HTTP transport. Colly adds crawling behavior on top of an HTTP client, so choosing a collector does not eliminate the need to configure and verify that transport. The useful comparison is configuration scope: which component owns routing, timeouts, cookies, and response processing?
Choose who owns the network configuration
| Approach | Configuration owner | Good fit | Check before sending |
|---|---|---|---|
net/http with Transport.Proxy |
Your application | API calls and explicit response processing | The request uses the configured client |
Colly SetProxy |
The collector | A simple collector with one proxy | The URL scheme and endpoint are correct |
Colly SetProxyFunc |
A routing callback | Selecting among approved endpoints | The callback is concurrency-safe and can report no usable endpoint |
Colly SetClient |
Your application-provided HTTP client | Sharing explicit transport policy | Collector settings do not unintentionally replace the client |
A fixed http.ProxyURL selects the configured proxy. http.ProxyFromEnvironment instead evaluates the documented environment rules, including exclusions. These are different policies. Do not assume that changing an environment variable overrides a transport already configured with a fixed URL.
Compare two paths with the same client
Use Go with modules and Colly v2.2.0. Save this program as main.go, then run go mod init proxycheck, go get github.com/gocolly/colly/v2@v2.2.0, and go mod tidy. Supply an HTTP PROXY_URL and an authorized TARGET_URL through controlled runtime configuration. A credential-bearing URI must not be printed or committed; encode username and password components if they contain URI delimiters.
package main
import (
"fmt"
"io"
"net/http"
"net/url"
"os"
"time"
"github.com/gocolly/colly/v2"
)
func main() {
if len(os.Args) != 2 { panic("usage: go run . net|colly") }
proxyURL, err := url.Parse(os.Getenv("PROXY_URL"))
if err != nil || proxyURL == nil || proxyURL.Scheme != "http" || proxyURL.Hostname() == "" || proxyURL.Port() == "" {
panic("PROXY_URL must contain an HTTP proxy host and port")
}
target, err := url.Parse(os.Getenv("TARGET_URL"))
if err != nil || target == nil || target.Hostname() == "" || (target.Scheme != "http" && target.Scheme != "https") { panic("invalid TARGET_URL") }
transport := http.DefaultTransport.(*http.Transport).Clone()
transport.Proxy = http.ProxyURL(proxyURL)
defer transport.CloseIdleConnections()
client := &http.Client{Transport: transport, Timeout: 20*time.Second,
CheckRedirect: func(_ *http.Request, _ []*http.Request) error {return http.ErrUseLastResponse}}
if os.Args[1] == "net" {
resp, err := client.Get(target.String())
if err != nil { fmt.Printf("request_failed type=%T\n",err);os.Exit(1) }
defer resp.Body.Close()
body, err := io.ReadAll(io.LimitReader(resp.Body, 1048577))
if err != nil || len(body)>1048576 {panic("body read failed or exceeded 1 MiB")}
fmt.Printf("status=%d bytes=%d\n",resp.StatusCode,len(body))
if resp.StatusCode != 200 {os.Exit(1)}
} else if os.Args[1] == "colly" {
c:=colly.NewCollector(colly.AllowedDomains(target.Hostname()),colly.MaxBodySize(1048576))
c.SetClient(client)
failed:=false
c.OnResponse(func(r *colly.Response){fmt.Printf("status=%d bytes=%d\n",r.StatusCode,len(r.Body));if r.StatusCode!=200{failed=true}})
c.OnError(func(_ *colly.Response,e error){failed=true;fmt.Printf("request_failed type=%T\n",e)})
if err:=c.Visit(target.String());err!=nil{failed=true}
if failed{os.Exit(1)}
} else {panic("choose net or colly")}
}
Run go run . net, then go run . colly. Use a small controlled target that returns 200. The program limits body size, disables automatic redirects, and prints only status and byte count. These observations compare request handling; use target-side logs or a controlled echo response to confirm the actual source address.
Explain differences before changing the proxy
If both paths fail, investigate the shared endpoint, authentication, network path, and certificate trust. If only Colly fails, inspect its allowed domains, robots handling, maximum body size, and visited-URL state. A URL rejected before network activity is not evidence that the proxy rejected it.
Colly normally avoids visiting the same URL repeatedly. Enable AllowURLRevisit only when the test actually requires repeated visits. For asynchronous collectors, call Wait before interpreting final counters. Keep the target’s allowed rate and access conditions in the test design.
Reuse does not define exit rotation
Go clients and transports are designed for reuse. Closing idle connections does not terminate active ones, and creating a new connection does not guarantee a new proxy exit. A proxy provider’s session or rotation policy is separate from the collector lifecycle.
When implementing endpoint replacement, stop assigning new work to the old configuration, create the replacement client, and drain or cancel old work according to the task deadline. Do not mutate a live transport’s fields concurrently with requests.
Frequently asked questions
Should an HTTPS target use an HTTPS proxy URL?
Not necessarily. An HTTP proxy can carry HTTPS through CONNECT when it supports the destination. The proxy URI scheme describes the connection to the proxy, not the target URL.
Can I infer proxy use from HTTP 200?
No. Check the target-observed source and relevant proxy logs as well as the content. A successful response alone does not identify the route.
Does Colly execute page JavaScript?
Its HTTP scraping and HTML callbacks do not provide a browser execution environment. A page with client-rendered data requires a suitable authorized data source or a separate rendering workflow.
References
Related concepts
For protocol selection, compare HTTP proxy forwarding and SOCKS5 negotiation; for HTTPS through an HTTP endpoint, review the separate CONNECT tunnel stage.