Error when fetching URL through proxy in Go

大兔子大兔子 提交于 2019-12-12 09:53:42

问题


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
}


来源:https://stackoverflow.com/questions/14669958/error-when-fetching-url-through-proxy-in-go

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!