Avatar billede coladrenge Nybegynder
08. maj 2005 - 10:12 Der er 3 kommentarer

SOAP client programmering

Hej

Nogen der kan give mig et eksempel på hvordan man programmere en SOAP client som kobler op mod en webservice.
Avatar billede martinlind Nybegynder
08. maj 2005 - 12:12 #1
Vi kan starte med at henvise til Delphi's hjælp, så vil du opdage at delphi ret faktisk har en wizard som wrapper din webservice ind som en component/Interface, og så er det bare at kode som du plejer !

Hurra for F1 !!!!
Avatar billede coladrenge Nybegynder
09. maj 2005 - 14:44 #2
Hej

Nu har jeg efterhånden været Delphi programmør i en del år - så F1 kender jeg godt.

Følgende er det interface som jeg har at arbejde med

// ************************************************************************ //
// The types declared in this file were generated from data read from the
// WSDL File described below:
// WSDL    : https://ei.sst.dk/ei/frontend.asmx?wsdl
// Encoding : utf-8
// Version  : 1.0
// (09-05-2005 14:44:27 - 1.33.2.5)
// ************************************************************************ //

unit frontend;

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

  SoapPacket          = class;                { "https://ei.sst.dk/" }
  ACKPacket            = class;                { "https://ei.sst.dk/" }

  { "https://ei.sst.dk/" }
  PacketPriority = (OneWay, High_, Admin);



  // ************************************************************************ //
  // Namespace : https://ei.sst.dk/
  // ************************************************************************ //
  SoapPacket = class(TRemotable)
  private
    FSoapData: WideString;
    FFound: Boolean;
  published
    property SoapData: WideString read FSoapData write FSoapData;
    property Found: Boolean read FFound write FFound;
  end;

  ArrayOfSoapPacket = array of SoapPacket;      { "https://ei.sst.dk/" }
  ArrayOfString = array of WideString;          { "https://ei.sst.dk/" }


  // ************************************************************************ //
  // Namespace : https://ei.sst.dk/
  // ************************************************************************ //
  ACKPacket = class(TRemotable)
  private
    FPriority: PacketPriority;
    FPacketID: WideString;
  published
    property Priority: PacketPriority read FPriority write FPriority;
    property PacketID: WideString read FPacketID write FPacketID;
  end;

  ArrayOfACKPacket = array of ACKPacket;        { "https://ei.sst.dk/" }

  // ************************************************************************ //
  // Namespace : https://ei.sst.dk/
  // soapAction: https://ei.sst.dk/%operationName%
  // transport : http://schemas.xmlsoap.org/soap/http
  // style    : document
  // binding  : FrontendSoap
  // service  : Frontend
  // port      : FrontendSoap
  // URL      : https://ei.sst.dk/ei/frontend.asmx
  // ************************************************************************ //
  FrontendSoap = interface(IInvokable)
  ['{E087EBD6-3C64-3FDE-6C41-4F395F04E0D1}']
    function  SendPackets(const Packets: ArrayOfSoapPacket): ArrayOfString; stdcall;
    function  SendACKPackets(const Packets: ArrayOfACKPacket): Boolean; stdcall;
    function  GetPackets(const PacketIDs: ArrayOfString): ArrayOfSoapPacket; stdcall;
  end;

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


implementation

function GetFrontendSoap(UseWSDL: Boolean; Addr: string; HTTPRIO: THTTPRIO): FrontendSoap;
const
  defWSDL = 'https://ei.sst.dk/ei/frontend.asmx?wsdl';
  defURL  = 'https://ei.sst.dk/ei/frontend.asmx';
  defSvc  = 'Frontend';
  defPrt  = 'FrontendSoap';
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 FrontendSoap);
    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(FrontendSoap), 'https://ei.sst.dk/', 'utf-8');
  InvRegistry.RegisterDefaultSOAPAction(TypeInfo(FrontendSoap), 'https://ei.sst.dk/%operationName%');
  InvRegistry.RegisterInvokeOptions(TypeInfo(FrontendSoap), ioDocument);
  RemClassRegistry.RegisterXSInfo(TypeInfo(PacketPriority), 'https://ei.sst.dk/', 'PacketPriority');
  RemClassRegistry.RegisterExternalPropName(TypeInfo(PacketPriority), 'High_', 'High');
  RemClassRegistry.RegisterXSClass(SoapPacket, 'https://ei.sst.dk/', 'SoapPacket');
  RemClassRegistry.RegisterXSInfo(TypeInfo(ArrayOfSoapPacket), 'https://ei.sst.dk/', 'ArrayOfSoapPacket');
  RemClassRegistry.RegisterXSInfo(TypeInfo(ArrayOfString), 'https://ei.sst.dk/', 'ArrayOfString');
  RemClassRegistry.RegisterXSClass(ACKPacket, 'https://ei.sst.dk/', 'ACKPacket');
  RemClassRegistry.RegisterXSInfo(TypeInfo(ArrayOfACKPacket), 'https://ei.sst.dk/', 'ArrayOfACKPacket');

