◆delphi调用webservice 问题◆

df978 2009-05-04 01:28:45
webservice用的是c#(Vs2005)的代码:
using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Data;

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Service : System.Web.Services.WebService
{
public Service()
{
//如果使用设计的组件,请取消注释以下行
//InitializeComponent();
}

[WebMethod]
public int Sub(int a, int b)//两个整数相减
{
return a - b;
}
}


下面是自动生成的service.pas:
// ************************************************************************ //
// The types declared in this file were generated from data read from the
// WSDL File described below:
// WSDL : http://10.18.1.27/webService/Service.asmx?WSDL
// Encoding : utf-8
// Version : 1.0
// (2009-05-04 13:23:01 - $Revision: 1.9.1.0.1.0.1.9 $)
// ************************************************************************ //

unit Service;

interface

uses InvokeRegistry, 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"


// ************************************************************************ //
// Namespace : http://tempuri.org/
// soapAction: http://tempuri.org/Sub
// transport : http://schemas.xmlsoap.org/soap/http
// binding : ServiceSoap
// service : Service
// port : ServiceSoap
// URL : http://10.18.1.27/webService/Service.asmx
// ************************************************************************ //
ServiceSoap = interface(IInvokable)
['{77573149-9C57-FA51-F11F-EFD527C91BD9}']
function Sub(const a: Integer; const b: Integer): Integer; stdcall;
end;

function GetServiceSoap(UseWSDL: Boolean=System.False; Addr: string=''): ServiceSoap;


implementation
uses SOAPHTTPClient;

function GetServiceSoap(UseWSDL: Boolean; Addr: string): ServiceSoap;
const
defWSDL = 'http://10.18.1.27/webService/Service.asmx?WSDL';
defURL = 'http://10.18.1.27/webService/Service.asmx';
defSvc = 'Service';
defPrt = 'ServiceSoap';
var
RIO: THTTPRIO;
begin
Result := nil;
if (Addr = '') then
begin
if UseWSDL then
Addr := defWSDL
else
Addr := defURL;
end;
RIO := THTTPRIO.Create(nil);
try
if UseWSDL then
begin
RIO.WSDLLocation := Addr;
RIO.Service := defSvc;
RIO.Port := defPrt;
end else
RIO.URL := Addr;
Result := (RIO as ServiceSoap);
finally
if Result = nil then
RIO.Free;
end;
end;


initialization
InvRegistry.RegisterInterface(TypeInfo(ServiceSoap), 'http://tempuri.org/', 'utf-8');
InvRegistry.RegisterDefaultSOAPAction(TypeInfo(ServiceSoap), 'http://tempuri.org/Sub');

end.


调用web服务的代码:
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,StdCtrls, DB, ADODB,InvokeRegistry, Rio, SOAPHTTPClient ;

type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

uses Service;

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
rtn: Integer;
ws: ServiceSoap;
begin
ws:= GetServiceSoap(false,'');
rtn:= ws.Sub(13,5);//计算13减去5的结果
showmessage('返回结果: '+ intToStr(rtn));
end;

end.


比较费解的是返回的结果一直“0”,但用C#调用时没有问题的
请高手帮忙,问题出在哪里?
...全文
580 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
liangpei2008 2009-05-04
  • 打赏
  • 举报
回复
InvRegistry.RegisterInvokeOptions(TypeInfo(ServiceSoap), ioDocument);
haisha 2009-05-04
  • 打赏
  • 举报
回复
在service单元的“initialization”加入:
InvRegistry.RegisterInvokeOptions(TypeInfo(ServiceSoap), ioDocument);
df978 2009-05-04
  • 打赏
  • 举报
回复
我用的是delphi 6
df978 2009-05-04
  • 打赏
  • 举报
回复
我试了一下,如果在web service加入如下函数的话,用delphi调用是可以得到"Hello World",但就是不能得到Sub的计算结果:
[WebMethod]
public string HelloWorld()
{
return "Hello World";
}

1,593

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 网络通信/分布式开发
社区管理员
  • 网络通信/分布式开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