选择 File(文件)- New(新建)- New Web Server Application(新Web服务器应用程序)。我们要做的事用CGI就可以了,而且便于调试。在真实环境中用ISAPI DLL会好些。于是我们得到一个WebModule。打开它的Action Editior,在OnAction事件中加入下面的代码:
procedure TWebModule1.WebModule1WebActionItem1Action(Sender: TObject;
Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
begin
Response.Content :=IntToStr( StrToIntDef(Request.QueryFields.Values['x'],0) +
StrToIntDef(Request.QueryFields.Values['y'],0));
end;
服务器计算两个变量之和,然后传送到客户端。
在Delphi中可以有多种方法实现HTTP协议。可以用WinInet API函数(在Marco Cantu的《精通Delphi 5》中有很好的例子)或者Indy组件(在Delphi 6中应该整合了Indy,也可以在http://www.nevrona.com/Indy下载。这里我打算用Microsoft Internet Transfer Control 6.0,所以你需要在Delphi中Import类型库MSINET.OCX。在INET98.CHM帮助文件里面可以找到更多信息。这个控件用起来很简单,在我们的例子中也只需要一行代码:
procedure TForm1.Button1Click(Sender: TObject);
begin
Label1.Caption :=
inet1.OpenURL('http://localhost/scripts/webcalc.exe?x='+Edit1.Text+'&y='
+Edit2.Text);
end;
控件向服务器发送请求,并且接收回答。OnStateChange事件允许我们得到操作的详细信息:
procedure TForm1.Inet1StateChanged(Sender: TObject; State: Smallint);
begin
case state of
icResolvingHost: statusbar1.Panels[0].Text :=
('寻找指定服务器的IP地址。');
icHostResolved: statusbar1.Panels[0].Text :=
(' 成功找到IP地址。');
icConnecting: statusbar1.Panels[0].Text :=
(' 正在连接到主机。');
icConnected: statusbar1.Panels[0].Text :=
(' 成功连接到主机。');
icRequesting: statusbar1.Panels[0].Text :=
(' 发送请求。');
icRequestSent: statusbar1.Panels[0].Text :=
(' 成功发送请求。');
icReceivingResponse: statusbar1.Panels[0].Text :=
('接收主机回应。');
icResponseReceived: statusbar1.Panels[0].Text :=
(' 成功接收到主机回应。');
icDisconnecting: statusbar1.Panels[0].Text :=
(' 从服务器断开。');
icDisconnected: statusbar1.Panels[0].Text :=
('成功从服务器断开。');
icError: statusbar1.Panels[0].Text :=
('与服务器通讯发生错误。');
icResponseCompleted: statusbar1.Panels[0].Text :=
('The request has completed and all data has been received');
end;
end;
var
SoapClient: OleVariant;
SoapClient := CreateOleObject('MSSOAP.SoapClient');
begin
SoapClient.mssoapinit('http://localhost/scripts/sumservice.wsdl','SumService','SummerSoapPort');
showmessage(SoapClient.GetSum(Edit1.Text,Edit2.Text));
end;