Webservice delphi soap with authentication

9

I'm using delphi to read a webservice from an insurance company.

It turns out that it comes in XML format in SOAP.

I make the WSDL import get the methods.

But to connect to the Web Service I have to send a token that I do not know how to do in the documentation has an example with wfc strom and it works. I have to create a string type msgheader where I send the token.

Then in the query parameters, I send only the bi and id of the company and it returns me data, before I have to go to the config and put the user, pass and domain to connect to the web service because it is in IIS. >

Delphi code that is not working

unit Unit3;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, WSClientes, StdCtrls,
  InvokeRegistry, SOAPHTTPClient, opCOnvertOptions, XMLIntf, XSBuiltIns,
  Soap.Rio;

type
  TForm3 = class(TForm)
    Memo1: TMemo;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);

  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form3: TForm3;

implementation

{$R *.dfm}

{ TSOAPCredentials }


procedure TForm3.Button1Click(Sender: TObject);

var
 ws   : WSClientes;

 Rio  : THTTPRIO;


begin
 Rio := THttpRIO.Create(nil);
 ws := WSClientes (false, '', Rio);

 ws :=WSClientes.MlBase2.Create;
 ws := WSClientes.GetIWSClientes();
 Rio.HTTPWebNode.UserName := 'dominio/user0121';
 Rio.HTTPWebNode.UserName := 'password';
 Rio.SOAPHeaders.ToString := 'L9+zby7w1V5OZTVGUPLZ1x8rJYzCEHAnxovPUuyadZFdbv21elZ1qsvy';
 Rio.SOAPHeaders.Send(Rio);

end;
    
asked by anonymous 19.11.2015 / 10:53

1 answer

1

My example of adding a header to the HTTPRIO object for authentication:

class function TCRMWebServices.GetFileTransferService(AUsername, APassword: string; ASendEvent: TPostingDataEvent): FileTransferServiceSoap;
var
  soapHeader: UserAuthentication;
  httpRio: THTTPRIO;
begin
  soapHeader := UserAuthentication.Create;
  soapHeader.Username := AUsername;
  soapHeader.Password := APassword;
  httpRio := GetHTTPRIO('Services/FileTransferService.asmx', soapHeader);
  if Assigned(ASendEvent) then
    httpRio.HTTPWebNode.OnPostingData := ASendEvent;
  result := httpRio as FileTransferServiceSoap;
end;

class function TCRMWebServices.GetHTTPRIO(AServicePath: string; ASOAPHeader: UserAuthentication): THTTPRIO;
begin
  if Trim(AServicePath) = '' then
    AServicePath := 'WebService.asmx';
  result := THTTPRIO.Create(nil);
  result.URL := 'http://' + TCRMWebConfig.GetWebHostname + '/' + AServicePath;
  if ASOAPHeader <> nil then
    result.SOAPHeaders.Send(ASOAPHeader as TSOAPHeader);
end;

The first method that is where I get an instance of HTTPRIO already set for the WebService I want to add creates an instance of a class of type TSOAPHeader , UserAuthentication .

This class has the properties Username and Password that I need to report according to specifications.

You should probably have received a class that inherits from TSOAPHeader , too, and this has the property that you need to tell the token / p>

The second method, GetHTTPRIO shows at the end how to add the Header to HTTPRIO :

result.SOAPHeaders.Send(ASOAPHeader as TSOAPHeader);

Result , which is the object HTTPRIO itself, .SOAPHeaders.Send(ASOAPHeader as TSOAPHeader);

See if you find this class and test this way as I demonstrated.

Of course, after getting an instance of HTTPRIO in a interface type variable for the Web Service you need to execute the desired method.

See that my first method returns the type FileTransferServiceSoap which is an interface with methods provided by WebService strong>.

Soon, I have something like:

var 
  service: FileTransferServiceSoap;
begin
  ...
  service := TCRMWebService.GetFileTransferService(username, password, evento);
  service.SendFile(file);
end;

This is something I did not see in your sample code and I would also like to cite that the method that is getting an instance of HTTPRIO and the WebService client > are confusing.

Trying to help

Just to complete, and try to help:

Often your statement:

var
  ws: WSClientes;

Where do you get:

ws := WSClientes(false, '', Rio);

Based on WSClientes that the Delphi tool generates, then:
- unit You are probably already the WebService client instantiated. - You are passing the ws object to it. Then you can try adding Header before moving on to this function.

Then just run the WebService method you want, for example:

clientes := ws.ObterClientes();

I now believe that you have more than enough information.

Good luck!

    
19.11.2015 / 14:39