VB.NET WebRequest 未检索到任何内容

VB.NET WebRequest Retrieves No Content

我正在开发一个 VB.NET 项目 (Outlook VSTO),我想通过其 API 从 Cisco AMP 获取数据。我面临的问题是,虽然我可以成功验证,并且收到 200 状态代码,但响应的内容长度始终为 -1,无论我从 API: ��

我知道这是我的 VB.NET 代码的问题,因为我能够通过 Python 请求检索实际信息,只需两行代码,绝对没有问题。根据 Cisco AMP 的文档,有两种方法可以访问 api:通过 https://APIGUID:APIKey@example.com/v1/requestedinformation 或通过 http://example.com/v1/requestedinformation 以及在 base64 (RFC 1945) 中编码的 headers 中指定的凭据。我可以从这两种方法中的任何一种中使用 Python 获得正确的信息,但到目前为止它们都不能与 VB.NET 一起使用。

以下是我在 VB.NET 中尝试过的两个主要解决方案,但请记住,我在每个尝试过的解决方案中都尝试了两种连接方法。

尝试 1 使用 WebClient:

    Public Sub Test()
        Dim url As String = "https://" & apiID & ":" & apiKey & "@" & "api.amp.cisco.com/v1/computers"
        MsgBox(url) 'Just to make sure I have the URL properly formatted

        'These two lines are needed to avoid SSL validation. 
        'The network I am working on this from sits behind a proxy for logging reasons,
        'so the certificate will not match the domain. This is 100% necessary.
        Net.ServicePointManager.ServerCertificateValidationCallback = New Net.Security.RemoteCertificateValidationCallback(Function() True)
        Net.ServicePointManager.SecurityProtocol = Net.SecurityProtocolType.Tls12
        
        'I have tried with the Uri data type as well as the url in String format
        Dim uri As Uri = New Uri(url)
        Dim request As Net.WebClient = New Net.WebClient

        'Headers, I copied them verbatim from Python since Python was able to get
        'the response content
        request.Headers.Add("Accept", "*/*") 'I have also tried application/json here
        request.Headers.Add("User-Agent", "python-requests/2.26.0")
        request.Credentials = New Net.NetworkCredential(apiID, apiKey)
        request.Headers.Add("Authorization", "Basic base64string") 'removed the actual base 64 string for privacy reasons, the actual string is in my code.
        request.Headers.Add("Accept-Encoding", "gzip, deflate")

        'I have tried DownloadData as well as DownloadString
        Dim htmlResponse As String = System.Text.Encoding.UTF8.GetString(request.DownloadData(uri))

        'Just using this mail item to display the response content
        Dim mail As Outlook.MailItem = Me.Application.CreateItem(Outlook.OlItemType.olMailItem)
        mail.HTMLBody = htmlResponse

        mail.Display()
    End Sub

尝试 2 使用 HTTPWebRequest:

    Public Sub Test()
        Dim url As String = "https://" & apiID & ":" & apiKey & "@" & "api.amp.cisco.com/v1/computers"
        MsgBox(url)

        'These two lines are needed to avoid SSL validation. 
        'The network I am working on this from sits behind a proxy for logging reasons,
        'so the certificate will not match the domain. This is 100% necessary.
        Net.ServicePointManager.ServerCertificateValidationCallback = New Net.Security.RemoteCertificateValidationCallback(Function() True)
        Net.ServicePointManager.SecurityProtocol = Net.SecurityProtocolType.Tls12

        'Headers, I copied them verbatim from Python since Python was able to get
        'the response content
        Dim uri As Uri = New Uri(url)
        Dim cookies As Net.CookieContainer = New Net.CookieContainer()
        Dim request As Net.HttpWebRequest = Net.HttpWebRequest.Create(uri)
        request.CookieContainer = cookies
        request.ServicePoint.Expect100Continue = False
        request.Accept = "*/*" 'I have also tried application/json here
        request.UserAgent = "python-requests/2.26.0"
        request.KeepAlive = True
        request.Credentials = New Net.NetworkCredential(apiID, apiKey)
        request.PreAuthenticate = True
        request.Headers.Add("Authorization", "Basic base64string")
        request.Headers.Add("Accept-Encoding", "gzip, deflate")

        Dim response As Net.HttpWebResponse = request.GetResponse()
        Dim reader As StreamReader = New StreamReader(response.GetResponseStream())
        Dim htmlResponse As String = reader.ReadToEnd()

        MsgBox(response.StatusCode)

        'Just using this mail item to display the response content
        Dim mail As Outlook.MailItem = Me.Application.CreateItem(Outlook.OlItemType.olMailItem)
        mail.HTMLBody = htmlResponse

        mail.Display()
    End Sub

在过去的几个小时里,我一直在为此苦苦思索,现在我已经没有想法了。我是在做蠢事还是有一些我不知道的 VB.NET 限制?

事实证明,解决方案非常简单。以上任何一个都可以,我只需要删除 Accept-Encoding header.

请注意,我首先添加它是因为:

  1. Cisco AMP 的 API 声明需要 Accept-Encoding: gzip header
  2. Python 使用了那个 header 并且能够检索数据

真的不知道为什么会这样,但无论如何,现在可以了。