Solved

web service client wrapping soap in delphi

Posted on 2004-08-17
15
1,348 Views
Last Modified: 2010-05-18
I would like to have a delphi based client from which I can access web services.
I have no experience in this, but obviously there is the wsdl file which provide informations about the service and a soap file which is used to transport the information.
I have seen an example to do this in VB Excel: Getting the google search results which could be done in less then an hour.

There should be a generic appraoch also in delphi 6+ to get to this.

Could there be a generic recipent to go step by step to utlize the following soap code in a delphi client?

This is an example soap file found among others in the web:
<?xml version="1.0" ?>
- <definitions name="TemperatureService" targetNamespace="http://www.xmethods.net/sd/TemperatureService.wsdl" xmlns:tns="http://www.xmethods.net/sd/TemperatureService.wsdl" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns="http://schemas.xmlsoap.org/wsdl/">
- <message name="getTempRequest">
  <part name="zipcode" type="xsd:string" />
  </message>
- <message name="getTempResponse">
  <part name="return" type="xsd:float" />
  </message>
- <portType name="TemperaturePortType">
- <operation name="getTemp">
  <input message="tns:getTempRequest" />
  <output message="tns:getTempResponse" />
  </operation>
  </portType>
- <binding name="TemperatureBinding" type="tns:TemperaturePortType">
  <soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http" />
- <operation name="getTemp">
  <soap:operation soapAction="" />
- <input>
  <soap:body use="encoded" namespace="urn:xmethods-Temperature" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
  </input>
- <output>
  <soap:body use="encoded" namespace="urn:xmethods-Temperature" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
  </output>
  </operation>
  </binding>
- <service name="TemperatureService">
  <documentation>Returns current temperature in a given U.S. zipcode</documentation>
- <port name="TemperaturePort" binding="tns:TemperatureBinding">
  <soap:address location="http://services.xmethods.net:80/soap/servlet/rpcrouter" />
  </port>
  </service>
  </definitions>
0
Comment
Question by:hush021299
15 Comments
 
LVL 12

Expert Comment

by:Lee_Nover
ID: 11828099
File - New - WebServices: WSDL Import
enter the WSDL address, click next, finish
delphi created the TemperatureService.pas file
include that file in your app and use the service like:

uses TemperatureService;

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
     with GetTemperaturePortType do
       ShowMessage(FloatToStr(getTemp(Edit1.Text)));
end;




tested with Delphi7 but should be the same in D6 :)
0
 
LVL 14

Expert Comment

by:DragonSlayer
ID: 11851396
and that, is much much less than 1 hour ;-)
0
 
LVL 1

Author Comment

by:hush021299
ID: 11865506
let me try it!
0
 
LVL 1

Author Comment

by:hush021299
ID: 11868667
OK, this was almost too simple. So I tried to create a proper running example. This one runs but has some hickups.
So please help me to get it working.

Example Babelfish:
WDSL: http://www.xmethods.net/sd/2001/BabelFishService.wsdl

My code:
uses  BabelFishService;

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var s:widestring; i:integer;a,b, pc:pChar;  c:array [0..500] of char;
//more dont work!??
begin
s:=memo1.text;
for i := 1 to 500 do
c[i]:=char(s[i]);
//how to get the first char empty?
c[0]:='.';
a:=pchar(combobox1.Text);
b:=pchar(combobox2.Text);
memo2.text := BabelFishService.GetBabelFishPortType().BabelFish(a+'_'+b,(c));
end;

The problem is the conversion :How to get the chars from the memo1.text (better then this).
Furthermore i do not know where to find the translation methode shortcut e.g. en_fr  en_de (etc. more i dont know)
0
 
LVL 12

Expert Comment

by:Lee_Nover
ID: 11868760
errr why all that nonsense code ? :)
this will do:

procedure TForm1.Button1Click(Sender: TObject);
begin
     Memo2.Text:=GetBabelFishPortType.BabelFish(ComboBox1.Text + '_' + ComboBox2.Text, Memo1.Text);
end;

about the lang codes I suggest you check the http://babelfish.altavista.com/ source code :)
0
 
LVL 1

Author Comment

by:hush021299
ID: 11870172
Nope
Address error.....an invalid character was found in text content".

If I remember right there has been something with null terminated strings...

and I have visited already the link you mentioned. There is no list of the methods e.g. what is italy_sweden??

This conversion sh.. is a problem for me. I can use text in code, but when I use an input via control, i got the converion err.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 12

Expert Comment

