在 Go 中通过代理获取 URL 时出错 [英] Error when fetching URL through proxy in Go

查看:33
本文介绍了在 Go 中通过代理获取 URL 时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这与其他问题有关.我正在使用以下简单代码通过代理获取 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:

获取http://stackoverflow.com:http:连接代理87.236.233.92:8080时出错:GetServByName:请求的名称有效,但未找到所请求类型的数据.

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,它就可以工作.知道为什么我会收到此错误吗?

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?

推荐答案

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

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
}

这篇关于在 Go 中通过代理获取 URL 时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