我想为使用代理创建带有选项true / false的简单函数。 这是我的实际代码:
var torProxy string = "socks5://127.0.0.1:9050"
func getPage(webUrl string, useProxy bool) {
torProxyUrl, err := url.Parse(torProxy)
if err != nil {
log.Fatal("Error parsing Tor proxy URL:", torProxy, ".", err)
}
transport := &http.Transport{
DisableKeepAlives: true,
}
torTransport := &http.Transport{Proxy: http.ProxyURL(torProxyUrl)}
client := &http.Client{Transport: transport, Timeout: time.Second * 5}
if useProxy {
client = &http.Client{Transport: torTransport, Timeout: time.Second * 5}
}
// Make request
resp, err := client.Get(webUrl)
if err != nil {
log.Fatal("Error making GET request.", err)
}
defer resp.Body.Close()
// Read response
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal("Error reading body of response.", err)
}
log.Println(string(body))
log.Println("Return status code:", resp.StatusCode)
}
func main() {
getPage("getPage", false)
}
当我执行我的代码时,返回此错误:2018/04/19 08:28:25 Error making GET request.Get getPage: unsupported protocol scheme ""
exit status 1
我错了吗?
答案 0 :(得分:0)
您在通话中传递的网址无效。变化
func main() {
getPage("getPage", false)
}
到
func main() {
const urlStr = 'https://stackoverflow.com' // Or whatever URL
getPage(urlStr, false)
}