如何强制Golang https GET请求使用特定的IP地址.我想跳过DNS解析,自己提供IP.curl中的类似功能是--Resolve,如下所示.

curl https://domain.com/dir/filename——解析"domain.com:443:10.10.10"

因为这是SSL,所以我希望避免在IP中替换域,如下例所示.

curl https://10.10.10.10/dir/filename——标题"Host:domain.com"

推荐答案

您可以提供一个自定义的Transport.DialContext函数.

func main() {
    dialer := &net.Dialer{
        Timeout:   30 * time.Second,
        KeepAlive: 30 * time.Second,
        // DualStack: true, // this is deprecated as of go 1.16
    }
    // or create your own transport, there's an example on godoc.
    http.DefaultTransport.(*http.Transport).DialContext = func(ctx context.Context, network, addr string) (net.Conn, error) {
        if addr == "google.com:443" {
            addr = "216.58.198.206:443"
        }
        return dialer.DialContext(ctx, network, addr)
    }
    resp, err := http.Get("https://google.com")
    log.Println(resp.Header, err)
}

Go相关问答推荐

../golang/pkg/mod/github.com/wmentor/lemmas@v0.0.6/processor.go:72:9:未定义:令牌.进程

Golang XML密钥名称冲突

如何在Golang中使用ECHO服务器实现Socket.IO服务器

Go 中带有回调的 MiniDumpWriteDump

Golang Gorm Fiber - 如何将定义为别名的名称发送到索引模板?

如果值为 false,gRPC 不返回布尔值

Get 请求在 Thunder 客户端/Postman 中返回数据,但在 Golang 代码中给出空白数据

我如何使用 TOML fixtures 在使用 Go Buffalo 框架的开发环境中为我的数据库 seeder ?

无法使用 gocsv 读取引用字段

Golang 通过接口反映/迭代{}

如何过滤来自 fsnotify 的重复系统消息

如何将一片 map 转换为一片具有不同属性的 struct

如何在 golang revel 中获取动态应用程序配置

如何将具有嵌入式 struct 的 struct 展平为 json

未定义 protoc protoc-gen-go 时间戳

分配空切片而不引用其类型?

GOENV 只能使用 OS 环境设置

不能使用 *T 类型的变量作为参数类型

如何发送带有登录数据的 GET 请求并将 cookie 数据保存到 txt 文件?

Golang 将类型 [N]byte 转换为 []byte