翻譯|使用教程|編輯:況魚杰|2019-07-04 10:58:49.397|閱讀 381 次
概述:本教程將展示在知道如何創(chuàng)建Web服務(wù)器以及如何發(fā)送動(dòng)態(tài)生成的內(nèi)容后,如何接受查詢參數(shù)。
# 界面/圖表報(bào)表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
RealThinClient SDK是用于開發(fā)標(biāo)準(zhǔn)的HTTP(S)服務(wù)器,ISAPI擴(kuò)展以及客戶端的VCL控件。可用于Windows下的CodeGear Delphi 6-2010。關(guān)于RealThinClient SDK的教程會(huì)持續(xù)更新,本節(jié)是RealThinClient SDK的第三課,如何使用參數(shù)查詢的服務(wù)器。
點(diǎn)擊下載最新版RealThinClient SDK
本教程將展示在知道如何創(chuàng)建Web服務(wù)器以及如何發(fā)送動(dòng)態(tài)生成的內(nèi)容后,如何接受查詢參數(shù)。我們將對(duì)我們之前的代碼(演示發(fā)送動(dòng)態(tài)生成內(nèi)容的代碼)進(jìn)行一些小的更改,以接受請(qǐng)求的查詢參數(shù)。
在之前編寫動(dòng)態(tài)生成的內(nèi)容時(shí),是沒有選擇讓用戶決定系統(tǒng)將返回哪個(gè)Square值的,所以接下來需要選擇Square值的起始編號(hào)和結(jié)束數(shù)字。
接下來看一下具體的操作步驟:
打開我們的第2課項(xiàng)目。
編輯RtcDataProvider組件的OnDataReceived事件
瀏覽器的Square請(qǐng)求地址欄中的URL是:http:// localhost / square,現(xiàn)在需要傳遞兩個(gè)參數(shù),即起始和結(jié)束數(shù)字,以返回Squared值。修改網(wǎng)址如下://localhost/square?start=10&end=20
再發(fā)送兩個(gè)查詢參數(shù)start和end。這里必須采取一些措施來防止內(nèi)容增長并導(dǎo)致拒絕服務(wù)或出現(xiàn)更糟糕的情況。(注意:可以通過檢查請(qǐng)求的平方數(shù)是否超過1,000來防止這種情況發(fā)生)。 若沒有start或end查詢參數(shù),就要為每個(gè)查詢參數(shù)設(shè)置一個(gè)默認(rèn)值,這樣服務(wù)器運(yùn)行時(shí)就不會(huì)出現(xiàn)錯(cuò)誤。但是,還是需要向用戶通知此類問題。
使用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;
檢查兩個(gè)查詢參數(shù)(start和end),如果沒有此參數(shù)的數(shù)據(jù),將會(huì)使用默認(rèn)值(1表示start,100表示end)。然后檢查范圍(end減去start)是否大于1,000,如果是,則將其設(shè)置為100。如果任這類何檢查失敗,我們都會(huì)向用戶發(fā)送錯(cuò)誤消息。
檢查服務(wù)器是否正在運(yùn)行并發(fā)送正確響應(yīng)。
接下來運(yùn)行應(yīng)用程序:
在瀏覽器中輸入以下任一地址:
//localhost/square?start=10&end=200
//localhost/square
//localhost/square?start=-15
//localhost/square?start=helloworld
瀏覽器顯示畫面如下:
本教程中附帶的資源:
RealThinClient SDK - DEMO第3課 - 使用查詢參數(shù)PDF
在慧都科技,能夠?yàn)槟钠髽I(yè)找到解決方案,還有企業(yè)IT相關(guān)培訓(xùn),以及計(jì)算機(jī)軟件/硬件的銷售,想要了解更多有關(guān)慧都的資訊,請(qǐng)點(diǎn)擊,或者關(guān)注慧都微信公眾號(hào) ???
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@fc6vip.cn
文章轉(zhuǎn)載自: