您的位置:首页 > 编程语言 > Delphi

delphi7编写客户端调用java服务器端webservice示例

2013-10-30 17:07 489 查看
1. 首先取得java-webservice服务器端地址。我的是:http://localhost:8080/mywebservice/services/mywebservice?wsdl

2. 然后打开delphi7,file-new-other:选择WebService选项卡,在选择WSDLImporter
,在弹出的界面中输入java-webservice地址。点击Next-finish.会生成一个.pas的webservice文件,



生成的代码如下:
Java代码 收藏代码
// ************************************************************************ //
// The types declared in this file were generated from data read from the
// WSDL File described below:
// WSDL : http://localhost:8080/mywebservice/services/mywebservice?wsdl // Encoding : UTF-8
// Version : 1.0
// (2011-7-21 10:17:02 - 1.33.2.5)
// ************************************************************************ //

unit mywebservice;

interface

uses InvokeRegistry, SOAPHTTPClient, Types, XSBuiltIns;

type

// ************************************************************************ //
// The following types, referred to in the WSDL document are not being represented
// in this file. They are either aliases[@] of other types represented or were referred
// to but never[!] declared in the document. The types from the latter category
// typically map to predefined/known XML or Borland types; however, they could also
// indicate incorrect WSDL documents that failed to declare or import a schema type.
// ************************************************************************ //
// !:int - "http://www.w3.org/2001/XMLSchema"
// !:string - "http://www.w3.org/2001/XMLSchema"

// ************************************************************************ //
// Namespace : http://server // transport : http://schemas.xmlsoap.org/soap/http // style : document
// binding : mywebserviceHttpBinding
// service : mywebservice
// port : mywebserviceHttpPort
// URL : http://localhost:8080/mywebservice/services/mywebservice // ************************************************************************ //
mywebservicePortType = interface(IInvokable)
['{56F18980-71B1-FAC0-BFF5-569F621A8591}']
function add(const a: Integer; const b: Integer): Integer; stdcall;
function sayHello(const name: WideString): WideString; stdcall;
end;

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

implementation

function GetmywebservicePortType(UseWSDL: Boolean; Addr: string; HTTPRIO: THTTPRIO): mywebservicePortType;
const
defWSDL = 'http://localhost:8080/mywebservice/services/mywebservice?wsdl';
defURL = 'http://localhost:8080/mywebservice/services/mywebservice';
defSvc = 'mywebservice';
defPrt = 'mywebserviceHttpPort';
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 mywebservicePortType);
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(mywebservicePortType), 'http://server', 'UTF-8');
InvRegistry.RegisterDefaultSOAPAction(TypeInfo(mywebservicePortType), '');
InvRegistry.RegisterInvokeOptions(TypeInfo(mywebservicePortType), ioDocument);
InvRegistry.RegisterExternalParamName(TypeInfo(mywebservicePortType), 'add', 'out_', 'out');
InvRegistry.RegisterExternalParamName(TypeInfo(mywebservicePortType), 'sayHello', 'out_', 'out');

end.

3.

新建一个form窗体,拖入一个Button和一个edit控件。
在窗体的uses部分加入InvokeRegistry, Rio, SOAPHTTPClient, mywebservice。
为Button添加click事件。
Java代码 收藏代码
procedure TForm1.Button1Click(Sender: TObject);
var
server:mywebservicePortType;//此处为delphi生成的java-webservice的方法名。
aa:string;
begin
server:=GetmywebservicePortType(true,'',nil);//delphi生成的方法
showmessage(server.sayHello(Edit1.Text));//调用java-webservice的sayHello()方法。
end;

到此,delphi调用java的webservice服务器端程序示例已经完成。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: