Base64-encoded credentials
Sometimes when you are connecting to a web service you need to authenticate using Base64 encoded credentials. This has became a lot easier now that we can use .Net
Here’s some sample code that connects to a web service with “username:password” but encoded as Base64. This code use a method to get a response which is saved in the XML document variable.
Name DataType Subtype Length HttpWebResponse DotNet System.Net.HttpWebResponse.'System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' HttpWebRequest DotNet System.Net.HttpWebRequest.'System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' MemoryStream DotNet System.IO.MemoryStream.'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' XMLDoc DotNet System.Xml.XmlDocument.'System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' Convert DotNet System.Convert.'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' Bytes DotNet System.Array.'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' UnicodeText DotNet System.Text.ASCIIEncoding.'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' MySetupTable.GET; //Some kind of setup table MySetupTable.TESTFIELD(Username); MySetupTable.TESTFIELD(Password); UnicodeText := UnicodeText.ASCIIEncoding; Bytes := UnicodeText.GetBytes(MySetupTable.Username + ':' + MySetupTable.Password); //Example of credential syntax HttpWebRequest := HttpWebRequest.Create(MySetupTable.URL); HttpWebRequest.Method := MyMethod; //Some textstring defining what data to get e.g. 'invoices' HttpWebRequest.ContentType := 'application/xml'; HttpWebRequest.Accept := 'application/xml'; HttpWebRequest.Headers.Add('Authorization', 'Basic ' + Convert.ToBase64String(Bytes)); HttpWebResponse := HttpWebRequest.GetResponse; //Here we execute the actual web service call MemoryStream := HttpWebResponse.GetResponseStream; //Load the response to the Memory Stream IF HttpWebResponse.StatusCode = 200 THEN BEGIN CLEAR(TempBlob); XMLDoc := XMLDoc.XmlDocument; XMLDoc.Load(MemoryStream); //Here we load the XML document with the response MemoryStream.Flush; MemoryStream.Close; END ELSE ERROR('Something went wrong')
If we are in classic runtime and cannot use .Net we need to be a bit more creative. There’s a function in an automation object for Commerce Gateway that can encode a text file and put the result in an XML Dome Document Node. So if we create a text file and encode it we can read the encoded node value. I wouldn’t do this at runtime on every web service call, so in the example below the encoded username/password is stored in a setup table.
Name DataType Subtype Length CGBase64 Automation 'CG Request Client'.Base64 DOMNode Automation 'Microsoft XML, v3.0'.IXMLDOMNode DOMDoc Automation 'Microsoft XML, v3.0'.DOMDocument TempFile File FileName Text 1024 IF (Username <> '') AND (Password <> '') THEN BEGIN TempFile.CREATETEMPFILE; FileName := TempFile.NAME; TempFile.CREATE(FileName); TempFile.WRITE(Username + ':' + Password); TempFile.CLOSE; CREATE(CGBase64); CREATE(DOMDoc); DOMNode := DOMDoc.createElement('b64'); // just a dummy to do the conversion CGBase64.Encode(FileName, DOMNode); "Base64 Username/password" := DOMNode.nodeTypedValue; CLEAR(DOMNode); CLEAR(DOMDoc); CLEAR(CGBase64); END ELSE CLEAR("Base64 Username/password");
No comments yet.