Processing Headers in Client Applications

From RAD Studio
Jump to: navigation, search

Go Up to Writing Clients for Web Services


If the Web Service application you are calling expects your client to include any headers in its requests or if its response messages include special headers, your client application needs the definitions of the header classes that correspond to these headers. When you import a WSDL document that describes the Web Service application, the importer automatically generates code to declare these header classes and register them with the remotable type registry. If the server is written in Delphi, you can use the same units that the server application uses to define and register these header classes instead of the files generated by importing a WSDL file. Be sure that the unit uses the same namespace URI and SOAPAction header when it registers invokable interfaces. These values can be explicitly specified in the code that registers the interfaces, or it can be automatically generated. If it is automatically generated, the unit that defines the interfaces must have the same name in both client and server, and both client and server must define the global AppSpacePrefix variable to have the same value.

Note: For more information about header classes, see Defining and using SOAP headers.

As with a server, client applications use the Soap.InvokeRegistry.ISOAPHeaders interface to access incoming headers and add outgoing headers. The remote interfaced object that you use to call invokable interfaces implements the ISOAPHeaders interface. However, you can't obtain an ISOAPHeaders interface directly from the remote interfaced object. This is because when you try to obtain an interface directly from a remote interfaced object, it generates an in-memory vtable, assuming that the interface is an invokable interface. Thus, you must obtain the ISOAPHeaders interface from the invokable interface rather than from the remote interfaced object:

 var
   Service: IMyService;
   Hdr: TAuthHeader;
   Val: Double;
 begin
   Service := HTTPRIO1 as IService;
   Hdr := TAUthHeader.Create;
   try
     Hdr.Name := "Frank Borland";
     Hdr.Password := "SuperDelphi";
     (Service as ISOAPHeaders).Send(Hdr); { add the header to outgoing message }
     Val := Service.GetQuote("BORL"); { invoke the service }
   finally
     Hdr.Free;
   end;
 end;

Example using Authentication

This example is for the zip code lookup service at https://www.geosvc.com/. For more information go to http://api.geosvc.com/ZipCodeLookup.asmx.

procedure TForm1.Button1Click(Sender: TObject);

var
  WS: GeoPlacesSoap; // web service interface; uses placelookup
  Token: AuthenticationHeader;  // soap header
begin
  Token := nil;
  WS := GetGeoPlacesSoap;
  try
    Token := AuthenticationHeader.Create;
    Token.SessionID := 'myregistrationidentifier';
    (WS as ISOAPHeaders).Send(Token);
    WS.GetPlacesWithin('94558', 'CA', 25, City);
  finally
    FreeAndNil(Token);
  end;
end;

For explanations and more detailed source code, see: Bob Swart's video on EDN: http://edn.embarcadero.com/article/41013 - "Delphi SOAP Web Services and Security"

See Also