调用servics时数组如何返回 ( 积分: 100 )

  • 主题发起人 主题发起人 noall
  • 开始时间 开始时间
N

noall

Unregistered / Unconfirmed
GUEST, unregistred user!
服务端这样:用dotnet写
定义了一个结构体:
public struct ClientData
{
public String Name;
public int ID;
}

[WebMethod(CacheDuration = 30,
Description = "返回客户记录")]

public ClientData[] GetClientData111(int Number)
{
ClientData[] Clients = null;

if (Number > 0 && Number <= 10)
{
Clients = new ClientData[Number];
for (int i = 0; i < Number; i++)
{
Clients.Name = "Client " + i.ToString();
Clients.ID = i;
}
}
return Clients;
}



客户端调用:用bds2006写:
这样:
不知道如何写:返回的数组长度无法定义,如何接收呢???
var call1:Service111Soap;
CallCleint:array of ClientData;
begin
call1:= htpr1 as Service111Soap;
CallCleint:=call1.GetClientData111(2);//这个有问题...当然其中2是可变的...
end;
 
转为字符串处理。自定义分割符。。。收到后再自己切嘛
 
无需那样复杂,根本就是动态数组.写完.NET的服务后,在Delphi里面导入WSDL后会自动生成接口.到时候直接使用就是了.给你写了一个例子.
.NET服务端:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Web;
using System.Web.Services;

namespace WebService1
{
public struct ClientData
{
public String Name;
public int ID;
}
public class WTest : System.Web.Services.WebService
{
public WTest()
{
InitializeComponent();
}

#region 组件设计器生成的代码
private IContainer components = null;
private void InitializeComponent()
{
}

/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if(disposing && components != null)
{
components.Dispose();
}
base.Dispose(disposing);
}

#endregion


[WebMethod(CacheDuration = 30,
Description = "返回客户记录")]

public ClientData[] GetClientData111(int Number)
{
ClientData[] Clients = null;

if (Number > 0 && Number <= 10)
{
Clients = new ClientData[Number];
for (int i = 0; i < Number; i++)
{
Clients.Name = "Client " + i.ToString();
Clients.ID = i;
}
}
return Clients;
}

}
}
Delphi导入WSDL生成的接口.
// ************************************************************************ //
// The types declared in this file were generated from data read from the
// WSDL File described below:
// WSDL : http://localhost/WebService1/WTest.asmx?wsdl
// Encoding : utf-8
// Version : 1.0
// (2007-3-28 11:38:22 - 16.03.2006)
// ************************************************************************ //

unit WTest;

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"

ClientData = class; { "http://tempuri.org/" }

ArrayOfClientData = array of ClientData; { "http://tempuri.org/" }


// ************************************************************************ //
// Namespace : http://tempuri.org/
// ************************************************************************ //
ClientData = class(TRemotable)
private
FName_: WideString;
FID: Integer;
published
property Name_: WideString read FName_ write FName_;
property ID: Integer read FID write FID;
end;


// ************************************************************************ //
// Namespace : http://tempuri.org/
// soapAction: http://tempuri.org/GetClientData111
// transport : http://schemas.xmlsoap.org/soap/http
// style : document
// binding : WTestSoap
// service : WTest
// port : WTestSoap
// URL : http://localhost/WebService1/WTest.asmx
// ************************************************************************ //
WTestSoap = interface(IInvokable)
['{6D9F3B0C-30AC-C377-BE76-BE658939B2D2}']
function GetClientData111(const Number: Integer): ArrayOfClientData; stdcall;
end;

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


implementation

function GetWTestSoap(UseWSDL: Boolean; Addr: string; HTTPRIO: THTTPRIO): WTestSoap;
const
defWSDL = 'http://localhost/WebService1/WTest.asmx?wsdl';
defURL = 'http://localhost/WebService1/WTest.asmx';
defSvc = 'WTest';
defPrt = 'WTestSoap';
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 WTestSoap);
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(WTestSoap), 'http://tempuri.org/', 'utf-8');
InvRegistry.RegisterDefaultSOAPAction(TypeInfo(WTestSoap), 'http://tempuri.org/GetClientData111');
InvRegistry.RegisterInvokeOptions(TypeInfo(WTestSoap), ioDocument);
RemClassRegistry.RegisterXSInfo(TypeInfo(ArrayOfClientData), 'http://tempuri.org/', 'ArrayOfClientData');
RemClassRegistry.RegisterXSClass(ClientData, 'http://tempuri.org/', 'ClientData');
RemClassRegistry.RegisterExternalPropName(TypeInfo(ClientData), 'Name_', 'Name');

end.

Delphi调用的例子:
var
Intf : WTestSoap;
R : ArrayOfClientData;
I:Integer;
begin
Intf := GetWTestSoap();
R := Intf.GetClientData111(5);
for I := Low(R) to High(R) do
ShowMessage(Format('%s ; %d',[R.Name_, R.ID]));

end;
 
接受答案了.
 

Similar threads

I
回复
0
查看
679
import
I
I
回复
0
查看
860
import
I
I
回复
0
查看
756
import
I
I
回复
0
查看
738
import
I
后退
顶部