viewpoint-particle

Author Topic: WebService SSL with CLR  (Read 190 times)

stanl

  • Pundit
  • *****
  • Posts: 861
WebService SSL with CLR
« on: January 13, 2019, 12:22:52 pm »
Attached is a script looking into a German Web Service for train schedules. You would, of course, need to include a specific city but the main concern is the script will error with a message that the request does not support a specific SSL. There is a weak attempt to build the protocols, but it still fails (the error is captured so you can try this at home) but I think the resolution might be in a callback that WB doesn't support. Again looking for suggestions. [bored in Raleigh - freezing rain, yucky, and a German Shepherd very angry he can't go outside]


Code: Winbatch

;==========================================================================
;Winbatch 2018b - Web Request to overcome SSL issues
;Stan Littlefield, 1/13/2019
;==========================================================================
gosub udfs
IntControl(73,1,0,0,0)
If Version( )< '2013A'
   Pause("Notice", "Need 2013A or Newer Version of WinBatch")
   Exit
EndIf


bTrue = ObjectType( 'BOOL', -1 )
cURL="https://rit.bahn.de/webservices/rvabuchung?WSDL"
;cURL="https://rit.bahn.de/webservices/rvabuchung?Hanover"


ObjectClrOption("useany", "System")
ObjectClrOption("useany", "System.Net")
Encoding = ObjectClrNew( 'System.Text.Encoding' )
WebRequest = ObjectClrNew('System.Net.WebRequest')


;running the script as is will produce an SSL error
;need to create a way to avoid error
svcmanager = ObjectClrNew('System.Net.ServicePointManager')
protocols = 'Ssl3,Tls,Tls11,Tls12'
protocols = ObjectClrType(svcmanager.SecurityProtocol,protocols)


;callback cannot be used with WB CLR - so does this kill it
;svcmanager.ServerCertificateValidationCallback = bTrue


WebRequest = WebRequest.Create(cURL)
WebRequest.Timeout = WebRequest.Timeout * 6
WebResponse = WebRequest.GetResponse()
ResponseStream = WebResponse.GetResponseStream


; Pipes the stream to a higher level stream reader with the required encoding format.
StreamReader = ObjectClrNew("System.IO.StreamReader", ResponseStream, Encoding.UTF8)
Pause("", StreamReader.ReadToEnd())
Exit


:WBERRORHANDLER
geterror()
Message("Error Encountered",errmsg)
Exit


:udfs
#DefineSubRoutine geterror()
   wberroradditionalinfo = wberrorarray[6]
   lasterr = wberrorarray[0]
   handlerline = wberrorarray[1]
   textstring = wberrorarray[5]
   linenumber = wberrorarray[8]
   errmsg = "Error: ":lasterr:@LF:textstring:@LF:"Line (":linenumber:")":@LF:wberroradditionalinfo
   Return(errmsg)
#EndSubRoutine


Return
 

td

  • Tech Support
  • *****
  • Posts: 2763
    • WinBatch
Re: WebService SSL with CLR
« Reply #1 on: January 14, 2019, 07:36:22 am »
Might be wrong but the assertion that some WIL limitation is the cause of your problem seems more based on agenda than evidence.  You might consider spending some time reading MSFT's documentation if a resolution is your goal. 
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

td

  • Tech Support
  • *****
  • Posts: 2763
    • WinBatch
Re: WebService SSL with CLR
« Reply #2 on: January 14, 2019, 08:30:34 am »
A quick read of MSFT's docs produced that following which works just fine for me:
Code: Winbatch
ObjectClrOption("useany", "System")
ObjectClrOption("useany", "System.Net")

Uri = "https://rit.bahn.de/webservices/rvabuchung?WSDL"
objUri = ObjectClrNew('System.Uri', Uri)
Encoding = ObjectClrNew( 'System.Text.Encoding' )
WebRequest = ObjectClrNew('System.Net.WebRequest')

objSvcManager = ObjectClrNew('System.Net.ServicePointManager')

; It's Europe so you need Tls12 which is the enum value 3072:
; https://docs.microsoft.com/en-us/dotnet/api/system.net.securityprotocoltype?view=netframework-4.7.2
protocols = ObjectClrType("System.Net.SecurityProtocolType",3072)
objSvcManager.SecurityProtocol = protocols
objSvcPoint = objSvcManager.FindServicePoint(objUri)

WebRequest = WebRequest.Create(Uri)
WebRequest.Timeout = WebRequest.Timeout * 6
WebResponse = WebRequest.GetResponse()
ResponseStream = WebResponse.GetResponseStream

; Pipes the stream to a higher level stream reader with the required encoding format.
StreamReader = ObjectClrNew("System.IO.StreamReader", ResponseStream, Encoding.UTF8)
Pause("", StreamReader.ReadToEnd())
 
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

