RealThinClient SDK Pro教程:如何使用參數查詢的服務器(第3課)

  RealThinClient SDK是用於開發標準的HTTP(S)服務器,ISAPI擴展以及客戶端的VCL控件。可用於Windows下的CodeGear Delphi 6-2010。關於RealThinClient SDK的教程會持續更新,本節是RealThinClient SDK的第二課,如何使用構建的服務器發送動態生成的內容。瀏覽器

                                 點擊下載最新版RealThinClient SDKbash

本教程將展現在知道如何建立Web服務器以及如何發送動態生成的內容後,如何接受查詢參數。咱們將對咱們以前的代碼(演示發送動態生成內容的代碼)進行一些小的更改,以接受請求的查詢參數。 服務器

在以前編寫動態生成的內容時,是沒有選擇讓用戶決定系統將返回哪一個Square值的,因此接下來須要選擇Square值的起始編號和結束數字。 併發

接下來看一下具體的操做步驟:ide

  1. 打開咱們的第2課項目。ui

    02DEMO2bOpenProject-300x142.png

  2. 編輯RtcDataProvider組件的OnDataReceived事件 spa

    02DEMO2bOpenProject-300x142.png

    瀏覽器的Square請求地址欄中的URL是:http:// localhost / square,如今須要傳遞兩個參數,即起始和結束數字,以返回Squared值。修改網址以下:http://localhost/square?start=10&end=20code

    再發送兩個查詢參數start和end。這裏必須採起一些措施來防止內容增加並致使拒絕服務或出現更糟糕的狀況。(注意:能夠經過檢查請求的平方數是否超過1,000來防止這種狀況發生)。 若沒有開始或結束查詢參數會,就要爲每一個查詢參數設置一個默認值,這樣服務器運行時就不會出現錯誤。可是,仍是須要向用戶通知此類問題。orm

    使用withcdn

    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;複製代碼
  3. 檢查兩個查詢參數(start和end),若是沒有此參數的數據,將會使用默認值(1表示start,100表示end)。而後檢查範圍(end減去start)是否大於1,000,若是是,則將其設置爲100。若是任這類何檢查失敗,咱們都會向用戶發送錯誤消息。

  4. 檢查服務器是否正在運行併發送正確響應。

    接下來運行應用程序:

    04DEMO2bServerRunning-300x142.png

    在瀏覽器中輸入如下任一地址:

    http://localhost/square?start=10&end=200

    http://localhost/square

    http://localhost/square?start=-15

    http://localhost/square?start=helloworld

    瀏覽器顯示畫面以下:

    05DEMO2bServerResponseExample-300x269.png

    本教程中附帶的資源:

    RealThinClient SDK - DEMO第3課 - 使用查詢參數PDF

    RealThinClient SDK第3課的代碼

相關文章
相關標籤/搜索