Delphi7 idhttpserver Post Json

万万没想到,多年以后还会重新写 delphi 7 的代码。当然,代码不是全部都是自己写的,在原有的 demo 的基础上增加了一些功能。

其中,有一个需求就是需要能够远程接收 http post 的请求,请求方式是 post,数据格式是 json。然而,多年不写代码,在网上随便一搜,搜到的代码感觉都是同一个地方抄来抄去实现的,在处理 poststream 的时候都会出问题。

直到翻到了下面的代码,https://www.cnblogs.com/yangxuming/p/12462459.html

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
1 procedure TForm1.IdHTTPServer1CommandGet(AContext: TIdContext; ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
2 var
3 tmp: UTF8String; //也可以是 array of AnsiChar; 不能用RawByteString或 widestring
4 tmpstr: string;
5 size: Integer;
6 JSONObject: TJSONObject; // JSON类
7 i: Integer;
8 jo: ISuperObject;
9 begin
10 size := ARequestInfo.PostStream.size;
11 SetLength(tmp, size);
12 ARequestInfo.PostStream.Position := 0;
13 ARequestInfo.PostStream.ReadBuffer(tmp[1], size); //tmp为array of AnsiChar时为tmp[0]
14 tmpstr := UTF8ToString(tmp);
15
16 jo := SO(tmpstr);
17 Memo1.Lines.Add(jo['test'].AsString);
18 20 kbmMemTable1.Append;
21 kbmMemTable1.FieldByName('test').AsString := jo['test'].AsString;
22 kbmMemTable1.Post;
23
24 // Memo1.Lines.Add(ExtractFilePath(Application.ExeName) + 'testPrint.fr3');
25
26 frxReport1.LoadFromFile(ExtractFilePath(Application.ExeName) + 'testPrint.fr3');
27 frxReport1.PrintOptions.ShowDialog := false;
28 frxReport1.ShowProgress := False;
29 frxReport1.PrepareReport;
30 frxReport1.Print;
31
32 // JSONObject := TJSONObject.ParseJSONValue(Trim(tmpstr)) as TJSONObject;
33 // for i := 0 to JSONObject.Count - 1 do
34 // Memo1.Lines.Add(JSONObject.Pairs[i].JsonString.ToString + '=' +
35 // JSONObject.Pairs[i].JsonValue.ToString);
36 end;
1 procedure TForm1.IdHTTPServer1CommandGet(AContext: TIdContext; ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo); 2 var 3 tmp: UTF8String; //也可以是 array of AnsiChar; 不能用RawByteString或 widestring 4 tmpstr: string; 5 size: Integer; 6 JSONObject: TJSONObject; // JSON类 7 i: Integer; 8 jo: ISuperObject; 9 begin 10 size := ARequestInfo.PostStream.size; 11 SetLength(tmp, size); 12 ARequestInfo.PostStream.Position := 0; 13 ARequestInfo.PostStream.ReadBuffer(tmp[1], size); //tmp为array of AnsiChar时为tmp[0] 14 tmpstr := UTF8ToString(tmp); 15 16 jo := SO(tmpstr); 17 Memo1.Lines.Add(jo['test'].AsString); 18 20 kbmMemTable1.Append; 21 kbmMemTable1.FieldByName('test').AsString := jo['test'].AsString; 22 kbmMemTable1.Post; 23 24 // Memo1.Lines.Add(ExtractFilePath(Application.ExeName) + 'testPrint.fr3'); 25 26 frxReport1.LoadFromFile(ExtractFilePath(Application.ExeName) + 'testPrint.fr3'); 27 frxReport1.PrintOptions.ShowDialog := false; 28 frxReport1.ShowProgress := False; 29 frxReport1.PrepareReport; 30 frxReport1.Print; 31 32 // JSONObject := TJSONObject.ParseJSONValue(Trim(tmpstr)) as TJSONObject; 33 // for i := 0 to JSONObject.Count - 1 do 34 // Memo1.Lines.Add(JSONObject.Pairs[i].JsonString.ToString + '=' + 35 // JSONObject.Pairs[i].JsonValue.ToString); 36 end;
1 procedure TForm1.IdHTTPServer1CommandGet(AContext: TIdContext; ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
 2 var
 3   tmp: UTF8String; //也可以是 array of AnsiChar; 不能用RawByteString或 widestring
 4   tmpstr: string;
 5   size: Integer;
 6   JSONObject: TJSONObject; // JSON类
 7   i: Integer;
 8   jo: ISuperObject;
 9 begin
10   size := ARequestInfo.PostStream.size;
11   SetLength(tmp, size);
12   ARequestInfo.PostStream.Position := 0;
13   ARequestInfo.PostStream.ReadBuffer(tmp[1], size); //tmp为array of AnsiChar时为tmp[0]
14   tmpstr := UTF8ToString(tmp);
15 
16   jo := SO(tmpstr);
17   Memo1.Lines.Add(jo['test'].AsString);
18 20   kbmMemTable1.Append;
21   kbmMemTable1.FieldByName('test').AsString := jo['test'].AsString;
22   kbmMemTable1.Post;
23 
24 //  Memo1.Lines.Add(ExtractFilePath(Application.ExeName) + 'testPrint.fr3');
25 
26   frxReport1.LoadFromFile(ExtractFilePath(Application.ExeName) + 'testPrint.fr3');
27   frxReport1.PrintOptions.ShowDialog := false;
28   frxReport1.ShowProgress := False;
29   frxReport1.PrepareReport;
30   frxReport1.Print;
31 
32 //  JSONObject := TJSONObject.ParseJSONValue(Trim(tmpstr)) as TJSONObject;
33 //  for i := 0 to JSONObject.Count - 1 do
34 //    Memo1.Lines.Add(JSONObject.Pairs[i].JsonString.ToString + '=' +
35 //      JSONObject.Pairs[i].JsonValue.ToString);
36 end;

这里的处理逻辑是争取的,poststream 貌似不能直接将 stream 转换为 stringstream 或者 memorystream,在后续的转换过程中会出错,当然,也可能是由于 stream 的Position位置异常导致的。

另外 一个问题就是处理 json,网上的代码多数都是一笔带过,但是这个一笔带过就很烦人,找个可用的 json 库也比较麻烦,TJSONObject这个东西,看很多代码说都是内置的,但是实际在使用的时候,会提示找不到这个类。

所以,可以借助第三方的 json 库,例如https://github.com/frostney/superobject

通过这个东西来解析 json 就简单多了:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
var
obj: ISuperObject;
begin
obj := SO('{"foo": true}');
obj := TSuperObject.ParseString('{"foo": true}');
obj := TSuperObject.ParseStream(stream);
obj := TSuperObject.ParseFile(FileName);
end;
var obj: ISuperObject; begin obj := SO('{"foo": true}'); obj := TSuperObject.ParseString('{"foo": true}'); obj := TSuperObject.ParseStream(stream); obj := TSuperObject.ParseFile(FileName); end;
var
  obj: ISuperObject;
begin
  obj := SO('{"foo": true}');
  obj := TSuperObject.ParseString('{"foo": true}');
  obj := TSuperObject.ParseStream(stream);
  obj := TSuperObject.ParseFile(FileName);
end;
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
val := obj.AsObject.S['foo']; // get a string
val := obj.AsObject.I['foo']; // get an Int64
val := obj.AsObject.B['foo']; // get a Boolean
val := obj.AsObject.D['foo']; // get a Double
val := obj.AsObject.O['foo']; // get an Object (default)
val := obj.AsObject.M['foo']; // get a Method
val := obj.AsObject.N['foo']; // get a null object
val := obj.AsObject.S['foo']; // get a string val := obj.AsObject.I['foo']; // get an Int64 val := obj.AsObject.B['foo']; // get a Boolean val := obj.AsObject.D['foo']; // get a Double val := obj.AsObject.O['foo']; // get an Object (default) val := obj.AsObject.M['foo']; // get a Method val := obj.AsObject.N['foo']; // get a null object
val := obj.AsObject.S['foo']; // get a string
 val := obj.AsObject.I['foo']; // get an Int64
 val := obj.AsObject.B['foo']; // get a Boolean
 val := obj.AsObject.D['foo']; // get a Double
 val := obj.AsObject.O['foo']; // get an Object (default)
 val := obj.AsObject.M['foo']; // get a Method
 val := obj.AsObject.N['foo']; // get a null object

现在 ai 生成的代码质量的确也是个问题,这些小众(当前)语言生成的效果就更差了。

☆版权☆

* 网站名称:obaby@mars
* 网址:https://oba.by/
* 个性:https://oba.by/
* 本文标题: 《Delphi7 idhttpserver Post Json》
* 本文链接:https://oba.by/2024/08/17770
* 短链接:https://oba.by/?p=17770
* 转载文章请标明文章来源,原文标题以及原文链接。请遵从 《署名-非商业性使用-相同方式共享 2.5 中国大陆 (CC BY-NC-SA 2.5 CN) 》许可协议。


You may also like

31 comments

  1.   Level 7
    Google Chrome 127 Google Chrome 127 Mac OS X 10.15 Mac OS X 10.15 cn中国–浙江–杭州 联通

    太久远了,找不到人维护了,能不动就不动。

    1. 公主 Queen 
      Google Chrome 126 Google Chrome 126 Mac OS X 10.15 Mac OS X 10.15 cn中国–山东–青岛 联通

      给的编译的 32 位的 dll,java 加载不了,不知道神马情况。就 delphi 的代码能加载,并且功能目前看起来还算正常。

  2. Level 5
    Google Chrome 127 Google Chrome 127 Windows 11 Windows 11 cn中国–陕西–西安 联通

    看不懂,根本看不懂,连标题是啥都不知道😥😥😥😥

    1. Level 5
      Google Chrome 127 Google Chrome 127 Windows 11 Windows 11 cn中国–陕西–西安 联通

      欸,我把地址填成我主站之后,是不是集美们的标签就没了

        1. Level 5
          Google Chrome 127 Google Chrome 127 Windows 11 Windows 11 cn中国–陕西–宝鸡–岐山县 联通

          一个是根域名下的主页站,一个是我的博客站

              1. 公主 Queen 
                Google Chrome 126 Google Chrome 126 Mac OS X 10.15 Mac OS X 10.15 cn中国–山东–青岛 联通

                第一次加载尤其慢,后续加载全部完成也需要 20s。
                速度慢

                1. Level 5
                  Google Chrome 127 Google Chrome 127 Windows 11 Windows 11 cn中国–陕西–宝鸡–岐山县 联通

                  emmm,俺也不知道为啥,可能是因为我做了国内外分流,一不小心分到国外节点了😥😥我这里打开还蛮快的😗

  3. Level 4
    IBrowse r IBrowse r Android 9 Android 9 cn中国–甘肃–兰州 移动

    AI可以驯化 但不能代替 至于用它生成代码嘛 只有看个人的优化了 哈哈
    好的代码 就是能反复利用的 将别人的东西变成自己的 然后加上自己的优化 让他变成自己的东西 有用的东西

    1. 公主 Queen 
      Google Chrome 126 Google Chrome 126 Mac OS X 10.15 Mac OS X 10.15 cn中国–山东–青岛 联通

      驯化也是越普遍的效果越好,样本太小了效果就比较差了

  4. Level 3
    Firefox 115 Firefox 115 Windows 7 Windows 7 cn中国–湖北–武汉 电信

    写完技术文章能不能说两句大白话,不然想证明自己来过都不知道说啥好,总不能夸代码里的字母好看吧?

    1. Level 6
      Microsoft Edge 126 Microsoft Edge 126 Windows 10 Windows 10 us美国–加利福尼亚州–洛杉矶–洛杉矶 CNSERVERS_LLC

      虽然是古早语言,但可以夸丝、夸腿啊

  5.  Level 6
    Microsoft Edge 126 Microsoft Edge 126 Windows 11 Windows 11 cn中国–广东–珠海 电信

    所有公司里的项目,在赚钱的都是老旧项目,不赚钱的都是新开发的项目,无解

    1. Level 3
      Google Chrome 127 Google Chrome 127 Windows 10 Windows 10 cn中国–香港–九龙半岛–油尖旺区-尖沙咀 Cogent

      维护屎山,多挣钱。销售左脚踩右脚都能上市

    1. 公主 Queen 
      Google Chrome 126 Google Chrome 126 Mac OS X 10.15 Mac OS X 10.15 cn中国–山东–青岛 联通

      是的 ide,现在不是这个叫法了,叫做Embarcadero Delphi XE7

  6.  Level 5
    Google Chrome 111 Google Chrome 111 Windows 10 Windows 10 cn中国–上海–上海 电信

    我看传奇的项目还在用Delphi。我游戏行业的

    1. 公主 Queen 
      Google Chrome 126 Google Chrome 126 Mac OS X 10.15 Mac OS X 10.15 cn中国–山东–青岛 联通

      毕竟这游戏也足够老了,用 delphi 也正常

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注