delphi idhttpsever

http://blog.csdn.net/chelen_jak/article/details/50203809

delphi idhttpsever

2015-12-07 11:36 216人阅读 评论(0) 收藏 举报

 分类:

Web(57)  Delphi(54) 

转自:http://3699119.blog.163.com/blog/static/167075351201373024054586/

[delphi] view plain copy

  1. unit main;
  2. interface
  3. uses
  4. Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  5. Dialogs, IdBaseComponent,IdContext ,IdComponent, IdCustomTCPServer, IdCustomHTTPServer,
  6. IdHTTPServer, StdCtrls;
  7. type
  8. TForm_main = class(TForm)
  9. IdHTTPServer1: TIdHTTPServer;
  10. Button_StartServer: TButton;
  11. Edit_Port: TEdit;
  12. Label1: TLabel;
  13. Label2: TLabel;
  14. Edit_Ip: TEdit;
  15. Button_stop: TButton;
  16. Label3: TLabel;
  17. Edit_RootDir: TEdit;
  18. Edit_index: TEdit;
  19. Label4: TLabel;
  20. procedure Button_StartServerClick(Sender: TObject);
  21. procedure Button_stopClick(Sender: TObject);
  22. procedure IdHTTPServer1CommandGet(AContext: TIdContext;
  23. ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
  24. private
  25. { Private declarations }
  26. public
  27. { Public declarations }
  28. end;
  29. var
  30. Form_main: TForm_main;
  31. implementation
  32. var
  33. RootDir:string;
  34. URL:string;
  35. {$R *.dfm}
  36. procedure TForm_main.Button_StartServerClick(Sender: TObject);
  37. begin
  38. try
  39. IdHTTPServer1.Bindings.Clear;
  40. //要绑定的端口,一定设置此项,这是真正要绑定的端口;
  41. IdHTTPServer1.DefaultPort:=strtoint(trim(edit_port.Text));
  42. IdHTTPServer1.Bindings.Add.IP := trim(edit_Ip.Text);
  43. //启动服务器
  44. IdHTTPServer1.Active := True;
  45. except
  46. showmessage(‘启动失败!‘);
  47. end;
  48. RootDir:=trim(edit_rootDir.Text);
  49. URL:=‘http://‘+trim(edit_Ip.Text)+trim(edit_port.Text)+‘/‘;
  50. end;
  51. procedure TForm_main.Button_stopClick(Sender: TObject);
  52. begin
  53. IdHTTPServer1.Active := false;
  54. end;
  55. procedure TForm_main.IdHTTPServer1CommandGet(AContext: TIdContext;
  56. ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
  57. var
  58. LFilename: string;
  59. LPathname: string;
  60. zhongwen:string;
  61. begin
  62. //浏览器请求http://127.0.0.1:8008/index.html?a=1&b=2
  63. //ARequestInfo.Document  返回    /index.html
  64. //ARequestInfo.QueryParams 返回  a=1b=2
  65. //ARequestInfo.Params.Values[‘name‘]   接收get,post过来的数据
  66. ////webserver发文件
  67. {LFilename := ARequestInfo.Document;
  68. if LFilename = ‘/‘ then
  69. begin
  70. LFilename := ‘/‘+trim(edit_index.Text);
  71. end;
  72. LPathname := RootDir + LFilename;
  73. if FileExists(LPathname) then
  74. begin
  75. AResponseInfo.ContentStream := TFileStream.Create(LPathname, fmOpenRead + fmShareDenyWrite);//发文件
  76. end
  77. else begin
  78. AResponseInfo.ResponseNo := 404;
  79. AResponseInfo.ContentText := ‘找不到‘ + ARequestInfo.Document;
  80. end;}
  81. //发html文件
  82. {AResponseInfo.ContentEncoding:=‘utf-8‘;
  83. AResponseInfo.ContentType :=‘text/html‘;
  84. AResponseInfo.ContentText:=‘<html><body>好</body></html>‘; }
  85. //发xml文件
  86. {AResponseInfo.ContentType :=‘text/xml‘;
  87. AResponseInfo.ContentText:=‘<?xml version="1.0" encoding="utf-8"?>‘
  88. +‘<students>‘
  89. +‘<student sex = "male"><name>‘+AnsiToUtf8(‘陈‘)+‘</name><age>14</age></student>‘
  90. +‘<student sex = "female"><name>bb</name><age>16</age></student>‘
  91. +‘</students>‘;}
  92. //下载文件时,直接从网页打开而没有弹出保存对话框的问题解决
  93. //AResponseInfo.CustomHeaders.Values[‘Content-Disposition‘] :=‘attachment; filename="‘+文件名+‘"‘;
  94. //替换 IIS
  95. {AResponseInfo.Server:=‘IIS/6.0‘;
  96. AResponseInfo.CacheControl:=‘no-cache‘;
  97. AResponseInfo.Pragma:=‘no-cache‘;
  98. AResponseInfo.Date:=Now;}
  99. end;
  100. end.
时间: 2024-12-28 20:23:37

delphi idhttpsever的相关文章

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

http://blog.csdn.net/xxkku521/article/details/16864759 Delphi IdHttp组件+IdHttpServer组件实现文件下载服务 2013-11-21 18:15 2624人阅读 评论(0) 收藏 举报  分类: DELPHI(10)  版权声明:本文为博主原创文章,未经博主允许不得转载. [delphi] view plain copy uses idhttp,IdHTTPServer; //idhttp组件提交下载请求 procedu

Delphi常用系统函数总结

字符串处理函数 Unit System 函数原型 function Concat(s1 [, s2,..., sn]: string): string; 说明 与 S := S1 + S2 + S3 ...; 相同. 将字符串相加. 函数原型 function Copy(S: string; Index, Count: Integer): string;说明 S : 字符串. Indexd : 从第几位开始拷贝. Count : 总共要拷贝几位. 从母字符串拷贝至另一个字符串. 函数原型 pro

最新的Delphi版本号对照

The CompilerVersion constant identifies the internal version number of the Delphi compiler. It is defined in the System unit and may be referenced either in code just as any other constant: if CompilerVersion = 20 then sCompilerName := 'Delphi 2009';

Delphi XE10 dxLayoutControl 控件应用指南

http://www.cnblogs.com/Bonny.Wong/p/7440288.html DevExpress VCL套件是一套非常强大的界面控件,可惜关于Delphi开发方面的说明太少,有些控件使用起来一头雾水,不知从何下手.本节详细介绍在Delphi Xe10 Seattle中如何利用dxLayoutControl 控件来做界面布局. 1.  首先从工具箱面板中将dxLayoutControl放在Form上,设置2个关键属性如下: 属性 属性值 说明 Align alClient 一

delphi 移动开发博客地址收集

这个是各位博主学习整理的笔记,很值得大家学习. XE2011的博客: http://www.cnblogs.com/xe2011/ 万一的博客:http://www.cnblogs.com/del/ 武稀松的博客:http://www.raysoftware.cn/ delphiteacher的博客:http://blog.csdn.net/DelphiTeacher 我一路走来的博客:http://blog.csdn.net/tingsking18/article/details/477210

Delphi使用android的NDK是通过JNI接口,封装好了,不用自己写本地代码,直接调用

一.Android平台编程方式:      1.基于Android SDK进行开发的第三方应用都必须使用Java语言(Android的SDK基于Java实现)      2.自从ndk r5发布以后,已经允许完全用C/C++ 来开发应用或者游戏,而不再需要编写任何Java 的代码   Android程序运行在Dalvik虚拟机中,NDK允许用户使用类似C / C++之类的原生代码语言执行部分程序. 二.跨平台移动开发   Delphi使用android的NDK是通过JNI接口,封装好了,不用自己

TStringBuilder类 - Delphi

摘自万一老师的博客,略作整理. //TStringBuilder.Create 可以无参数 procedure TForm1.Button1Click(Sender: TObject); var sb: TStringBuilder; begin sb := TStringBuilder.Create; sb.Append('Embarcadero'); sb.Append(' CodeGear'); sb.Append(' Delphi'); sb.Append(' 2009'); ShowM

delphi 属性 参数 新注释

delphi 属性 参数 新注释,在写代码的时候,可以自动看到属性.参数的的备注说明,太方便了. Tmyclass=class /// <summary> /// 姓名 /// </summary> name:string; /// <summary> /// 性别 /// </summary> sex:string; end; var aclass: Tmyclass; begin aclass.name; aclass.sex; 鼠标放上去的时候提示 写

[转]delphi的procedure of object

delphi的procedure of object(一个特殊的指针类型) 理论:     //适用于实现不是某一特定过程或函数          type                TNotifyEvent = procedure(Sender: TObject) of object;       首先:procedure 也是类型,可以理解为过程类型,定义过程的参数结构,而具体的实现可以动态赋值  onclick那样例子:      声明:  onclick= procedure(Sen