如何正确填充InetSocketAddress

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

How to properly populate InetSocketAddress

问题

我正尝试填充主机头。我已为我的示例插入了虚拟数据,但我想知道我是否填充正确。当检查地址的值时,它似乎与我预期的不同。我预期的是[域名]:[端口],但我看到的是[域名]/[IP地址]:[端口]。这正常吗?

我正在使用以下代码行:

InetSocketAddress address = new InetSocketAddress("sample.com", 8080);

这是之后变量的值:

address = {InetSocketAddress} sample.com/173.230.129.147:8080
英文:

I am trying to populate the Host header. I have inserted dummy data for the my example but I am wondering it I am populating this correctly. When examine the value of address it appears unlike I would expect. I would expect [domain]:[port] but I am seeing [domain]/[ip address]:[port]. Is this normal?

Here is the line I am using

InetSocketAddress address = new InetSocketAddress("sample.com", 8080);

Here is the variable value afterwards:

address = {InetSocketAddress} sample.com/173.230.129.147:8080

Thanks

答案1

得分: 0

是的,这是正常的。 InetSocketAddress 会尝试自动将主机名解析为 IP 地址。

根据 API 规范

此类实现了 IP 套接字地址(IP 地址 + 端口号),它还可以是一对(主机名 + 端口号),在这种情况下,将尝试解析主机名

如果您想要创建不将主机名解析为 IP 地址的 InetSocketAddress,您可以使用 createUnresolved 工厂方法。

jshell> InetSocketAddress.createUnresolved("example.com", 8080);
$20 ==> example.com:8080
英文:

Yes this is normal. InetSocketAddress tries to resolve the host name to an IP address automatically.

From the API specification:

> This class implements an IP Socket Address (IP address + port number) It can also be a pair (hostname + port number), in which case an attempt will be made to resolve the hostname.

If you want to create the InetSocketAddress without resolving the host name to an IP address, you can use the createUnresolved factory method.

jshell> InetSocketAddress.createUnresolved("example.com", 8080);
$20 ==> example.com:8080

huangapple
  • 本文由 发表于 2020年4月7日 23:47:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/61084010.html
匿名

发表评论

匿名网友

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

确定