我正在處理一個 VB.NET 專案(Outlook VSTO),我想通過它的API從 Cisco AMP 獲取資料。我面臨的問題是,雖然我可以成功認證,并且收到 200 狀態碼,但回應的內容長度始終為 -1,無論我從 API 請求什么資訊,我得到的內容始終是這個:
我知道這是我的 VB.NET 代碼的問題,因為我能夠使用 Python 請求檢索實際資訊,使用 2 行代碼并且絕對沒有問題。根據 Cisco AMP 的檔案,有兩種訪問 api 的方法:通過 https://APIGUID:[email protected]/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 有一些限制?
uj5u.com熱心網友回復:
事實證明,解決方案非常簡單。以上任何一個都可以,我只需要洗掉Accept-Encoding標題。
請注意,我首先添加它是因為:
- Cisco AMP 的 API 宣告
Accept-Encoding: gzip需要標頭 - Python 使用了那個確切的標頭并且能夠檢索資料
真的不知道為什么會這樣,但無論如何,它現在有效。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/363432.html
上一篇:在C#中創建一個新物件
下一篇:如何選擇具有類功能的函式?
