Error when fetching URL through proxy in Go

2019-05-11 22:50发布

问题:

This is related to this other question. I'm fetching a URL through a proxy using this simple code:

package main

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

func main() {
    proxyUrl, err := url.Parse("87.236.233.92:8080")
    httpClient := &http.Client { Transport: &http.Transport { Proxy: http.ProxyURL(proxyUrl) } }
    response, err := httpClient.Get("http://stackoverflow.com")
    if err != nil {
        fmt.Println(err.Error())
    } else {
        body, _ := ioutil.ReadAll(response.Body)
        fmt.Println("OK: ", len(body))
    }
}

If I run this code, I am getting this error:

Get http://stackoverflow.com: http: error connecting to proxy 87.236.233.92:8080: GetServByName: The requested name is valid, but no data of the requested type was found.

I know that the proxy address is valid and if I fetch the URL through the proxy by other means it work. Any idea why I'm getting this error?

回答1:

Specify your proxy with http:// in and it should work, eg

proxyUrl, err := url.Parse("http://87.236.233.92:8080")
if err != nil {
    fmt.Println("Bad proxy URL", err)
    return
}


标签: url proxy go fetch