VB.NET WebRequest 不检索任何内容
Posted
技术标签:
【中文标题】VB.NET WebRequest 不检索任何内容【英文标题】:VB.NET WebRequest Retrieves No Content 【发布时间】:2021-12-30 15:23:27 【问题描述】:我正在处理一个 VB.NET 项目 (Outlook VSTO),我想通过其API 从 Cisco AMP 获取数据。我面临的问题是,虽然我可以成功验证,并且收到 200 状态码,但响应的内容长度始终为 -1,无论我从 API 请求什么信息,我得到的内容始终是这个:
我知道这是我的 VB.NET 代码的问题,因为我能够使用 Python 请求检索实际信息,只需 2 行代码,绝对没有问题。根据 Cisco AMP 的文档,有两种方法可以访问 api:通过 https://APIGUID:APIKey@example.com/v1/requestedinformation 或通过http://example.com/v1/requestedinformation 使用 base64 编码的标头中指定的凭据(RFC 1945 )。我可以从这两种方法中的任何一种方法中使用 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 限制?
【问题讨论】:
请修剪您的代码,以便更容易找到您的问题。请按照以下指南创建minimal reproducible example。 【参考方案1】:事实证明,解决方案非常简单。以上任何一种都可以,我只需要删除Accept-Encoding
标头即可。
请注意,我首先添加它是因为:
-
Cisco AMP 的 API 声明需要
Accept-Encoding: gzip
标头
Python 使用了准确的标头并能够检索数据
真的不知道为什么会这样,但无论如何,它现在可以工作了。
【讨论】:
以上是关于VB.NET WebRequest 不检索任何内容的主要内容,如果未能解决你的问题,请参考以下文章
VB.net webrequest.uploadfile 中止错误
在验证 vb.net 中的有效凭据后,WebRequest 无法验证无效的代理凭据
jQuery hide 不隐藏 vb.net/asp.net webform 中的任何内容