简体   繁体   English

VB.NET WebRequest 检索到无内容

[英]VB.NET WebRequest Retrieves No Content

I'm working on a VB.NET project (Outlook VSTO) that I want to grab data from Cisco AMP via its API .我正在研究一个 VB.NET 项目(Outlook VSTO),我想通过它的API从 Cisco AMP 获取数据。 The issue I'm facing is that although I can successfully authenticate, and I receive a 200 status code, the content length of the response is always -1, and the content I get is always this no matter what information I request from the API: � 我面临的问题是,虽然我可以成功验证,并且收到 200 状态码,但响应的内容长度始终为 -1,无论我从 API 请求什么信息,我得到的内容始终是这个: �

I know this is an issue with my VB.NET code because I am able to retrieve the actual information with Python requests with like 2 lines of code and absolutely no issues.我知道这是我的 VB.NET 代码的问题,因为我能够使用 Python 请求检索实际信息,只需 2 行代码,绝对没有问题。 According to Cisco AMP's documentation, there are two ways to access the api: either by https://APIGUID:APIKey@example.com/v1/requestedinformation or by http://example.com/v1/requestedinformation with the credentials specified in the headers encoded in base64 (RFC 1945). According to Cisco AMP's documentation, there are two ways to access the api: either by https://APIGUID:APIKey@example.com/v1/requestedinformation or by http://example.com/v1/requestedinformation with the credentials specified in在 base64 (RFC 1945) 中编码的标头。 I can get the proper information with Python from either of these methods, but neither of them work with VB.NET so far.我可以通过这两种方法中的任何一种使用 Python 获得正确的信息,但到目前为止,它们都不能与 VB.NET 一起使用。

Here are the two main solutions I have tried in VB.NET, but keep in mind, I have tried both connection methods within each solution I have tried.这是我在 VB.NET 中尝试过的两个主要解决方案,但请记住,我在我尝试过的每个解决方案中都尝试了这两种连接方法。

Attempt 1 Using WebClient:尝试 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

Attempt 2 Using HTTPWebRequest:尝试 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

I've been bashing my head against this for the past few hours, I'm out of ideas at this point.在过去的几个小时里,我一直在反对这个,我现在没有想法。 Am I doing something stupid or is there some limitation of VB.NET I'm not aware of?我是在做一些愚蠢的事情还是我不知道 VB.NET 有一些限制?

As it turns out, the solution was quite simple.事实证明,解决方案非常简单。 Either of the above will work, I just needed to remove the Accept-Encoding header.以上任何一个都可以,我只需要删除Accept-Encoding header。

Note that I added it in the first place because:请注意,我首先添加了它,因为:

  1. Cisco AMP's API states that the Accept-Encoding: gzip header is needed Cisco AMP 的 API 声明需要Accept-Encoding: gzip header
  2. Python used that exact header and was able to retrieve the data Python 使用了准确的 header 并能够检索数据

Really not sure why this is the case, but at any rate, it works now.真的不知道为什么会这样,但无论如何,它现在有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM