
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
792
|
Chapter 14: Web
StringBuilder addressList = new StringBuilder( );
// Get each IP address.
foreach(IPAddress address in addresses)
{
// Append it to the list.
addressList.AppendFormat("IP Address: {0};", address.ToString( ));
}
return addressList.ToString( );
}
// ...
// Writes "IP Address: 208.201.239.37;IP Address: 208.201.239.36;"
Console.WriteLine(HostName2IP("www.oreilly.com"));
Discussion
An IPHostEntry can associate multiple IP addresses with a single hostname via the
AddressList property. AddressList is an array of IPAddress objects, each of which
holds a single IP address. Once the
IPHostEntry is resolved, the AddressList can be
looped over using
foreach to create a string that shows all of the IP addresses for the
given hostname. If the entry cannot be resolved, a
SocketException is thrown.
See Also
See the “DNS Class,” “IPHostEntry Class,” and “IPAddress” topics in the MSDN
documentation.
14.3 Parsing a URI
Problem
You need to split a uniform resource identifier (URI) into its constituent parts.
Solution
Construct a System.Net.Uri object and pass the URI to the constructor. This class
constructor parses out the constituent parts of the URI and allows access to them via
the
Uri properties. You can then display the URI pieces individually, as shown in
Example 14-1.
Example 14-1. ParseURI ...