end.



Det som jeg har brug for er et eksempel hvor man kan sende en XML fil servicen.
Avatar billede jawico Nybegynder
16. september 2009 - 00:24 #3
Her er noget D7 WebService der virker på min Apache server localhost

unit main;  // Mainformen med en knap, field og listbox

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TDelphiWebServiceClientSample = class(TForm)
    Button1: TButton;
    Label1: TLabel;
    Edit1: TEdit;
    Button2: TButton;
    ListBox1: TListBox;
    Label2: TLabel;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  DelphiWebServiceClientSample: TDelphiWebServiceClientSample;

implementation

uses soapserver;

{$R *.dfm}

procedure TDelphiWebServiceClientSample.Button1Click(Sender: TObject);
begin
    //Set the label caption with the result of calling the "echo" service
    label1.caption:=GetVCLWebServicePortType.serviceEcho(edit1.text);
end;

procedure TDelphiWebServiceClientSample.Button2Click(Sender: TObject);
var
  input: ArrayOfstring;
  i: integer;
  result: ArrayOfinteger;
begin
  setlength(input,16);
  for i := 0 to 9 do begin
      input[i]:=inttostr(i);
  end;

  //Fills the listbox with the integers returned from the service
  result:=GetVCLWebServicePortType.StringArrayToIntArray(input);

    listbox1.clear;
  for i := 0 to high(result) do begin
    listbox1.items.add(inttostr(result[i])+ '. linie');
  end;

end;

end.

------------------den unit der referers til soapserver er her
// ************************************************************************ //
// The types declared in this file were generated from data read from the
// WSDL File described below:
// WSDL    : http://localhost:3569/soapserver.php?wsdl
//  >Import : http://localhost:3569/soapserver.php?wsdl:0
// Encoding : ISO-8859-1
// Version  : 1.0
// (17/10/2007 18:53:04 - - $Rev: 7010 $)
// ************************************************************************ //

unit soapserver;  // OBS!!!!! ved deployment skal port :3569 fjernes !!!! det er sat ind ved RUN på debugging serveren.

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

  ArrayOfstring = array of WideString;          { "http://localhost"[GblCplx] }
  ArrayOfinteger = array of Int64;              { "http://localhost"[GblCplx] }

  // ************************************************************************ //
  // Namespace : http://localhost/
  // soapAction: http://localhost/soapserver.php/%operationName%
  // transport : http://schemas.xmlsoap.org/soap/http
  // style    : rpc
  // binding  : VCLWebServiceBinding
  // service  : VCLWebService
  // port      : VCLWebServicePort
  // URL      : http://localhost:3569/soapserver.php
  // ************************************************************************ //
  VCLWebServicePortType = interface(IInvokable)
  ['{2E51F07C-1564-1B1B-A2D8-45B42189790A}']
    function  serviceEcho(const input: WideString): WideString; stdcall;
    function  StringArrayToIntArray(const input: ArrayOfstring): ArrayOfinteger; stdcall;
  end;

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


implementation
  uses SysUtils;

function GetVCLWebServicePortType(UseWSDL: Boolean; Addr: string; HTTPRIO: THTTPRIO): VCLWebServicePortType;
const
  defWSDL = 'http://localhost:3569/soapserver.php?wsdl';
  //defURL  = 'http://localhost:3569/soapserver.php';
  defURL  = 'http://localhost/soapserver.php';
  defSvc  = 'VCLWebService';
  defPrt  = 'VCLWebServicePort';
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 VCLWebServicePortType);
    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(VCLWebServicePortType), 'http://localhost/', 'ISO-8859-1');
  InvRegistry.RegisterDefaultSOAPAction(TypeInfo(VCLWebServicePortType), 'http://localhost/soapserver.php/%operationName%');
  RemClassRegistry.RegisterXSInfo(TypeInfo(ArrayOfstring), 'http://localhost', 'ArrayOfstring');
  RemClassRegistry.RegisterXSInfo(TypeInfo(ArrayOfinteger), 'http://localhost', 'ArrayOfinteger');

