Delphi IdHttp组件+IdHttpServer组件实现文件下载服务

uses idhttp,IdHTTPServer;

//idhttp组件提交下载请求
procedure TVodService.Button3Click(Sender: TObject); 
var 
    h:TIdhttp; 
    MyStream:TMemoryStream; 
    url:string;
begin 
    MyStream:=TMemoryStream.Create; 
    h:=Tidhttp.Create(nil); 
    url:='http://192.168.0.254:9003/GetIni';//请求地址       
    try 
        h.get(url,MyStream);//提交请求     except   
        Application.Messagebox('网络出错,请检查网络连接','出错框',MB_OK+MB_ICONERROR) ;
        MyStream.Free;
        h.free; 
        exit; 
    end; 
    MyStream.SaveToFile(extractfilepath(application.exename)+'System.ini'); 
    MyStream.Free; 
    h.free;
end;

//IdHttpSever组件响应请求
procedure TVodService.DataModuleCreate(Sender: TObject);//初始化IdHttpServer组件
var
    hport:integer;
    Binding : TIdSocketHandle;
begin
    try
        VodHttpServer.Bindings.Clear;
        Binding := VodHttpServer.Bindings.Add;
        Binding.Port:=9003;
        binding.IP:='192.168.0.254';
        VodHttpServer.Active:=true;
    except
        on e:Exception do
            begin
                 FrmMain_VodSer.write_Logfile('加载APP设置error '+e.message);
            end;
    end;
end; 


procedure TVodService.VodHttpServerCommandGet(AThread: TIdPeerThread;
  ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);//响应请求
var
    ByteSent: Cardinal;
    LocalDoc:string;
    tempstr,ips:string;
    len,sport:integer;
begin
    tempstr:=ARequestInfo.Document; //获取请求字符串
    ips:=TIdIOHandlerSocket(AThread.Connection.IOHandler).Binding.PeerIP;//获取请求地址
    sport:=TIdIOHandlerSocket(AThread.Connection.IOHandler).Binding.PeerPort;//获取请求端口
    if fileexists(extractfilepath(application.exename)+'System.ini') then
    begin
        LocalDoc:=extractfilepath(application.exename)+'System.ini';
        ByteSent :=VodHttpServer.ServeFile(AThread, AResponseInfo, LocalDoc);
    end else
    begin
        Application.Messagebox('没有找到文件System.ini!','提示框',MB_OK+MB_ICONERROR) ;
    end;
end;


 

你可能感兴趣的:(DELPHI)