在http.Client
中自定义Transport
,设置Proxy
即可,目前网上存在很多代理ip网站,本人也写过一些代理ip网址的爬取,见 GitHub
// 如果需要代理验证,那么如下进行设置
// 否则直接设置为url.Parse("http://inproxy.sjtu.edu.cn:8000")
uri, err := url.Parse("http://username:password@inproxy.sjtu.edu.cn:8000")
if err != nil{
log.Fatal("parse url error: ", err)
}
log.Println(uri.User)
client := http.Client{
Transport: &http.Transport{
// 设置代理
Proxy: http.ProxyURL(uri),
},
}
resp, err := client.Get("http://www.baidu.com")
if err != nil{
log.Fatal(err)
}
defer resp.Body.Close()
data, _ := ioutil.ReadAll(resp.Body)
log.Println(string(data))
除此之外,你也可以设置环境变量,比如
HTTP_PROXY, HTTPS_PROXY, NO_PROXY
然后修改上方的代码为
client := http.Client{
Transport: &http.Transport{
// 设置代理,从环境变量中获取
Proxy: http.ProxyFromEnvironment,
},
}
具体的内容查看文档如下即可
// ProxyFromEnvironment returns the URL of the proxy to use for a
// given request, as indicated by the environment variables
// HTTP_PROXY, HTTPS_PROXY and NO_PROXY (or the lowercase versions
// thereof). HTTPS_PROXY takes precedence over HTTP_PROXY for https
// requests.
//
// The environment values may be either a complete URL or a
// "host[:port]", in which case the "http" scheme is assumed.
// An error is returned if the value is a different form.
//
// A nil URL and nil error are returned if no proxy is defined in the
// environment, or a proxy should not be used for the given request,
// as defined by NO_PROXY.
//
// As a special case, if req.URL.Host is "localhost" (with or without
// a port number), then a nil URL and nil error will be returned.