使用来自 VB.NET 的身份验证使用 API
Consuming API with authentication from VB.NET
我正在尝试使用需要我使用用户名和密码登录的 API。
我收到的错误是
The remote server returned an error: (403) Forbidden.
身份验证类型为基本。
Public Function ForWhosebug(requestUri As String)
Dim response As String
Dim baseUri = $"http://someapi.example.com/api"
Dim URI As Uri = New Uri(requestUri)
Dim credentialCache As New CredentialCache()
Dim username As String = "username"
Dim password As String = "password"
credentialCache.Add(New System.Uri(baseUri), "Basic", New NetworkCredential(username, password))
Dim request = WebRequest.Create(URI)
request.ContentType = "application/x-www-form-urlencoded"
request.Method = "GET"
request.Credentials = credentialCache
Using responseStream = request.GetResponse.GetResponseStream
Using reader As New StreamReader(responseStream)
response = reader.ReadToEnd()
End Using
End Using
Return response
End Function
我在尝试解决此问题时访问的一些资源。
https://docs.microsoft.com/en-us/dotnet/api/system.net.httprequestheader?view=net-6.0
How to consume an asp.net web api in vb.net application
Value of type 'System.Threading.Tasks.Task(Of String)' cannot be converted to 'String'
Accept self-signed TLS/SSL certificate in VB.NET
Could not establish trust relationship for the SSL/TLS secure channel: The remote certificate is invalid according to the validation procedure
我建议使用 HttpClient
和 Async/Await
:
例如
Public Async Function ForWhosebug(requestUri As String) As Task(Of String)
Using client As New HttpClient()
Dim URI As Uri = New Uri(requestUri)
Dim auth = Encoding.ASCII.GetBytes("username:password1234")
client.DefaultRequestHeaders.Authorization = New Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(auth))
Dim response = Await client.GetAsync(URI)
Dim content = response.Content
Return Await content.ReadAsStringAsync()
End Using
End Function
用法:
Dim result = Await ForWhosebug("http://SomeURL")
' or
' Dim result = ForWhosebug("http://SomeURL").Result
我正在尝试使用需要我使用用户名和密码登录的 API。
我收到的错误是
The remote server returned an error: (403) Forbidden.
身份验证类型为基本。
Public Function ForWhosebug(requestUri As String)
Dim response As String
Dim baseUri = $"http://someapi.example.com/api"
Dim URI As Uri = New Uri(requestUri)
Dim credentialCache As New CredentialCache()
Dim username As String = "username"
Dim password As String = "password"
credentialCache.Add(New System.Uri(baseUri), "Basic", New NetworkCredential(username, password))
Dim request = WebRequest.Create(URI)
request.ContentType = "application/x-www-form-urlencoded"
request.Method = "GET"
request.Credentials = credentialCache
Using responseStream = request.GetResponse.GetResponseStream
Using reader As New StreamReader(responseStream)
response = reader.ReadToEnd()
End Using
End Using
Return response
End Function
我在尝试解决此问题时访问的一些资源。
https://docs.microsoft.com/en-us/dotnet/api/system.net.httprequestheader?view=net-6.0
How to consume an asp.net web api in vb.net application
Value of type 'System.Threading.Tasks.Task(Of String)' cannot be converted to 'String'
Accept self-signed TLS/SSL certificate in VB.NET
Could not establish trust relationship for the SSL/TLS secure channel: The remote certificate is invalid according to the validation procedure
我建议使用 HttpClient
和 Async/Await
:
例如
Public Async Function ForWhosebug(requestUri As String) As Task(Of String)
Using client As New HttpClient()
Dim URI As Uri = New Uri(requestUri)
Dim auth = Encoding.ASCII.GetBytes("username:password1234")
client.DefaultRequestHeaders.Authorization = New Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(auth))
Dim response = Await client.GetAsync(URI)
Dim content = response.Content
Return Await content.ReadAsStringAsync()
End Using
End Function
用法:
Dim result = Await ForWhosebug("http://SomeURL")
' or
' Dim result = ForWhosebug("http://SomeURL").Result