码农的笔记

Delphi虽好,但已不流行; 博客真好,可以做笔记

博客园 首页 新随笔 联系 订阅 管理

Delphi7中测试idHttpServer,自带的idHttpServer

 

 

仅放上测试的部分代码,便于自己查看

  1 unit Unit1;
  2 
  3 interface
  4 
  5 uses
  6 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs
  7 , IdBaseComponent, IdComponent, IdTCPServer, IdCustomHTTPServer, IdHTTPServer
  8 , IdThreadMgr, IdThreadMgrPool, StdCtrls, IniFiles, DB, ADODB, WinSock
  9 ;
 10 
 11 type
 12 TForm1 = class(TForm)
 13 IdHTTPServer1: TIdHTTPServer;
 14 IdThreadMgrPool1: TIdThreadMgrPool;
 15 Label1: TLabel;
 16     ADOQuery1: TADOQuery;
 17     ADOConnection1: TADOConnection;
 18     Button1: TButton;
 19 procedure FormCreate(Sender: TObject);
 20 procedure IdHTTPServer1CommandGet(AThread: TIdPeerThread; ARequestInfo: TIdHTTPRequestInfo
 21 ; AResponseInfo: TIdHTTPResponseInfo);
 22     procedure Button1Click(Sender: TObject);
 23     procedure FormDestroy(Sender: TObject);
 24 private
 25   procedure sqlexec(vs1,vs2:string);
 26 { Private declarations }
 27 public
 28 { Public declarations }
 29 end;
 30 
 31 var
 32 Form1: TForm1;
 33 
 34 implementation
 35 
 36 {$R *.dfm}
 37 
 38 //uses untDM;
 39 function GetSysComputerIP: string;
 40 var 
 41   vHostName: array[0..255] of Char; 
 42   vWSData: TWSAData; 
 43   vHost: PHostEnt; 
 44 begin 
 45   Result := '127.0.0.1'; 
 46   WSAstartup(2, vWSData); 
 47   GetHostName(@vHostName[0], SizeOf(vHostName)); 
 48   vHost :=GetHostByName(@vHostName[0]); 
 49   if vHost <> nil then 
 50     Result :=Format('%d.%d.%d.%d', [Ord(vHost.h_addr^[0]), Ord(vHost.h_addr^[1]), Ord(vHost.h_addr^[2]), Ord(vHost.h_addr^[3])]); 
 51   WSACleanup; 
 52 end;
 53 
 54 procedure TForm1.FormCreate(Sender: TObject);
 55 var
 56 ini: TIniFile;
 57 begin
 58 IdHTTPServer1.ThreadMgr := IdThreadMgrPool1;
 59 //设置绑定参数
 60 IdHTTPServer1.Active := False;
 61 IdHTTPServer1.Bindings.Clear;
 62 ini := TIniFile.Create(ExtractFilePath(Application.ExeName) + 'config.ini');
 63 try
 64 IdHTTPServer1.DefaultPort := ini.ReadInteger('local', 'port', 0);
 65 finally
 66 ini.Free;
 67 end;
 68 IdHTTPServer1.Bindings.Add.IP :=GetSysComputerIP;
 69 //启动服务器
 70 IdHTTPServer1.Active := True;
 71 ADOConnection1.Connected:=True;
 72 end;
 73 
 74 procedure TForm1.IdHTTPServer1CommandGet(AThread: TIdPeerThread; ARequestInfo: TIdHTTPRequestInfo;
 75 AResponseInfo: TIdHTTPResponseInfo);
 76 var
 77   method, vt1,vT2: string;
 78   s:string;
 79  // v:TStringStream;
 80  ContentStream1 : TStream;
 81  i:Integer;
 82 begin
 83   method := ARequestInfo.Document;
 84 
 85   ShowMessage(Utf8ToAnsi(ARequestInfo.FormParams));
 86 
 87   //ShowMessage( ARequestInfo.RawHTTPCommand);
 88   if method = '/Api/test' then
 89   begin
 90     vt1 := ARequestInfo.Params.Values['t1'];
 91     vt2 := ARequestInfo.Params.Values['t2'];
 92     AResponseInfo.ResponseNo := 0;
 93     AResponseInfo.ContentType := 'text/html';
 94 
 95     try
 96       sqlexec(vt1,vt2);
 97       AResponseInfo.ContentText :=vt1+vt2;
 98       AResponseInfo.WriteContent;
 99     finally
100 
101     end;
102   end;
103 end;
104 
105 procedure TForm1.sqlexec(vs1, vs2: string);
106 begin
107   ADOQuery1.Close;
108   ADOQuery1.SQL.Text:=Format('insert into dbo.table2(D,E) values(''%s'',''%s'')',[vs1,vs2]);
109   ADOQuery1.ExecSQL;
110 end;
111 
112 procedure TForm1.Button1Click(Sender: TObject);
113 begin
114   ShowMessage(GetSysComputerIP);
115 end;
116 
117 procedure TForm1.FormDestroy(Sender: TObject);
118 begin
119   IdHTTPServer1.Active := False;
120   ADOConnection1.Connected:=False;
121 end;
122 
123 end.

 

---------------------------

网页Post的总不知道怎么获取参数

以下资料来源:https://blog.csdn.net/weixin_33924770/article/details/86399065

 1 procedure TFrmMain.HTTPServerCommandGet(AThread: TIdPeerThread;
 2   ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
 3 var
 4   RespStr,recvText: string;
 5   msgText: string;
 6 begin
 7   CoInitialize(nil);
 8   try
 9     try
10       msgText := 'Log|【'+FormatDateTime('yyyy-mm-dd hh:mm:ss',Now)+''+Format('收到请求:类型:%s, 路径:%s,来自:%s:%d',
11                              [ARequestInfo.Command, ARequestInfo.Document,
12                              TIdIOHandlerSocket(AThread.Connection.IOHandler).Binding.PeerIP,
13                              TIdIOHandlerSocket(AThread.Connection.IOHandler).Binding.PeerPort]);
14 
15       SendMessage(FrmMain.Handle,MYTHREAD_MESSAGE,Integer(@msgText),1);
16       if ARequestInfo.Command = 'GET' then
17         recvText := Utf8ToAnsi(Httpdecode(ARequestInfo.QueryParams))   //引用 Httpapp
18       else if ARequestInfo.Command = 'POST' then//post
19         recvText := Utf8ToAnsi(ARequestInfo.FormParams);
20 if (Pos(LowerCase('/api/getBusinessHallStatus'), LowerCase(ARequestInfo.Document)) = 1) then //查询营业厅状态接口
21       begin
22         RespStr := Httpserver_getBusinessHallStatus(recvText);
23       end
24       begin
25         RespStr := 'URL路径未定义';
26       end;
27       AResponseInfo.ContentType := 'text/HTML;charset=utf-8';
28       AResponseInfo.ContentText := AnsiToUtf8(RespStr);except
29       on e: Exception do
30       begin
31         msgText := 'ErrorLog|【'+FormatDateTime('yyyy-mm-dd hh:mm:ss',Now)+'】执行HTTPServerCommandGet发生异常,原因::'+e.Message;
32         SendMessage(FrmMain.Handle,MYTHREAD_MESSAGE,Integer(@msgText),1);
33       end;
34     end;
35   finally
36     CoUninitialize;
37   end;
38 end;

 

-----------------------------------

 

只是测试

 

posted on 2022-05-07 14:14  码农的笔记  阅读(795)  评论(0编辑  收藏  举报