日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

kbmmw 的HTTPSmartService入门

發布時間:2025/6/17 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 kbmmw 的HTTPSmartService入门 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

前面介紹過kbmmw 中的smartservice. 這個既可以用于kbmmw 的客戶端,也可以使用http 訪問。

在新版的kbmmw里面,作者加強了http 的支持,我們可以只使用HTTPSmartService

,這樣可以省去很多代碼,可以更方便、簡單的實現REST 服務。

首先先建一個工程文件,放置對應的控件。

?

?

新建一個kbmmw service

選HTTP smart service ,(這個向導界面太丑了,希望作者以后能請個美工處理一下)。

?

剩下的一路點過去,到最后生成代碼。

回到主窗口,輸入以下對應的代碼

procedure TForm1.Button1Click(Sender: TObject); beginkbmmwserver1.Active:=True; end;procedure TForm1.FormCreate(Sender: TObject); begin kbmmwserver1.AutoRegisterServices; end;

再看看生成的代碼

[kbmMW_Service('name:xalionrest, flags:[listed]')][kbmMW_Rest('path:/xalionrest')]// Access to the service can be limited using the [kbmMW_Auth..] attribute.// [kbmMW_Auth('role:[SomeRole,SomeOtherRole], grant:true')] TkbmMWCustomHTTPSmartService1 = class(TkbmMWCustomHTTPSmartService)private{ Private declarations }protected{ Protected declarations }public{ Public declarations }// HelloWorld function callable from both a regular client,// due to the optional [kbmMW_Method] attribute,// and from a REST client due to the optional [kbmMW_Rest] attribute.// The access path to the function from a REST client (like a browser)+// is in this case relative to the services path.// In this example: http://.../xalionhttp/helloworld// Access to the function can be limited using the [kbmMW_Auth..] attribute.// [kbmMW_Auth('role:[SomeRole,SomeOtherRole], grant:true')][kbmMW_Rest('method:get, path:helloworld')][kbmMW_Method]function HelloWorld:string;end;implementationuses kbmMWExceptions;{$R *.dfm}// Service definitions. //---------------------function TkbmMWCustomHTTPSmartService1.HelloWorld:string; beginResult:='Hello world'; end;initializationTkbmMWRTTI.EnableRTTI(TkbmMWCustomHTTPSmartService1); end.

由于我們是要做rest,因此,修改一下helloworld 的代碼,使其更符合rest 的格式

function TkbmMWCustomHTTPSmartService1.HelloWorld:string; beginResult:='{"result":"Hello world"}'; end;

ok, 編譯運行,使用瀏覽器訪問

?

?沒有任何問題,非常簡單方便。

?我們可以直接增加新的函數。

[kbmMW_Rest('method:get, path:version')][kbmMW_Method]function version:string;end;implementationuses kbmMWExceptions;{$R *.dfm}// Service definitions. //---------------------function TkbmMWCustomHTTPSmartService1.version: string; beginResult:='{"result":"'+self.Server.Version+'"}'; end;

運行顯示

處理參數

如果只有一個參數,可以直接使用 http://127.0.0.1/xalionrest/echostring/{參數}

[kbmMW_Method('EchoString')] // 回應輸入的串[kbmMW_Rest('method:get, path: ["echostring/{AString}","myechostring/{AString}" ]')][kbmMW_Auth('role:[SomeRole,SomeOtherRole], grant:true')]function EchoString([kbmMW_Rest('value: "{AString}"')] const AString:string):string; function TkbmMWCustomHTTPSmartService1.EchoString(const AString: string): string; beginresult:='{"result":"你好!'+astring+'"}';; end;

如果是多個參數,可以使用http://127.0.0.1/xalionrest/cal/numbers?arg1=10&arg2=20

[kbmMW_Method][kbmMW_Rest('method:get, path: "cal/addnumbers"')]function AddNumbers([kbmMW_Rest('value: "$arg1", required: true')] const AValue1:integer;[kbmMW_Rest('value: "$arg2", required: true')] const AValue2:integer;[kbmMW_Arg(mwatRemoteLocation)] const ARemoteLocation:string):string; function TkbmMWCustomHTTPSmartService1.AddNumbers(const AValue1,AValue2: integer; const ARemoteLocation: string):string; beginResult:='{"result":"'+(AValue1+AValue2).ToString+'"}';; end;

運行結果

?

下面處理一下post 的函數,首先做一個含有form 的html 文件

<table width="770" border="0" align="center" cellpadding="0" cellspacing="0" class="unnamed2"><tr><td width="848" align="center"><span class="style1">新生錄取查詢</span><br></td></tr><form name="form1" method="post" action="/xalionrest/postdata"><tr><td align="center"><span class="style2">姓名:</span> <input name="xsxm" type="text" id="xsxm"><span class="style2">身份證號:</span> <input name="sfzh" type="text" id="sfzh"></td></tr><tr><td align="center"><br><input type="submit" name="Submit" value="提交" onClick="return B1_onclick()">        <input type="reset" name="Submit" value="重置"></td></tr></form> </table>

?

?

對應的函數為

?

[kbmMW_Rest('method:post, path:postdata')][kbmMW_Method]function postdata:string; function TkbmMWCustomHTTPSmartService1.postdata: string; varvl:TkbmMWHTTPCustomValues;s,xsxm,sfzh:string;p:Tbytes;beginvl:=TkbmMWHTTPQueryValues.Create;tryp:= RequestStream.SaveToBytes;vl.AsString:= Tencoding.ASCII.GetString(p);xsxm:= vl.ValueByName['xsxm'];sfzh:=vl.ValueByName['sfzh'];// 這里就可以向數據庫里面操作了 result:='姓名:'+xsxm+' 身份證號'+sfzh;finallyvl.Free ;end;SetResponseMimeType('text/html');end;

運行結果

?

?基本上就是這樣。

kbmmw 5.04.40 新增加了post 內容文本自動識別功能,上面的函數可以變得更簡單。

[kbmMW_Rest('method:post, path:poststring')][kbmMW_Method]function poststring([kbmMW_Rest('value: "body", required: true')] const body:string):string; function TkbmMWCustomHTTPSmartService1.poststring(const body: string): string; varvl:TkbmMWHTTPCustomValues;s,xsxm,sfzh:string; beginvl:=TkbmMWHTTPQueryValues.Create;tryvl.AsString:= body;xsxm:= vl.ValueByName['xsxm'];sfzh:=vl.ValueByName['sfzh'];// 這里就可以向數據庫里面寫了 result:='姓名:'+xsxm+' 身份證號'+sfzh;finallyvl.Free ;end;SetResponseMimeType('text/html');end;

運行結果

?

?

?

后面我們再介紹數據庫的操作。

轉載于:https://www.cnblogs.com/xalion/p/7895179.html

總結

以上是生活随笔為你收集整理的kbmmw 的HTTPSmartService入门的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。