简体   繁体   English

使用 ipv6 地址连接到使用 HttpClient 的服务器(使用 ipv6 地址定义 URI)修复 - Invalid URI: Invalid port specified)

[英]Using ipv6 address to connect to a server using HttpClient (using ipv6 address to define URI) Fixing - Invalid URI: Invalid port specified)

I have simple HttpClient which works perfectly when using ipv4/fqdb/host names (please see below for code snippet).我有一个简单的 HttpClient,它在使用 ipv4/fqdb/host 名称时完美运行(请参阅下面的代码片段)。 However same code doesn't work, the moment I tried to use ipv6 address to connect to server.但是相同的代码不起作用,当我尝试使用 ipv6 地址连接到服务器时。 I probably need to change some configuration settings and able to define uri with ipv6 address.我可能需要更改一些配置设置并能够使用 ipv6 地址定义 uri。

I have looked at msdn and it has the following statement:我看过msdn,它有以下声明:

If the host name is an IPv6 address, the canonical IPv6 address is used.如果主机名是 IPv6 地址,则使用规范 IPv6 地址 ScopeId and other optional IPv6 data are removed - http://msdn.microsoft.com/en-us/library/system.uri.aspx ScopeId 和其他可选的 IPv6 数据被删除 - http://msdn.microsoft.com/en-us/library/system.uri.aspx

Not sure what it means, will try to figure it out.不知道是什么意思,会努力弄明白的。

What can I try to fix the problem?我可以尝试解决什么问题?

Looks like I need to keep ipv6 address in square brackets [enclose it '[]'] http://[fe08::83e7:71e8:1364:0dff%19]:58703/ and looks like everything is working OK now.看起来我需要将 ipv6 地址保留在方括号中 [将其括在 '[]'] http://[fe08::83e7:71e8:1364:0dff%19]:58703/并且看起来现在一切正常。 thanks to How to include ipv6 addresses with (or without) zone indexes in uri for .net remoting?感谢如何在 uri 中包含带(或不带)区域索引的 ipv6 地址以进行 .net 远程处理?

Code:代码:

this.Client = new HttpClient();
**//below line throws UriFormatException (Invalid URI: Invalid port specified)**
this.Client.BaseAddress = new Uri(http://fe08::83e7:71e8:1364:0dff%19:58703/);
this.Client.DefaultRequestHeaders.Accept.Add(
    new MediaTypeWithQualityHeaderValue("application/xml"));

//whereas below code works, when ipv4/fqdn is used...

this.Client = new HttpClient();
this.Client.BaseAddress = new Uri(10.0.0.1:58501);
this.Client.DefaultRequestHeaders.Accept.Add(
    new MediaTypeWithQualityHeaderValue("application/xml"));

You need to specify the URI in the format specified by RFC 2732 .您需要以RFC 2732指定的格式指定 URI。 Basically, wrap the actual IPv6 address in square brackets.基本上,将实际的 IPv6 地址括在方括号中。

The ScopeId you mention is the "%19" part of your example URI.您提到的 ScopeId 是示例 URI 的“%19”部分。 The very high-level, hand-waving description is "it basically identifies which network interface the address corresponds to on the local machine."非常高级,挥手的描述是“它基本上确定了地址对应于本地机器上的哪个网络接口”。 This Super User post and this MSDN article have reasonably understandable detailed descriptions of what it actually means, if you're interested.如果您有兴趣, 这篇超级用户帖子这篇 MSDN 文章对它的实际含义进行了合理易懂的详细描述。

In your case, all you really need to know is that it's meaningless/misleading to include it in a BaseAddress property because the value is only meaningful for your specific machine.在您的情况下,您真正​​需要知道的是,将它包含在 BaseAddress 属性中是毫无意义/误导性的,因为该值仅对您的特定机器有意义。 It doesn't make sense to send it out in HTTP responses since the value has no meaning for remote clients.在 HTTP 响应中发送它没有意义,因为该值对远程客户端没有意义。 That's why, as the documentation you mention points out, HttpClient won't use it even if you include it in the BaseAddress.这就是为什么,正如您提到的文档所指出的,即使您将其包含在 BaseAddress 中,HttpClient 也不会使用它。

The final updated URI would look like:最终更新的 URI 将如下所示:

this.Client.BaseAddress = new Uri(@"http://[ef08::83e7:71e8:1364:0dff]:54502/");

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

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