Dim userName As String
userName = Console.ReadLine()
Console.WriteLine("Please enter your password. ")
Dim password As String
password = Console.ReadLine()
Dim client As New WebClient
client.Credentials = New NetworkCredential(userName, password)
Notice that in this code the credentials are obtained by prompting the user to enter them on the command line. System.Net also provides a way of using the default credentials of the account under which the application is running. You do so by setting the Credentials property on a class equal to CredentialCache.DefaultCredentials. Whenever possible, developers should use default credentials instead of prompting the user or reading credentials from some other source because default credentials are obtained through the security systems integrated into the underlying operating system.
DNS can be used to represent nodes on the network with human-readable names rather than IP addresses. For example, imagine trying to remember a string of characters such as www.contoso.com instead of a series of numbers such as 220.127.116.11. One feature provided by DNS is the ability to have multiple IP addresses assigned to one host name. You can even have different IP address types (for example, IPv4 and IPv6) assigned to the same host. It’s often useful to be able to resolve a name to the list of IP addresses associated with that name.
Access to DNS is enabled and controlled in System.Net through the Dns, DnsPermission, and IPHostEntry classes. DNS is covered in more detail in Chapter 7. The following example shows the basic steps involved in resolving a name and outputting the corresponding IP addresses to the console using System.Net.
static void ResolveName(string name)
IPHostEntry hostInfo = Dns.Resolve(name);
foreach(IPAddress address in hostInfo.AddressList)
Sub ResolveName(ByVal name As String)
Dim hostInfo As IPHostEntry
hostInfo = Dns.Resolve(name)
Dim address As IPAddress
For Each address In hostInfo.AddressList
Catch ex As Exception