Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
77 views
in Technique[技术] by (71.8m points)

c# - Host vs DnsSafeHost

I need to get the host out of the Request object. Which property should I use and why?

From MSDN:

Uri.DnsSafeHost Property

A String that contains the unescaped host part of the URI that is suitable for DNS resolution; or the original unescaped host string, if it is already suitable for resolution.

vs

Uri.Host Property

A String that contains the host name. This is usually the DNS host name or IP address of the server.

My testing has been with the ASP.NET Development Server. Both of these always return localhost. Even when I put in 127.0.0.1, both return localhost.

Reading on, the DnsSafeHost property will handle IPv6 addresses, as well as Unicode to ASCII conversion if needed. It can also account for IRI and IDN. Even though I currently don't care about these things, should I just use the DnsSafeHost property to be safe?

question from:https://stackoverflow.com/questions/1224587/host-vs-dnssafehost

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Apply DnsSafeHost to the URL if you intend to send the resulting host name to a DNS server on your own. DNS was originally an ASCII-only protocol (and a subset of ASCII at that). Present day URL's allow character sequences in the host portion of a URL that cannot be sent directly to a DNS server. Examples are:

  • bracket-enclosed IPv6 addresses. DnsSafeHost drops the brackets.

  • Non-ASCII, non-English domain names. DnsSafeHost (optionally) uses RFC 3987 IRI to normalize the characters, and converts non-ASCII character to IDN, Punycode encoding.

  • Who knows what URLs will allow in the future? The application may well outlive our assumptions of what's okay in a URL. DnsSafeHost adds a degree of resilience there.

If you need to display the host name to the user, use the Host property instead.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...