View Full Version : Autenticação Basica para consumo WebService

24-May-2022, 04:20 PM

Alguém tem um exemplo de como informar usuário e senha para autenticação basica para consumo de webservice?


Mike Peat
25-May-2022, 06:07 AM
Yes. Basically it is just a matter of concatenating <username> ":" <password> and base64 encoding them.


Function BasicAuthCreds String sUsername String sPassword Returns String
String sCreds
Pointer pEnc
Integer iVoid

Move (Trim(sUsername) + ":" + Trim(sPassword)) to sCreds
Move (Base64Encode(AddressOf(sCreds), SizeOfString(sCreds))) to pEnc // Note: in earlier (pre DF20.0) DF versions, use Length(sCreds) rather than SizeOfString
Move (PointerToString(pEnc)) to sCreds
Move (Free(pEnc)) to iVoid
Function_Return sCreds

Should work (untested).

Mike (sorry for replying in English! ;))

25-May-2022, 01:15 PM
Thank you very much. really worth it.;).

No objeto baseado na classe criada, também coloquei o seguinte.

Function Base64EncodeString Global String sText Returns String
Address pBase64
String sResult
Integer iVoid

Move (Base64Encode(AddressOf(sText), Length(sText))) to pBase64
Move pBase64 to sResult
Move (Free(pBase64)) to iVoid
Function_Return sResult

Object oWSserverFormulario1 is a cWSserverFormulario

// Interface:
// Function wsConsultar String llid_pedido Returns String
// Function wsAtualizar String llfchave String llfnumcartorio String llfnumprotocolo String llfdataprotocolo String llfusuario Returns String

// phoSoapClientHelper
// Setting this property will pop up a view that provides information
// about the Soap (xml) data transfer. This can be useful in debugging.
// If you use this you must make sure you USE the test view at the top
// of your program/view by adding: Use WebClientHelper.vw // oClientWSHelper

Set psPassword to "ppppppppppppp"
Set psUserName to "uuuuuuuuuuuuu"

Procedure OnPreSendSOAPRequest Handle hoSOAPXml Handle hoHttp String ByRef sHost String ByRef sFilePath
Forward Send OnPreSendSOAPRequest hoSOAPXml hoHttp (&sHost) (&sFilePath)
Boolean bOK
Get AddHeader of (phoHttp(Self)) "Authorization" ("Basic" * Base64EncodeString(psUsername(Self) + ":" + psPassword(Self))) to bOk



Mike Peat
26-May-2022, 06:56 AM
Yes, that should work as well. :)