将参数从Delphi Client传递到Java Webservice

时间:2014-09-09 16:06:58

标签: java web-services delphi client

我在GlassFish 3.1.2中运行java的简单Web服务:

@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;
}
}

所以,我使用Delphi的WSDL Importer来生成我的客户端。生成的代码是:

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.

当我做:

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;

我的客户找到了我的网络服务器,但参数为空

有人可以帮助我吗?

2 个答案:

答案 0 :(得分:1)

您的代码使用不同的值设置选项两次:

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

删除第一个,然后再试一次,看看是否有帮助:

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

如果这样无法解决问题,请更改

@SOAPBinding(style = SOAPBinding.Style.RPC)

@SOAPBinding(style = SOAPBinding.Style.DOCUMENT)

答案 1 :(得分:1)

我已经使用Delphi 2009和GlassFish 4进行了测试。

Delphi WSDL导入器返回的输出与您的输出不同:

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.

正如您所看到的,初始化部分存在差异。

Delphi客户端代码是:

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;

Java Web Service代码是:

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;
    }
}

Web服务返回预期结果。分析差异是另一回事,也许使用Fiddler2捕获HTTP流量进行比较是有帮助的。