由于社保局的升级与公司的需要,要用delphi写个调用webservice的东西
由于之前同事都不是很精通那个方面,所以那突破技术的那苦力活又落在我身上了
社保局那边是用java方面的J2EE架构,另外提供了一个Webservice的接口出来让我们调用,其中还设计到了签名证书,TMD真麻烦。
现在证书还没拿到手,先用delphi7测试个例子
首先我自己模拟了一下java的webservice的模式,用myeclipse6写了一个基于Xfire的websercive ,用的也是helloWorld的经典例子,呵呵,加入tomcat,启动服务,OK。
下面轮到用delphi写客户端了,这个是我们的主题,
1.新建Project --> Application;
2.New-->Other-->WebService-->WSDL Importer,选择WebService服务的WSDL描述文件,也可选择WSDL文件的URL,填写“http://localhost:8080/HelloWorldService/services/HelloWorld?wsdl”;
3.单击“Finish”按钮,生成WSDL文件对应*.pas单元文件;
文件内容具体如下
unit HelloWorld;
interface
uses InvokeRegistry, SOAPHTTPClient, Types, XSBuiltIns;
type
HelloWorldPortType = interface(IInvokable)
['{7F30309F-3B9E-B482-BF0B-5A57A208D8D1}']
function example(const in0: WideString): WideString; stdcall;
end;
function GetHelloWorldPortType(UseWSDL: Boolean=System.False; Addr: string=''; HTTPRIO: THTTPRIO = nil): HelloWorldPortType;
implementation
function GetHelloWorldPortType(UseWSDL: Boolean; Addr: string; HTTPRIO: THTTPRIO): HelloWorldPortType;
const
defWSDL = 'http://localhost:8080/HelloWorldService/services/HelloWorld?wsdl';
defURL = 'http://localhost:8080/HelloWorldService/services/HelloWorld';
defSvc = 'HelloWorld';
defPrt = 'HelloWorldHttpPort';
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 HelloWorldPortType);
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(HelloWorldPortType), 'http://hellows', 'UTF-8');
InvRegistry.RegisterDefaultSOAPAction(TypeInfo(HelloWorldPortType), '');
InvRegistry.RegisterInvokeOptions(TypeInfo(HelloWorldPortType), ioDocument);
InvRegistry.RegisterExternalParamName(TypeInfo(HelloWorldPortType), 'example', 'out_', 'out');
end.
最后一步测试调用代码如下
procedure TForm1.btn1Click(Sender: TObject);
var
he : HelloWorldPortType;
st : string;
begin
he := GetHelloWorldPortType();
st := he.example('kenter');
ShowMessage(st);
end;
好的 测试成功