【问题标题】:Consuming API with authentication from VB.NET使用来自 VB.NET 的身份验证的 API
【发布时间】: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


【解决方案1】:

我建议使用HttpClientAsync/Await

例如

    Public Async Function ForStackOverFlow(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 ForStackOverFlow("http://SomeURL")
' or 
' Dim result = ForStackOverFlow("http://SomeURL").Result

【讨论】:

  • at Dim response = Await client.GetAsync(New Uri(requestUri)) 我得到对象引用未设置为对象的实例“此异常最初是在此调用堆栈中引发的:[外部代码] " 我试图用 try catch 换行,但它只是进入了中断模式。
  • 两个问题。 1-您使用的是什么 dotnet 版本? 2- 确保传递正确的 URL。
  • 4.7.2。我验证了该 URL 与我测试我尝试拨打的电话时以大摇大摆的方式显示的 URL 一样。
  • 我已经更新了答案。现在也在本地测试。此版本应按预期工作。如果没有,请粘贴错误消息
  • 我尝试打开不同的异常设置,但它只说“System.NullReferenceException”然后它说这个异常最初是在这个调用堆栈中引发的:[外部代码]。当我在 Swagger 上登录时,它显示 Authorization Basic,所以我认为我们的身份验证类型是正确的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-04-01
  • 2014-01-26
  • 1970-01-01
  • 2011-03-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多