delphi7调用.net的webservices

本文介绍如何使用Delphi 7调用基于.NET创建的WebService,包括创建WebService、生成客户端代理类及调用示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

先用vs.net创建一个简单的webservices;

using System.Web.Services.Protocols;

using System.Data.SqlClient;
using System.Data.Sql;
using System.Data;

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class ServiceReceive : System.Web.Services.WebService
{
   public ServiceReceive () {

        //如果使用设计的组件,请取消注释以下行
        //InitializeComponent();
    }

 [WebMethod(Description = "接收sql 的webService 返回sql")]
    public string getSql(string strsql)
    {

        return strsql;
    }
    ///////////////////////////

}


2.编译调试:

我们的webservices已经可以用了;


下面在打开delphi 7

新建一个应用程序:

拖两个button两个edit 和一个HTTPRIO(在webservices页上)到窗体上;


3.接下来:

File->new->other->webservices->WSDL importer

http://192.168.0.99:8080/web/ServiceReceive.asmx
点击next 导入

保存unit Service1
此文件是
// ************************************************************************ //
// The types declared in this file were generated from data read from the
// WSDL File described below:
// WSDL     : http://192.168.0.99:8080/web/ServiceReceive.asmx?WSDL
// Encoding : utf-8
// Version  : 1.0
// (2007-10-22 9:58:37 - 1.33.2.5)
// ************************************************************************ //

unit Service1;

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.
  // ************************************************************************ //
  // !:string          - "http://www.w3.org/2001/XMLSchema"

  Dsselect             = class;                 { "http://tempuri.org/" }
  SelectDB_HistoryResult = class;               { "http://tempuri.org/" }
  SelectDB_NowResult   = class;                 { "http://tempuri.org/" }
  SelectDB_GetnowdataResult = class;            { "http://tempuri.org/" }
  SqlDBResult          = class;                 { "http://tempuri.org/" }

 

  // ************************************************************************ //
  // Namespace : http://tempuri.org/
  // ************************************************************************ //
  Dsselect = class(TRemotable)
  private
    Fschema: WideString;
  published
    property schema: WideString read Fschema write Fschema;
  end;

 

  // ************************************************************************ //
  // Namespace : http://tempuri.org/
  // ************************************************************************ //
  SelectDB_HistoryResult = class(TRemotable)
  private
    Fschema: WideString;
  published
    property schema: WideString read Fschema write Fschema;
  end;

 

  // ************************************************************************ //
  // Namespace : http://tempuri.org/
  // ************************************************************************ //
  SelectDB_NowResult = class(TRemotable)
  private
    Fschema: WideString;
  published
    property schema: WideString read Fschema write Fschema;
  end;

 

  // ************************************************************************ //
  // Namespace : http://tempuri.org/
  // ************************************************************************ //
  SelectDB_GetnowdataResult = class(TRemotable)
  private
    Fschema: WideString;
  published
    property schema: WideString read Fschema write Fschema;
  end;

 

  // ************************************************************************ //
  // Namespace : http://tempuri.org/
  // ************************************************************************ //
  SqlDBResult = class(TRemotable)
  private
    Fschema: WideString;
  published
    property schema: WideString read Fschema write Fschema;
  end;


  // ************************************************************************ //
  // Namespace : http://tempuri.org/
  // soapAction: http://tempuri.org/%operationName%
  // transport : http://schemas.xmlsoap.org/soap/http
  // binding   : ServiceReceiveSoap
  // service   : ServiceReceive
  // port      : ServiceReceiveSoap
  // URL       : http://192.168.0.99:8080/web/ServiceReceive.asmx
  // ************************************************************************ //
  ServiceReceiveSoap = interface(IInvokable)
  ['{A687B73B-6D8E-01B7-4D15-3481B9DDACEF}']
    function  Siteidreceive(const Siteid: WideString; const Stable: WideString): WideString; stdcall;
    procedure DsTable(const Dsselect: Dsselect); stdcall;
    function  SelectDB_History(const SelectQuery: WideString; const stable: WideString): SelectDB_HistoryResult; stdcall;
    function  SelectDB_Now(const SelectQuery: WideString; const stable: WideString): SelectDB_NowResult; stdcall;
    function  SelectDB_Getnowdata(const SelectQuery: WideString; const stable: WideString): SelectDB_GetnowdataResult; stdcall;
    function  SqlDB(const Select_str: WideString; const stable: WideString): SqlDBResult; stdcall;
    function  getDataset(const datasetrev: WideString): WideString; stdcall;
    function  getSql(const strsql: WideString): WideString; stdcall;
   
  end;

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


implementation

function GetServiceReceiveSoap(UseWSDL: Boolean; Addr: string; HTTPRIO: THTTPRIO): ServiceReceiveSoap;
const
  defWSDL = 'http://192.168.0.99:8080/web/ServiceReceive.asmx?WSDL';
  defURL  = 'http://192.168.0.99:8080/web/ServiceReceive.asmx';
  defSvc  = 'ServiceReceive';
  defPrt  = 'ServiceReceiveSoap';
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 ServiceReceiveSoap);
    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(ServiceReceiveSoap), 'http://tempuri.org/', 'utf-8');
  InvRegistry.RegisterDefaultSOAPAction(TypeInfo(ServiceReceiveSoap), 'http://tempuri.org/%operationName%');

 


  InvRegistry.RegisterInvokeOptions(TypeInfo(ServiceReceiveSoap), ioDocument);// //就是这一行

 


  RemClassRegistry.RegisterXSClass(Dsselect, 'http://tempuri.org/', 'Dsselect');
  RemClassRegistry.RegisterXSClass(SelectDB_HistoryResult, 'http://tempuri.org/', 'SelectDB_HistoryResult');
  RemClassRegistry.RegisterXSClass(SelectDB_NowResult, 'http://tempuri.org/', 'SelectDB_NowResult');
  RemClassRegistry.RegisterXSClass(SelectDB_GetnowdataResult, 'http://tempuri.org/', 'SelectDB_GetnowdataResult');
  RemClassRegistry.RegisterXSClass(SqlDBResult, 'http://tempuri.org/', 'SqlDBResult');


end.

4.在unit1(form1 的单元文件)中引用Service1, XSBuiltIns

5. HTTPRIO1的

WSDLLocation  属性设置'http://192.168.0.99:8080/web/ServiceReceive.asmx?WSDL'

6.添加form1 的代码如下:

unit Unit1;

interface

uses

  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

  Dialogs, StdCtrls, InvokeRegistry, Rio, SOAPHTTPClient;

type

  TForm1 = class(TForm)

    Button1: TButton;

    Button2: TButton;

    Edit1: TEdit;

    Edit2: TEdit;

    HTTPRIO1: THTTPRIO;

    procedure Button1Click(Sender: TObject);

    procedure Button2Click(Sender: TObject);

  private

    { Private declarations }

  public

    { Public declarations }

  end;

var

  Form1: TForm1;

implementation

uses

  XSBuiltIns,

  Service1;

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);

 

begin

   Edit1.Text:=(HTTPRIO1 as  ServiceReceiveSoap).getSql('dddddd') ;

end;

 

end.

7.编译运行

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值