在Go中通过代理获取URL时出现错误

huangapple go评论88阅读模式
英文:

Error when fetching URL through proxy in Go

问题

这与这个问题有关。我正在使用这段简单的代码通过代理获取一个URL:

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))
    }
}

如果我运行这段代码,我会得到这个错误:

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.

我知道代理地址是有效的,如果我通过其他方式通过代理获取URL,它是可以工作的。有任何想法为什么我会得到这个错误吗?

英文:

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

得分: 9

使用http://来指定您的代理,它应该可以工作,例如

proxyUrl, err := url.Parse("http://87.236.233.92:8080")
if err != nil {
    fmt.Println("代理URL无效", err)
    return
}
英文:

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
}

huangapple
  • 本文由 发表于 2013年2月3日 14:07:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/14669958.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定