標籤:
http://blog.csdn.net/xxkku521/article/details/16864759Delphi IdHttp組件+IdHttpServer組件實現檔案下載服務2013-11-21 18:15 2624人閱讀 評論(0) 收藏 舉報 分類: DELPHI(10)
著作權聲明:本文為博主原創文章,未經博主允許不得轉載。
[delphi] view plain copy
- 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 IdHttp組件+IdHttpServer組件實現檔案下載服務