简体   繁体   English

net/http 不适用于 ipv6 地址

[英]net/http is not working with ipv6 address

I'm trying to make a "GET" request to a local ipv6 address (discovered via mdns).我正在尝试向本地 ipv6 地址(通过 mdns 发现)发出“GET”请求。 The issue is that the request fails .问题是请求失败 Why is that?这是为什么?

package main

import (
    "fmt"
    "net/http"
)

func main() {
    url := "http://[fe80::cf5:b02f:27c3:2abe]:7070/kw/"
    rsp, err := http.Get(url)
    fmt.Println(rsp, err)
}

Output输出

<nil> Get http://[fe80::cf5:b02f:27c3:2abe]:7070/kw/: dial tcp [fe80::cf5:b02f:27c3:2abe]:7070: socket: Unknown protocol

You're trying to connect to an IPv6 link-local address, but these require a scope ID to be valid.您正在尝试连接到 IPv6 链接本地地址,但这些地址需要有效范围 ID。 The scope ID is usually the interface name or number (eg enp4s0 or wlp5s0 on Linux; 3 or 12 on Windows).范围 ID 通常是接口名称或编号(例如,Linux 上的enp4s0wlp5s0 ;Windows 上的312 )。

The % also needs to be escaped, because it appears in a URL. %也需要转义,因为它出现在 URL 中。 So it will appear as %25 .所以它将显示为%25

So your URL should appear as:所以你的 URL 应该显示为:

url := "http://[fe80::cf5:b02f:27c3:2abe%25interface]:7070/kw/"

Where interface is your scope ID.其中interface是您的范围 ID。


Also note well that while net/http will happily make HTTP requests to IPv6 link-local address literals, web browsers will not.还要注意,虽然net/http乐意向 IPv6 链接本地地址文本发出 HTTP 请求,但 Web 浏览器不会。 They will, however, connect to names which resolve to properly scoped link-local addresses.他们将,但是,连接到哪个决心正常范围的链路本地地址名称 They will also connect to global (and ULA) IPv6 address literals.它们还将连接到全局(和 ULA)IPv6 地址文字。 If you will eventually have a web browser involved in your project, keep this in mind.如果您最终将在您的项目中使用 Web 浏览器,请记住这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM