JSON Help: System.Net.WebRequest

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

Previous topic - Next topic

stanl

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()


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


exit