简体   繁体   English

构建 HTTP 服务器应用程序

[英]Building HTTP Server Application

I have a project which does financial reports and I want to let user to be able to get this reports through the internet我有一个做财务报告的项目,我想让用户能够通过互联网获得这个报告

I tried using TIdHTTPServer which is an Indy component to make my application to work as an HTTP Server and to let it to be able我尝试使用 TIdHTTPServer 这是一个 Indy 组件来使我的应用程序作为 HTTP 服务器工作并让它能够

receive request -> process the request -> send back the result of the request process接收请求 -> 处理请求 -> 发回请求处理结果

using a special port.使用特殊端口。

now my problem is that I'm getting a lot of Access Violation errors and random exceptions it looks like about threads problem or I don't know because if I process the same request without using the TIdHTTPServer I don't get any problem现在我的问题是我收到了很多访问冲突错误和随机异常,它看起来像是线程问题,或者我不知道,因为如果我在不使用 TIdHTTPServer 的情况下处理相同的请求,我不会遇到任何问题

i'm using the OnCommandGet Event to process the request and send the result back to user inside the context stream.我正在使用 OnCommandGet 事件来处理请求并将结果发送回上下文流内的用户。

what I need is a demonstration on how to use it with TADODataSet and TADOConnection我需要的是关于如何将它与 TADODataSet 和 TADOConnection 一起使用的演示

for example I need the user to be able to send a request and the TIdHTTPServer takes the request (for example call a stored procedure using to ADODataSet and take the result as XML file and send it back to the user)例如,我需要用户能够发送请求并且 TIdHTTPServer 接受请求(例如调用使用 ADODataSet 的存储过程并将结果作为 XML 文件并将其发送回用户)

please help....thank you.请帮助....谢谢。

one possibility how a Server could work ...服务器如何工作的一种可能性......

unit Unit3;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs,IDContext, IdBaseComponent, IdComponent, IdCustomTCPServer, IdTCPServer, StdCtrls, DB, ADODB;

type
  TForm3 = class(TForm)
    IdTCPServer1: TIdTCPServer;
    Memo1: TMemo;
    Button1: TButton;
    DummyConnection: TADOConnection;
    procedure Button1Click(Sender: TObject);
    procedure IdTCPServer1Execute(AContext: TIdContext);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form3: TForm3;

implementation
uses ComObj,AdoInt,ActiveX;
{$R *.dfm}
function SendStream(AContext: TIdContext; AStream: TStream): Boolean;
begin
   Result := False;
   try
     AContext.Connection.IOHandler.Write(AStream.Size);  // sending length of Stream first
     AContext.Connection.IOHandler.WriteBufferOpen;
     AContext.Connection.IOHandler.Write(AStream, AStream.Size);
     AContext.Connection.IOHandler.WriteBufferFlush;
   finally
     AContext.Connection.IOHandler.WriteBufferClose;
   end;
   Result := True;
end;

procedure TForm3.Button1Click(Sender: TObject);
begin
  IdTCPServer1.Active := true;
end;


{ Clientside function
Function RecordsetFromXMLStream(Stream:TStream): _Recordset;
var
  RS: Variant;
begin
    RS := CreateOleObject('ADODB.Recordset');
    RS.Open(TStreamAdapter.Create(Stream) as IUnknown);
    Result := IUnknown(RS) as _Recordset;
end;
}

Procedure RecordsetToXMLStream(const Recordset: _Recordset;Stream:TStream);
var
  RS: Variant;
begin
    if Recordset = nil then Exit;
    RS := CreateOleObject('ADODB.Recordset');
    RS := Recordset;
    RS.Save(TStreamAdapter.Create(stream) as IUnknown, adPersistXML);
    Stream.Position := 0;
end;

Procedure GetQueryStream(Const s,ConStr:String;ms:TMemoryStream);
var
 AC:TAdoConnection;
 ads:TAdodataset;
begin
 AC:=TAdoConnection.Create(nil);
 try
 ads:=TAdodataset.Create(nil);
   try
     ads.Connection := AC;
     AC.ConnectionString := ConStr;
     ads.CommandText := s;
     ads.Open;
     RecordsetToXMLStream(ads.Recordset,ms);
   finally
     ads.Free
   end;
 finally
   AC.Free
 end;

end;

procedure TForm3.IdTCPServer1Execute(AContext: TIdContext);
var
 cmd:String;
 ms:TMemoryStream;
begin
     CoInitialize(nil);
     AContext.Connection.IOHandler.Readln(cmd);
     ms:=TMemoryStream.Create;
     try
     GetQueryStream('Select * from Adressen',DummyConnection.ConnectionString,ms);
     ms.Position := 0;
     SendStream(AContext,ms);
     AContext.Connection.Socket.CloseGracefully;
     finally
        ms.Free;
        CoUninitialize;
     end;

end;
end.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM