.NET WEB SERVICE 学习记录(二)
建立WEB SERVICE的CLIENT端
用DELPHI7, “FILE”-》“NEW” -》“APPLICATION”
其中主要单元的代码如下:
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, InvokeRegistry, xmldom, Grids, DBGrids, DB, DBClient, Provider,
Xmlxform, StdCtrls, Rio, SOAPHTTPClient,DBTables,XMLIntf,XMLDoc,SOAPConst;
type
TForm1 = class(TForm)
HTTPRIO1: THTTPRIO;
Button1: TButton;
Memo1: TMemo;
XMLTransformProvider1: TXMLTransformProvider;
ClientDataSet1: TClientDataSet;
DataSource1: TDataSource;
DBGrid1: TDBGrid;
Button2: TButton;
Database1: TDatabase;
Query1: TQuery;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
USES SERVICE1;
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
A:Service1Soap;
B:WideString;
XMLDoc: IXMLDocument;
time1 : Tdatetime;
begin
DataSource1.DataSet := ClientDataSet1;
time1 := now;
A := HTTPRIO1 as Service1Soap;
B := A.GetPersonTable;
Memo1.Lines.Add( B );
DataSource1.DataSet := ClientDataSet1;
ClientDataset1.Active := FALSE;
XMLDoc := NewXMLDocument;
XMLDoc.Encoding := SUTF8;
XMLDoc.LoadFromXML(B);
XMLTransformProvider1.TransformRead.SourceXmlDocument := XMLDoc.GetDOMDocument;
ClientDataset1.Active := TRUE;
showmessage(floattostr((now-time1)*24*60*60));
end;
procedure TForm1.Button2Click(Sender: TObject);
VAR
time1 : Tdatetime;
begin
TIME1 := NOW;
DataSource1.DataSet := QUERY1;
//DATABASE1.Close;
//DATABASE1.OPEN; 打开数据库连接需要很久
QUERY1.Close;
QUERY1.Open;
showmessage(floattostr((now-time1)*24*60*60));
end;
end.
其编译前,需要要配置ClientDataSet1、HTTPRIO1、XMLTransformProvider1,以及接口单元文件SERVICE1.PAS(WSDL导入)
1.ClientDataSet1的配置:ClientDataSet1.ProviderName = XMLTransformProvider1
2. 配置HTTPRIO1:HTTPRIO1.WSDLLocation = http://localhost/mywebservice/Service1.asmx?WSDL
如果WEB SERVICES 是可用的,则HTTPRIO1.SERVICES 拉出Service1,HTTPRIO1.PORT 拉出Service1SOAP,
3. 产生SERVICE1.PAS, “FILE”->“NEW”->“OTHER”->"WEB SERVICES”->“WSDL Importer”
在Location of WSDL File or Url:中输入http://localhost/mywebservice/Service1.asmx?WSDL,然后next-》finish
这样就有了SERVICE1.PAS, 并在主界面上引用USES SERVICE1;
4. 配置XMLTransformProvider1前需要配置一个*.XTR文件来引导如何解析XML文件,是要赋值给属性 XMLTransformProvider1.TransformRead.TransformationFile
配置过程:需要用到DELPHI7的工具XML MAPPER(在DELPHI7的tools工具中)
1).需要一个从接口传过来的XML原形,如
<PersonTable>
<temp>
<USER_ID>14</USER_ID>
<USER_NAME>test</USER_NAME>
<USER_PASSWORD>test</USER_PASSWORD>
<USED_DATE>2003-11-05T00:00:00.0000000+08:00</USED_DATE>
<CHANGE_DATE>2005-03-15T14:47:58.0000000+08:00</CHANGE_DATE>
<LOGOUT_FLAG>0</LOGOUT_FLAG>
<USER_ADMIN_FLAG>0</USER_ADMIN_FLAG>
<USER_EMAIL>test@iconsh.com.cn</USER_EMAIL>
</temp>
</PersonTable>
保存为1.XML(可以先用程序从接口先得到,就是把ClientDataset1.Active := TRUE;先注销,从MEMO1得到XML原型)
2). 打开XML MAPPER,在左边框“Document View”中右键点击“open XML document”,打开上步骤存的1.XML
就可以看到表格结构了
3). 在XML MAPPER,左边框的结构上右键“SELECT ALL”把所有字段都选择到中间框
4).点击XML MAPPER的菜单Create|Datapacket from XML.,则在右边的Datapacket看到转换的表格了
5) 点击XML mapper的菜单保存转换过程 File -》Save -》 Transformation,保存名称为“ToDp.xtr”,这样就建立了对应格式用的格式文件
5.配置XMLTransformProvider1: XMLTransformProvider1.TransformRead.TransformationFile=目录+ToDp.xtr
这样就完成了客户端,进行编译运行就可以看到从WEB SERVICE传过来的数据了,如果改SQL,需要重新配置Transformation的文件“ToDp.xtr”