end.

------------------------------------på serveren kører dette soapserver.php

<?php
/**
*  This file is part of the VCL for PHP project
*
*  Copyright (c) 2004-2008 qadram software S.L. <support@qadram.com>
*
*  Checkout AUTHORS file for more information on the developers
*
*  This library is free software; you can redistribute it and/or
*  modify it under the terms of the GNU Lesser General Public
*  License as published by the Free Software Foundation; either
*  version 2.1 of the License, or (at your option) any later version.
*
*  This library is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
*  Lesser General Public License for more details.
*
*  You should have received a copy of the GNU Lesser General Public
*  License along with this library; if not, write to the Free Software
*  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
*  USA
*
*/


//        ini_set("display_errors",1);
//        error_reporting(E_ALL);

require_once("vcl/vcl.inc.php");
//Includes
use_unit("comctrls.inc.php");
use_unit("webservices.inc.php");
use_unit("forms.inc.php");
use_unit("extctrls.inc.php");
use_unit("stdctrls.inc.php");

/*
* Returns the input
*/
function serviceEcho($input)
{
  return($input);
}

/*
* Converts the input array, which are strings, to an array of integers
*/
function StringArrayToIntArray($inputarray)
{
  $result = array();
  reset($inputarray);
  while(list($key, $val) = each($inputarray))
  {
      $result[] = (int)$val;
  }

  return($result);
}

//Class definition
class DmSoapServer extends DataModule
{
  public $MyWebService = null;
  function MyWebServiceAddComplexTypes($sender, $params)
  {
      //Add the complex type array of strings
      $this->MyWebService->addComplexType
      (
      'ArrayOfstring',
      'complexType',
      'array',
      '',
      'SOAP-ENC:Array',
      array(),
      array(array('ref' => 'SOAP-ENC:arrayType', 'wsdl:arrayType' => 'string[]')),
      'xsd:string'
      );

      //Add the complex type array of integers
      $this->MyWebService->addComplexType
      (
      'ArrayOfinteger',
      'complexType',
      'array',
      '',
      'SOAP-ENC:Array',
      array(),
      array(array('ref' => 'SOAP-ENC:arrayType', 'wsdl:arrayType' => 'integer[]')),
      'xsd:integer'
      );
  }

  function MyWebServiceRegisterServices($sender, $params)
  {
      //Register the echo service
      $this->MyWebService->register(
      "serviceEcho",
      array('input' => 'xsd:string'),
      array('return' => 'xsd:string'),
      'http://localhost/'
      );


      //Register the conversion service
      $this->MyWebService->register(
      "StringArrayToIntArray",
      array('input' => 'tns:ArrayOfstring'),
      array('return' => 'tns:ArrayOfinteger'),
      'http://localhost/'
      );

  }
}

global $application;

global $DmSoapServer;

//Creates the form
$DmSoapServer = new DmSoapServer($application);

//Read from resource file
$DmSoapServer->loadResource(__FILE__);

?>
----------------------------med denne soapserver.xml.php

<?php
<object class="DmSoapServer" name="dmSoapServer" baseclass="datamodule">
  <property name="Height">600</property>
  <property name="Name">dmSoapServer</property>
  <property name="Width">800</property>
  <object class="Service" name="MyWebService" >
        <property name="Left">244</property>
        <property name="Top">95</property>
    <property name="Active">1</property>
    <property name="Name">MyWebService</property>
    <property name="NameSpace">http://localhost</property>
    <property name="SchemaTargetNamespace">http://localhost/xsd</property>
    <property name="ServiceName">VCLWebService</property>
    <property name="OnAddComplexTypes">MyWebServiceAddComplexTypes</property>
    <property name="OnRegisterServices">MyWebServiceRegisterServices</property>
  </object>
</object>
?>
-------------------------------------
PHP delen er lavet med Delphi for PHP 2.0

Jeg har været glad for Delphi i 15 år men synes at Internet programmering har været en pine. Jeg ved ikke om du kan huske CGIExpert til D4.

Jeh gar en Visual Studio 2008 Pro på årøve og overvejer vedmodigt om ikke jeg skulle fortsætte med den. Det fungerer men er lidt klodset og Hjælp funktionerne er uoverskuelige.

Bare dog at Delphi for PHP kunne komme med list extra. Et canvas at printe på ville være fint.
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester