本文整理汇总了VB.NET中System.Net.CredentialCache.GetCredential方法的典型用法代码示例。如果您正苦于以下问题:VB.NET CredentialCache.GetCredential方法的具体用法?VB.NET CredentialCache.GetCredential怎么用?VB.NET CredentialCache.GetCredential使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Net.CredentialCache
的用法示例。
在下文中一共展示了CredentialCache.GetCredential方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的VB.NET代码示例。
示例1: GetPage
Public Shared Sub GetPage(url As String, userName As String, password As String, domainName As String)
Try
Dim myCredentialCache As New CredentialCache()
' Dummy names used as credentials
myCredentialCache.Add(New Uri("http://microsoft.com/"), "Basic", New NetworkCredential("user1", "passwd1", "domain1"))
myCredentialCache.Add(New Uri("http://msdn.com/"), "Basic", New NetworkCredential("user2", "passwd2", "domain2"))
myCredentialCache.Add(New Uri(url), "Basic", New NetworkCredential(userName, password, domainName))
' Creates a webrequest with the specified url.
Dim myWebRequest As WebRequest = WebRequest.Create(url)
' Call 'GetCredential' to obtain the credentials specific to our Uri.
Dim myCredential As NetworkCredential = myCredentialCache.GetCredential(New Uri(url), "Basic")
Display(myCredential)
myWebRequest.Credentials = myCredential 'Associating only our credentials
' Sends the request and waits for response.
Dim myWebResponse As WebResponse = myWebRequest.GetResponse()
' Process response here.
Console.WriteLine(ControlChars.Cr + "Response Received.")
myWebResponse.Close()
Catch e As WebException
If Not (e.Response Is Nothing) Then
Console.WriteLine(ControlChars.Lf + ControlChars.Cr + "Failed to obtain a response. The following error occurred : {0}", CType(e.Response, HttpWebResponse).StatusDescription)
Else
Console.WriteLine(ControlChars.Lf + ControlChars.Cr + "Failed to obtain a response. The following error occurred : {0}", e.Status)
End If
Catch e As Exception
Console.WriteLine(ControlChars.Cr + "The following exception was raised : {0}", e.Message)
End Try
End Sub
Public Shared Sub Display(ByVal credential As NetworkCredential)
Console.WriteLine("The credentials are: ")
Console.WriteLine(ControlChars.Cr + "Username : {0} ,Password : {1} ,Domain : {2}", credential.UserName, credential.Password, credential.Domain)
End Sub