【发布时间】:2022-02-10 06:54:44
【问题描述】:
我正在尝试使用需要我使用用户名和密码登录的 API。
我收到的错误是
远程服务器返回错误:(403) Forbidden。
身份验证类型为基本。
Public Function ForStackOverFlow(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
vb.net Task(Of HttpResponseMessage) to HttpResponseMessage
Value of type 'System.Threading.Tasks.Task(Of String)' cannot be converted to 'String'
SSL TLS 1.2 Channel creation VB.net HTTPS WebRequest
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
【问题讨论】:
-
什么是API认证类型? JWP?曲奇饼?基本的?
-
身份验证类型为基本,我编辑了问题以使其更清晰。
-
很少有经过身份验证的 API 将允许原始 http 访问。您可能需要 https://。很少有 API 也使用基本身份验证。您几乎总是需要生成某种令牌。
标签: vb.net api authentication