微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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;
<span style="font-family:Arial,Helvetica,sans-serif;"><span style="white-space: normal;">
</span></span>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