Dimmy Magalhães
Dimmy Magalhães

Reputation: 357

Pass parameter from Delphi Client to Java Webservice

I have a simply web service in java running in GlassFish 3.1.2:

@WebService
@SOAPBinding(style = SOAPBinding.Style.RPC)
public class TesteDeWSComDelphi {

/**
 * WebMethod
 *
 * @param s
 * @return
 */
@WebMethod(operationName = "operacaoPrincipal")
public String operacao1(String s) {
    System.out.println("Dados passados: " + s);
    return "Vc enviou para operação1 " + s;
}
}

So, I useed WSDL Importer from Delphi to generate my client. The genereted code is:

unit TesteDeWSComDelphiService1;

interface

uses InvokeRegistry, SOAPHTTPClient, Types, XSBuiltIns;

type
   TesteDeWSComDelphi = interface(IInvokable)
   ['{07BD6B8D-0E3D-9A88-AF46-D07FD1DB6D5B}']
   function  operacaoPrincipal(s: WideString): WideString; stdcall;
end;

function GetTesteDeWSComDelphi(UseWSDL: Boolean=System.False; Addr: string=''; HTTPRIO: THTTPRIO = nil): TesteDeWSComDelphi;


implementation

function GetTesteDeWSComDelphi(UseWSDL: Boolean; Addr: string; HTTPRIO: THTTPRIO):
TesteDeWSComDelphi;
const
  defWSDL = 'http://10.20.137.54:8080/mavenproject1/TesteDeWSComDelphiService?wsdl';
  defURL  = 'http://10.20.137.54:8080/mavenproject1/TesteDeWSComDelphiService';
  defSvc  = 'TesteDeWSComDelphiService';
  defPrt  = 'TesteDeWSComDelphiPort';
var
  RIO: THTTPRIO;
begin
  Result := nil;
  if (Addr = '') then
  begin
    if UseWSDL then
      Addr := defWSDL
    else
      Addr := defURL;
  end;
  if HTTPRIO = nil then
    RIO := THTTPRIO.Create(nil)
  else
    RIO := HTTPRIO;
  try
    Result := (RIO as TesteDeWSComDelphi);
    if UseWSDL then
    begin
      RIO.WSDLLocation := Addr;
      RIO.Service := defSvc;
      RIO.Port := defPrt;
    end else
      RIO.URL := Addr;
  finally
    if (Result = nil) and (HTTPRIO = nil) then
      RIO.Free;
  end;
end;


initialization
  InvRegistry.RegisterInvokeOptions(TypeInfo(TesteDeWSComDelphi), ioDocument);
  InvRegistry.RegisterInterface(TypeInfo(TesteDeWSComDelphi), 'http://ws.jus.br/', 'UTF-8');
  InvRegistry.RegisterInvokeOptions(TypeInfo(TesteDeWSComDelphi), ioDefault);
  InvRegistry.RegisterDefaultSOAPAction(TypeInfo(TesteDeWSComDelphi), '');
end.

When I make:

procedure TForm1.TestarClick(Sender: TObject);
var
 servico:TesteDeWSComDelphi;
 valor:string;
 begin
   servico:=GetTesteDeWSComDelphi(False, '');
   valor:=edt1.Text;
   Dialogs.ShowMessage('Enviando "' + valor+ '"');
   retorno.Text := servico.operacaoPrincipal(valor);
 end;

My client found my webserver, but the parameter is null.

Someone can help me?

Upvotes: 1

Views: 2417

Answers (2)

mjn
mjn

Reputation: 36634

I have tested it with Delphi 2009 and GlassFish 4.

The Delphi WSDL importer returned a different output than yours:

unit EchoWSService;

interface

uses InvokeRegistry, SOAPHTTPClient, Types, XSBuiltIns;

type
  EchoWS = interface(IInvokable)
  ['{F0D4A733-3EB7-44F0-47B7-FC0B5E5B6576}']
    function  echo(const arg0: string): string; stdcall;
  end;

function GetEchoWS(UseWSDL: Boolean=System.False; Addr: string=''; HTTPRIO: THTTPRIO = nil): EchoWS;


implementation
  uses SysUtils;

function GetEchoWS(UseWSDL: Boolean; Addr: string; HTTPRIO: THTTPRIO): EchoWS;
const
  defWSDL = 'http://127.0.0.1:8080/mavenproject1/EchoWSService?wsdl';
  defURL  = 'http://127.0.0.1:8080/mavenproject1/EchoWSService';
  defSvc  = 'EchoWSService';
  defPrt  = 'EchoWSPort';
var
  RIO: THTTPRIO;
begin
  Result := nil;
  if (Addr = '') then
  begin
    if UseWSDL then
      Addr := defWSDL
    else
      Addr := defURL;
  end;
  if HTTPRIO = nil then
    RIO := THTTPRIO.Create(nil)
  else
    RIO := HTTPRIO;
  try
    Result := (RIO as EchoWS);
    if UseWSDL then
    begin
      RIO.WSDLLocation := Addr;
      RIO.Service := defSvc;
      RIO.Port := defPrt;
    end else
      RIO.URL := Addr;
  finally
    if (Result = nil) and (HTTPRIO = nil) then
      RIO.Free;
  end;
end;


initialization
  InvRegistry.RegisterInterface(TypeInfo(EchoWS), 'http://echows/', 'UTF-8');
  InvRegistry.RegisterDefaultSOAPAction(TypeInfo(EchoWS), '');

end.

As you can see there are differences in the initialization section.

Delphi client code is:

procedure TForm7.Button1Click(Sender: TObject);
var
  servico: EchoWS;
  valor: string;
begin
  servico := GetEchoWS;

  valor := 'Hello';
  Dialogs.ShowMessage('Send "' + valor + '"');

  Edit1.Text := servico.echo(valor);
end;

And the Java Web Service code is:

package echows;

import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;

@WebService
@SOAPBinding(style = SOAPBinding.Style.RPC)
public class EchoWS {

    @WebMethod(operationName = "echo")
    public String echo(String s) {
        System.out.println("Received: " + s);
        return "Echo: " + s;
    }
}

The web service returns the expected result. Analyzing the differences is a different thing, maybe it is helpful to capture the HTTP traffic using Fiddler2 for comparision.

Upvotes: 1

mjn
mjn

Reputation: 36634

Your code sets the options two times with different values:

  InvRegistry.RegisterInvokeOptions(TypeInfo(TesteDeWSComDelphi), ioDocument);
  InvRegistry.RegisterInvokeOptions(TypeInfo(TesteDeWSComDelphi), ioDefault);

Remove the first, and try again to see if this helps:

InvRegistry.RegisterInvokeOptions(TypeInfo(TesteDeWSComDelphi), ioDefault);

If this does not fix the problem, change

@SOAPBinding(style = SOAPBinding.Style.RPC)

to

@SOAPBinding(style = SOAPBinding.Style.DOCUMENT)

Upvotes: 1

Related Questions