本教程将展示在知道如何创建Web服务器以及如何发送动态生成的内容后,如何接受查询参数。
RealThinClient SDK是用于开发标准的HTTP(S)服务器,ISAPI扩展以及客户端的VCL控件。可用于Windows下的CodeGear Delphi 6-2010。关于RealThinClient SDK的教程会持续更新,本节是RealThinClient SDK的第三课,如何使用参数查询的服务器。
最新版RealThinClient SDK
本教程将展示在知道如何创建Web服务器以及如何发送动态生成的内容后,如何接受查询参数。我们将对我们之前的代码(演示发送动态生成内容的代码)进行一些小的更改,以接受请求的查询参数。
在之前编写动态生成的内容时,是没有选择让用户决定系统将返回哪个Square值的,所以接下来需要选择Square值的起始编 和结束数字。
接下来看一下具体的操作步骤:
-
打开我们的第2课项目。
-
浏览器的Square请求地址栏中的URL是:http:// localhost / square,现在需要传递两个参数,即起始和结束数字,以返回Squared值。修改 址如下:http://localhost/squaretart=10&end=20
再发送两个查询参数start和end。这里必须采取一些措施来防止内容增长并导致拒绝服务或出现更糟糕的情况。(注意:可以通过检查请求的平方数是否超过1,000来防止这种情况发生)。若没有start或end查询参数,就要为每个查询参数设置一个默认值,这样服务器运行时就不会出现错误。但是,还是需要向用户通知此类问题。
使用with
procedure TForm1.rdpSquareDataReceived(Sender: TRtcConnection); var viLine : integer; viStart, viEnd : integer; vbStartError, vbEndError, vbRangeError : boolean;begin with TRtcDataServer(Sender) do begin if Request.Complete then begin viStart := 1; viEnd := 100; vbStartError := True; vbEndError := True; vbRangeError := True; if Request.Query['start'] <> '' then try viStart := StrToInt(Request.Query['start']); vbStartError := False; except end; if Request.Query['end'] <> '' then try viEnd := StrToInt(Request.Query['end']); vbEndError := False; except end; if viEnd - viStart > 1000 then viEnd := viStart + 100 else vbRangeError := False; Write(''); Write('Square Values'); if vbStartError = True then Write('ERROR: Wrong start parameter. Set to Default (1)'); if vbEndError = True then Write('ERROR: Wrong end parameter. Set to Default (100)'); if vbRangeError = True then Write('ERROR: Wrong Range. Set to Default (100)'); Write('NumberSquare'); for viLine := viStart to viEnd do begin Write('' + IntToStr(viLine) + ''); Write('' + IntToStr(viLine * viLine) + ''); end; Write(''); end; end;end;
不使用with
procedure TForm1.rdpSquareDataReceived(Sender: TRtcConnection); var viLine : integer; rdsServer : TRtcDataServer absolute Sender; viStart, viEnd : integer; vbStartError, vbEndError, vbRangeError : boolean;begin if rdsServer.Request.Complete then begin viStart := 1; viEnd := 100; vbStartError := True; vbEndError := True; vbRangeError := True; if rdsServer.Request.Query['start'] <> '' then try viStart := StrToInt(rdsServer.Request.Query['start']); vbStartError := False; except end; if rdsServer.Request.Query['end'] <> '' then try viEnd := StrToInt(rdsServer.Request.Query['end']); vbEndError := False; except end; if viEnd - viStart > 1000 then viEnd := viStart + 100 else vbRangeError := False; rdsServer.Write(''); rdsServer.Write('Square Values'); if vbStartError = True then rdsServer.Write('ERROR: Wrong start parameter. Set to Default (1)'); if vbEndError = True then rdsServer.Write('ERROR: Wrong end parameter. Set to Default (100)'); if vbRangeError = True then rdsServer.Write('ERROR: Wrong Range. Set to Default (100)'); rdsServer.Write('NumberSquare'); for viLine := viStart to viEnd do begin rdsServer.Write('' + IntToStr(viLine) + ''); rdsServer.Write('' + IntToStr(viLine * viLine) + ''); end; rdsServer.Write(''); end;end;
-
检查服务器是否正在运行并发送正确响应。
接下来运行应用程序:
在浏览器中输入以下任一地址:
http://localhost/squaretart=10&end=200
http://localhost/square
http://localhost/squaretart=-15
http://localhost/squaretart=helloworld
浏览器显示画面如下:
本教程中附带的资源:
RealThinClient SDK – DEMO第3课 – 使用查询参数PDF
RealThinClient SDK第3课的代码
检查两个查询参数(start和end),如果没有此参数的数据,将会使用默认值(1表示start,100表示end)。然后检查范围(end减去start)是否大于1,000,如果是,则将其设置为100。如果任这类何检查失败,我们都会向用户发送错误消息。
标签:
声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!