JSON Help: System.Net.WebRequest

Started by stanl, September 27, 2020, 03:37:04 AM

Previous topic - Next topic


Having found setting User-Agent was the key to getting a CLR HttpClient request to work (in previous post) I turned my attention to System.Net.WebRequest which has a UserAgent property. The key there was not to set the property until after the Create() method was called. Really getting more comfortable working with .NET in WB's CLR :)
Code (WINBATCH) Select

;Winbatch 2020A - weather alerts for state
;Data returned as Json
cURL = "https://api.weather.gov/alerts/active?area=NC"
cFile = "C:\temp\NC.json"
If FileExist(cFile) Then FileDelete(cFile)
ObjectClrOption('useany', 'System')
oWeb = ObjectClrNew('System.Net.WebUtility')

oURI = ObjectClrNew('System.Uri', cURL)
oSP = ObjectClrNew('System.Net.ServicePointManager')
protocols = ObjectClrType("System.Net.SecurityProtocolType",3072|768|12288|192) 
oSP.SecurityProtocol = protocols
oPoint = oSP.FindServicePoint(oURI)

request = ObjectClrNew('System.Net.WebRequest')
request = request.Create(oURI)
request.UserAgent = "Other"  ;implement after Create()
request.Timeout = request.Timeout * 6
response = request.GetResponse()
oStream = response.GetResponseStream
Encoding = ObjectClrNew( 'System.Text.Encoding' )
oReader = ObjectClrNew("System.IO.StreamReader", oStream, Encoding.UTF8)
data = oReader.ReadToEnd()

If FileExist(cFile) Then Pause("Json File Created",cFile)



Stan, once again you have saved my bacon. Your example was plug-n-play. By now I owe you several footballs. (... "saved bacon"... "the ol' pig skin"... it's a stretch) Cannot thank you enough - and you too, Tony, of course, for creating the tech and the new JSON extender. And Jim, I know you're in there somewhere - and not only for the initial JSON extender... Man, I'm almost giddy - I was expecting a few hours of had slogging.


For those curious, I used this example with the OUI Lookup table at macaddress.io. All I did was change the first line in Stan's example, to (fake key):

cURL = "https://api.macaddress.io/v1?apiKey=at_4FLjZSTsWHmVAMrTVrZOPWxEXEeVK&output=json&search=44:38:39:ff:ef:57"

Got back a json reply with the company information, as desired.

(OUI lookups accept a MAC address and return the manufacturer name)