stanl

  • Pundit
  • *****
  • Posts: 861
Re: WebService SSL with CLR
« Reply #3 on: January 14, 2019, 02:29:46 pm »
Might be wrong but the assertion that some WIL limitation is the cause of your problem seems more based on agenda than evidence.  You might consider spending some time reading MSFT's documentation if a resolution is your goal.


I did spend time with the MSFT docs or I wouldn't have got as far as I did. I originally tried an async connect and got lots of errors. When I got to the protocols I saw them as text strings, so I messed up the ObjClrType(). And I wasn't questioning 'limitations' and it appears the callback wasn't necessary in the first place. As usual, thanks for your help. As a final note - what if I wasn't sure of the protocol and wanted to set up for all available - could all the numerics for the text protocols be sent to ObjClrType() as an array, or is it individual to each specific type.

td

  • Tech Support
  • *****
  • Posts: 2763
    • WinBatch
Re: WebService SSL with CLR
« Reply #4 on: January 14, 2019, 07:53:04 pm »
The link I provided to MSFT's coverage of the protocol enumerations recommends using the 0 a.k.a. the default protocol.  However, this requires dotNet newer than .Net4.6.2 and it doesn't work with your German site.  The documentation for the property also states that "Defaults vary depending on individual machine configuration, installed software, and applied patches." In a more positive vane, the documentation does mention that the enumeration has the FlagsAttribute attribute.   This means that you can or (|) the enumeration's values together. For example,   

protocols = ObjectClrType("System.Net.SecurityProtocolType",3072|768)

which your German site is happy with.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

stanl

  • Pundit
  • *****
  • Posts: 861
Re: WebService SSL with CLR
« Reply #5 on: January 15, 2019, 03:45:21 am »
In a more positive vane, the documentation does mention that the enumeration has the FlagsAttribute attribute.   This means that you can or (|) the enumeration's values together. For example,   

protocols = ObjectClrType("System.Net.SecurityProtocolType",3072|768)

which your German site is happy with.


The actual site I may have to touch is in Romania. Since the URL is a WSDL I may be able to write the script w/out the CLR. The Web ain't what it used to be  :o

kdmoyers

  • Full Member
  • ***
  • Posts: 241
Re: WebService SSL with CLR
« Reply #6 on: January 15, 2019, 04:44:40 am »
Could someone kindly post a link to a likely starting spot in the MSFT docs that talks about this kind of thing?  I have ZERO experience with webservices, but have a huge web services project emerging on the horizon.  Thanks!!
The mind is everything; What you think, you become.

stanl

  • Pundit
  • *****
  • Posts: 861
Re: WebService SSL with CLR
« Reply #7 on: January 15, 2019, 05:25:36 am »
Could someone kindly post a link to a likely starting spot in the MSFT docs that talks about this kind of thing?  I have ZERO experience with webservices, but have a huge web services project emerging on the horizon.  Thanks!!


I started here: https://docs.microsoft.com/en-us/dotnet/api/system.net?view=netframework-4.7.2


I tried to post a script that illustrated I didn't need Tony to do my homework (just help). My biggest hangup is how to correctly configure ObjClrType() as opposed to ObjType() which can sometimes be used with CLR. But, as support here is excellent, he usually fills in the missing blanks.


The basic way to access a web service with WB/CLR is in a script posted by Deana on the tech db [and I borrowed from that].
http://techsupt.winbatch.com/webcgi/webbatch.exe?techsupt/nftechsrch.web+~+TechHome#


.02




kdmoyers

  • Full Member
  • ***
  • Posts: 241
Re: WebService SSL with CLR
« Reply #8 on: January 16, 2019, 04:52:26 am »
Thanks Stan! What a great start!!
A quick followup question: what was the title of the article by Deanna?  Was it "Simple EWS Email Message" ?
(That link you posted makes an error for me. screenshot attached.)
-Kirby
The mind is everything; What you think, you become.

stanl

  • Pundit
  • *****
  • Posts: 861
Re: WebService SSL with CLR
« Reply #9 on: January 16, 2019, 05:16:30 am »
It was titled Obtain Web Data. Sorry about the link. I did a right-click copy link but didn't check it. Been using the Avast Secure Browser, based on chrome I think, and the copy link is bogus.


http://techsupt.winbatch.com/webcgi/webbatch.exe?techsupt/getarticle.web+WinBatch/dotNet/System_Net+Obtain~Web~Data.txt

kdmoyers

  • Full Member
  • ***
  • Posts: 241
Re: WebService SSL with CLR
« Reply #10 on: January 16, 2019, 06:11:05 am »
Perfect, thanks!!
The mind is everything; What you think, you become.