by:Lee_Nover
ID: 11870247
that's odd .. may as well be Delphi6 problem
can you paste the interface part of the generated method ?
mine looks like:

  BabelFishPortType = interface(IInvokable)
  ['{B04F2684-B029-834D-8A91-8366E7B7E356}']
    function  BabelFish(const translationmode: WideString; const sourcedata: WideString): WideString; stdcall;
  end;
0
 
LVL 1

Author Comment

by:hush021299
ID: 11900466
 BabelFishPortType = interface(IInvokable)
  ['{B04F2684-B029-834D-8A91-8366E7B7E356}']
    function  BabelFish(const translationmode: WideString; const sourcedata: WideString): WideString; stdcall;
  end;
..its the same!
0
 
LVL 1

Author Comment

by:hush021299
ID: 11977417
..I still owe you points.
However, I would like to have more advice for 250.
what do you think?
Basically the answer I have implemented came from the forum in about com.
However basically you have answered the question!?
0
 
LVL 12

Expert Comment

by:Lee_Nover
ID: 11978401
I think I answered the question fully .. even gave you a step-by-step procedure and an example of usage
I don't know why delphi6 missinterpreted the service and can't test it coz I have only D7
and what was the 'answer' from about.com ?
0
 
LVL 1

Author Comment

by:hush021299
ID: 11983636
     9815.2 in reply to 9815.1

You're on the right track.  What you have there is a WSDL document.  This is a description of a webservice's exposed interfaces.  Unfortunately, it looks like you copied it out of Internet Explorer.  Next time, SAVE it instead, so you don't get those "- <..." dash symbols.  They invalidate the XML document.  I have removed them for you and pasted the result in this message for reference.

Now then!  You have a WSDL.  Save it to disk somewhere.

If you're using Delphi 7, start a new project.  Then, go to File -> New -> Other.  In the webservices tab, select WSDL IMPORTER.  Point to the file and click OK.  It will read the XML and generate a Delphi unit with all the webservice functions in it.

If you're using Delphi 8, most of these steps are pretty much the same.  Instead of File-New, you just right-click on your project references and add a web reference.

To call one of the webservice functions, you need to:

1) Use the unit.  Press Alt-F11 to do this it easily.

2) Get a handle to the interface that the unit defines.  This is done with the GET* function that was automatically generated.  For this WSDL, it describes the TemperaturePortType service, so the function here is GetTemperaturePortType().  This will return an interface of type TemperaturePortType.  You may call this function with no parameters if that's easier... they are all optional.  It may not be best, but it should work.  Try to specify them if you can, though.

3) Call any of the methods from the TemparaturePortType interface (I see that it really only has one method, getTemp()).  This automatically calls underlying methods to create an XML request according to the request type defined in the WSDL, sends the request to the URL in the WSDL via HTTP.  The web service there will read the XML, process the request, and send an XML response back, according to the response type defined in the WSDL.  The interface method will deserialize the XML response and return the value sent by the webservice.

When you don't have the WSDL document as a file, you can point the Import Wizard to the URL that publishes the WSDl instead.  It will download it automatically and generate the same unit that it would generate if you gave it a file instead.

 

<?xml version="1.0" ?>
<definitions name="TemperatureService" targetNamespace="http://www.xmethods.net/sd/TemperatureService.wsdl"

xmlns:tns="http://www.xmethods.net/sd/TemperatureService.wsdl" xmlns:xsd="http://www.w3.org/2001/XMLSchema"

xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns="http://schemas.xmlsoap.org/wsdl/">
<message name="getTempRequest">
<part name="zipcode" type="xsd:string" />
</message>
<message name="getTempResponse">
<part name="return" type="xsd:float" />
</message>
<portType name="TemperaturePortType">
<operation name="getTemp">
<input message="tns:getTempRequest" />
<output message="tns:getTempResponse" />
</operation>
</portType>
<binding name="TemperatureBinding" type="tns:TemperaturePortType">
<soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http" />
<operation name="getTemp">
<soap:operation soapAction="" />
<input>
<soap:body use="encoded" namespace="urn:xmethods-Temperature" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
</input>
<output>
<soap:body use="encoded" namespace="urn:xmethods-Temperature" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
</output>
</operation>
</binding>
<service name="TemperatureService">
<documentation>Returns current temperature in a given U.S. zipcode</documentation>
<port name="TemperaturePort" binding="tns:TemperatureBinding">
<soap:address location="http://services.xmethods.net:80/soap/servlet/rpcrouter" />
</port>
</service>
</definitions>
0
 
LVL 1

Author Comment

by:hush021299
ID: 12623333
Pls. close question
0
 

Accepted Solution

by:
modulo earned 0 total points
ID: 13395997
Closed, 250 points refunded.

modulo
Community Support Moderator
Experts Exchange
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now