如何解决VB代码向API发送请求解析批量用户代理的问题
通过发送名为 X-API-KEY 的 HTTP 标头来验证我的 API 请求,其值为我按照获取您的 API 密钥说明获得的 API 密钥。
UserAgentParseDataBatchRequest 和 UserAgentParseDataBatchResponse 显示 BC30002:未定义类型“UserAgentParseDataBatchRequest”。 UserAgentParseDataBatchResponse 也是如此。我需要做什么才能使其正常工作。
模块程序
Sub Main()
Const API_KEY = "efebbbc7b3ec945e2890afc9143610e5"
Dim userAgentsToParse = New Dictionary(Of String,String)
' userAgentsToParse("1") = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_4) AppleWebKit/537.36 (KHTML,like Gecko) Chrome/44.0.2403.155 Safari/537.36"
' userAgentsToParse("2") = "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML,like Gecko) Chrome/78.0.3904.108 Safari/537.36"
' userAgentsToParse("3") = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_4) AppleWebKit/600.7.12 (KHTML,like Gecko) Version/8.0.7 Safari/600.7.12"
' userAgentsToParse("4") = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML,like Gecko) Chrome/42.0.2311.135 Safari/537.36"
' userAgentsToParse("5") = "Mozilla/5.0 (iPhone; cpu iPhone OS 13_3_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML,like Gecko) Version/13.0.5 Mobile/15E148 Safari/604.1"
' userAgentsToParse("6") = "Mozilla/5.0 (PlayStation 4 5.55) AppleWebKit/601.2 (KHTML,like Gecko)"
' userAgentsToParse("7") = "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)"
' userAgentsToParse("8") = "Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko"
Dim apiUrl = "https://api.whatismybrowser.com/api/v2/user_agent_parse_batch"
Dim headers As New Dictionary(Of String,String)()
headers.Add("X-API-KEY",API_KEY)
Dim postData As New UserAgentParseDataBatchRequest
postData.UserAgents = userAgentsToParse
'postData.ParSEOptions = New UserAgentParseDataRequestParSEOptions
' postData.ParSEOptions.ReturnMetadataForUserAgent = True
If (userAgentsToParse.Count > 500) Then
Console.WriteLine("You are attempting to send more than the maximum number of user agents in one batch")
Return
End If
Dim client = New RestClient(apiUrl)
client.UseNewtonsoftJson()
Dim request = New RestRequest(Method.Post)
For Each header In headers
request.AddHeader(header.Key,header.Value)
Next
request.AddJsonBody(postData)
Dim result = client.Execute(request)
Dim response As UserAgentParseDataBatchResponse
Try
response = JsonConvert.DeserializeObject(Of UserAgentParseDataBatchResponse)(result.Content)
Catch ex As Exception
Console.WriteLine(result.Content)
Console.WriteLine("Couldn't decode the response as JSON: {0}",ex)
Return
End Try
If (response.Result.Code <> "success") Then
Console.WriteLine("This API did not return a 'success' response. It said: result code: {0},message_code: {1},message: {2}",response.Result.Code,response.Result.MessageCode,response.Result.MessageCode)
'Console.WriteLine(JsonConvert.SerializeObject(response,Formatting.Indented));
Return
End If
' Now you have "result_json" And can store,display Or process any part of the response.
' --display some basic info about each parse result in the list
For Each parseRecord In response.Parses
' -- get the whole result from the batch
' this includes the 'parse' dict,as well as 'result'
If (parseRecord.Value Is nothing Or parseRecord.Value.Result Is nothing Or parseRecord.Value.Result.Code <> "success") Then
Console.WriteLine("There was a problem parsing the user agent with the id {0}",parseRecord.Key)
If (Not parseRecord.Value Is nothing And Not parseRecord.Value.Result Is nothing) Then
Console.WriteLine(parseRecord.Value.Result.Message)
End If
Continue For
End If
Console.WriteLine(JsonConvert.SerializeObject(parseRecord.Value,Formatting.Indented))
Dim parse = parseRecord.Value.Parse
Console.WriteLine("{0}: [{1}/{2}] {3}",parseRecord.Key,parse.HardwareType,parse.softwareType,parse.SimpleSoftwareString)
Next
End Sub
结束模块
解决方法
是的,您缺少 UserAgentParseDataBatchRequest 的类定义。
您将 postData 设置为新的 UserAgentParseDataBatchRequest 并且明确“未定义类型‘UserAgentParseDataBatchRequest’”
不过基于 postData,我们知道定义至少应该有: .用户代理 .ParseOptions .ParseOptions.ReturnMetadataForUserAgent
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。