C #如下
[WebMethod]
public string HelloWorld()
{
return "Hello World";
}
导入WSDL生成Delphi接口如下
Service1Soap = interface(IInvokable)
['{CFE467D4-A39E-2BD2-5836-7685A9E27F8D}']
function HelloWorld: WideString;
stdcall;
end;
还有一个生成接口的方法要稍做变动,否则中文参数会有点问题
function GetService1Soap(UseWSDL: Boolean;
Addr: string;
HTTPRIO: THTTPRIO): Service1Soap;
const
defWSDL = 'C:/Documents and Settings/WRUI.ECHON/Mydo
cuments/Service1.xml';
defURL = 'http://localhost/WebService3/Service1.asmx';
defSvc = 'Service1';
defPrt = 'Service1Soap';
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 Service1Soap);
if UseWSDL then
begin
RIO.WSDLLocation := Addr;
RIO.Service := defSvc;
RIO.Port := defPrt;
end else
RIO.URL := Addr;
RIO.HTTPWebNode.UseUTF8InHeader := True;//我加的,因为.NET默认WebService传递的字符串
//是UTF8,否则用中文参数会有问题
finally
if (Result = nil) and (HTTPRIO = nil) then
RIO.Free;
end;
end;
调用的时候代码如下
var
Intf:Service1Soap;
begin
Intf:= GetService1Soap(false,'http://localhost/WebService3/Service1.asmx');
ShowMessage(Intf.HelloWorld());
end;