Webservice Delphi Soap with authentication

Asked

Viewed 4,061 times

9

I’m using Delphi to read a web service from an insurance company.

It happens to come in XML format in SOAP.

I import the WSDL I can get the methods.

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

Then in the parameters of the query, I send only the company’s bi and id and return 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.

Code Delphi 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;

1 answer

1

My example of adding a header to the object HTTPRIO 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 from where I get an instance of the HTTPRIO already set for the Webservice I wish to add creates an instance of a class of the type TSOAPHeader, to UserAuthentication.

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

You probably when parsing the WSDL must have received a class you inherit from TSOAPHeader also and this has the property you need to inform the token.

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

result.SOAPHeaders.Send(ASOAPHeader as TSOAPHeader);

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

See if you can find that class and test it this way.

Of course after obtaining an instance of HTTPRIO in a variable of the interface concerning the Web Service you need to execute the desired method.

See that my first method returns the type FileTransferServiceSoap which is a interface the methods made available by Webservice.

So I have something like:

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

This is something I have not seen in your example code and I would also like to quote that the method that is getting an instance of HTTPRIO and the client of Webservice are confused.


Trying to help

Just to complete, and try to help:

probably your statement:

var
  ws: WSClientes;

Already you give Ws as being WSClientes the guy from Webservice.

Where do you get:

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

Based on the unit that the tool of Delphi generates, then:
- ws He’s probably already the client of Webservice unstable.
- You’re passing the object HTTPRIO for it. Then you can try adding the Header before moving to this function.

Then just run the Webservice you want, as an example:

clientes := ws.ObterClientes();

I now believe you have more than enough information.

Good luck!

  • Why did you do that one amendment to the question?

  • @jbueno only to highlight the part that is really necessary

  • yes.I hate Delphi

  • wsclientes is the generated object, now authentication is missing

  • where do I pass the token?

  • @user2964140 does not have a class inherited from Tsoapheader?

  • I am not a layman in web services

  • ok.. I’m sorry for anything.. I unfortunately can’t help without more information.

  • how do I fetch the statement tsoapheader. i in the HTTPRIO object already have the user and password placed in the object properties

  • @user2964140, if I’m not mistaken, this user and password placed in the HTTPRIO component are for proxy.. When you use the Delphi tool to parse a wsdl, and there is use of Soapheaders in services, Unit generated by Delphi will have a class inherited from Tsoapheader. In my case, UserAuthentication is an inherited class of TSOAPHeader, guy UserAuthentication = class(TSOAPHeader);

  • OK then I don’t put in on the properties of the HHTPRIO..

Show 6 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.