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

时间:2021-05-28 00:34:38
 http://blog.csdn.net/xxkku521/article/details/16864759

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

2013-11-21 18:15 2624人阅读 评论(0) 收藏 举报
Delphi IdHttp组件+IdHttpServer组件实现文件下载服务 分类:
DELPHI(10) Delphi IdHttp组件+IdHttpServer组件实现文件下载服务

版权声明:本文为博主原创文章,未经博主允许不得转载。

  1. uses idhttp,IdHTTPServer;
  2. //idhttp组件提交下载请求
  3. procedure TVodService.Button3Click(Sender: TObject);
  4. var
  5. h:TIdhttp;
  6. MyStream:TMemoryStream;
  7. url:string;
  8. begin
  9. MyStream:=TMemoryStream.Create;
  10. h:=Tidhttp.Create(nil);
  11. url:='http://192.168.0.254:9003/GetIni';//请求地址
  12. try
  13. h.get(url,MyStream);//提交请求     except
  14. Application.Messagebox('网络出错,请检查网络连接','出错框',MB_OK+MB_ICONERROR) ;
  15. MyStream.Free;
  16. h.free;
  17. exit;
  18. end;
  19. MyStream.SaveToFile(extractfilepath(application.exename)+'System.ini');
  20. MyStream.Free;
  21. h.free;
  22. end;
  23. //IdHttpSever组件响应请求
  24. procedure TVodService.DataModuleCreate(Sender: TObject);//初始化IdHttpServer组件
  25. var
  26. hport:integer;
  27. Binding : TIdSocketHandle;
  28. begin
  29. try
  30. VodHttpServer.Bindings.Clear;
  31. Binding := VodHttpServer.Bindings.Add;
  32. Binding.Port:=9003;
  33. binding.IP:='192.168.0.254';
  34. VodHttpServer.Active:=true;
  35. except
  36. on e:Exception do
  37. begin
  38. FrmMain_VodSer.write_Logfile('加载APP设置error '+e.message);
  39. end;
  40. end;
  41. end;
  42. procedure TVodService.VodHttpServerCommandGet(AThread: TIdPeerThread;
  43. ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);//响应请求
  44. var
  45. ByteSent: Cardinal;
  46. LocalDoc:string;
  47. tempstr,ips:string;
  48. len,sport:integer;
  49. begin
  50. tempstr:=ARequestInfo.Document; //获取请求字符串
  51. ips:=TIdIOHandlerSocket(AThread.Connection.IOHandler).Binding.PeerIP;//获取请求地址
  52. sport:=TIdIOHandlerSocket(AThread.Connection.IOHandler).Binding.PeerPort;//获取请求端口
  53. if fileexists(extractfilepath(application.exename)+'System.ini') then
  54. begin
  55. LocalDoc:=extractfilepath(application.exename)+'System.ini';
  56. ByteSent :=VodHttpServer.ServeFile(AThread, AResponseInfo, LocalDoc);
  57. end else
  58. begin
  59. Application.Messagebox('没有找到文件System.ini!','提示框',MB_OK+MB_ICONERROR) ;
  60. end;
  61. end;

相关文章