2014-11-25 24 views
1

我想要做的HttpServer用Delphi 7 /印(9)組件(控制檯應用程序,而無需表單/應用):如何使控制檯應用程序中印的httpserver

更新:一個完整​​的控制檯例如用無限循環。

program httpserver; 
{$APPTYPE CONSOLE} 

uses 
    IdHTTPServer, IdTCPServer, IdCustomHTTPServer; 

Type 
    TCommandHandler= class 
    protected 
    procedure DoCommandGet(AThread: TIdPeerThread; 
     ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo); 

end; 

{$R *.res} 
var Server:TIdHTTPServer ; 
CH:TCommandHandler; 


procedure TCommandHandler.DoCommandGet(AThread: TIdPeerThread; 
    ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo); 
begin 
    AResponseInfo.ResponseText := '1234'; 
end; 

begin 
    Server := TIdHTTPServer.Create(nil); 
    CH := TCommandHandler.Create; 
    Server.OnCommandGet :=CH.DoCommandGet; 
    Server.DefaultPort := 3030; 
    Server.Active := True; 
    while true do ; 
end. 

但不行!我讀了indy的源代碼,並沒有成功搜索谷歌。

UPDATE:

  1. 沒有循環,程序結束(當然...)
  2. 隨着循環,響應很奇怪(的狀態碼級聯 - OK +響應) http://www.image-share.com/upload/2766/294.png
  3. 使用表格,回覆是確定的。 http://www.image-share.com/upload/2766/295.png

或許與印一個bug /限制......

+2

它工作正常。只是當你的應用程序碰到'end;'時(這實際上是一個'end.',順便說一句 - 請在這裏提問時發佈你真實的代碼)。爲了使代碼保持運行,您需要一個循環,直到您告訴它停止退出條件爲止。 (對於未來的問題,「不起作用」不是一個好問題描述;你需要告訴我們它是如何以你期望的方式工作的。) – 2014-11-25 01:45:58

回答

1

您設置了錯誤的成員AResponseInfo的事件添加代碼。 您必須使用ContentText代替ResponseText(這將在ResponseCode之後,你發現了被寫入)

program httpserver; 
{$APPTYPE CONSOLE} 

uses 
    IdHTTPServer, IdTCPServer, IdCustomHTTPServer; 

Type 
    TCommandHandler= class 
    protected 
    procedure DoCommandGet(AThread: TIdPeerThread; 
     ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo); 

end; 

{$R *.res} 
var Server:TIdHTTPServer ; 
CH:TCommandHandler; 


procedure TCommandHandler.DoCommandGet(AThread: TIdPeerThread; 
    ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo); 
begin 
    AResponseInfo.ContentText := '1234'; 
    AResponseInfo.ContentType := 'text/plain'; 
end; 

begin 
    Server := TIdHTTPServer.Create(nil); 
    CH := TCommandHandler.Create; 
    Server.OnCommandGet :=CH.DoCommandGet; 
    Server.DefaultPort := 3030; 
    Server.Active := True; 
    Readln; 
end. 
0

嗨,你需要做一個死循環,並觀看關閉一些組合。

program httpserver; 
{$APPTYPE CONSOLE} 

uses idHTTPServer; 


var 
    Server: TidHTTPServer; 
begin 
    Server := TidHTTPServer.Create(); 
    Server.DefaultPort := 3030; 
    Server.Active := True; 

    repeat 
    //DO SOMETHING 
    until (Break some how); 
end; 

所以應用程序永遠不會結束,德爾福與命令application.run開始爲Windows消息的內部循環。

其他的事情,需要爲TidHTTPServer組件

+0

我也試過用循環。 – 2014-11-25 03:17:18

+0

你已經把代碼放在事件上了嗎? – Oscar 2014-11-26 02:10:12

相關問題