interface uses windows,Messages,SysUtils,Variants,Classes,Graphics,Controls,Forms, Dialogs,IDBaseComponent,IDContext,IDComponent,IDCustomTcpserver,IDCustomhttpServer,u5b8bu4f53; Font-size:16px"> IDhttpServer,StdCtrls; type TForm_main = class(TForm) IDhttpServer1: TIDhttpServer; button_StartServer: Tbutton; Edit_Port: TEdit; Label1: TLabel; Label2: TLabel; Edit_Ip: TEdit; button_stop: Tbutton; Label3: TLabel; Edit_RootDir: TEdit; Edit_index: TEdit; Label4: TLabel; procedure button_StartServerClick(Sender: TObject); procedure button_stopClick(Sender: TObject); procedure IDhttpServer1CommandGet(AContext: TIDContext; ARequestInfo: TIDhttpRequestInfo; AResponseInfo: TIDhttpResponseInfo); private { Private declarations } public { Public declarations } end; var Form_main: TForm_main; implementation RootDir:string; URL:string; {$R *.dfm} procedure TForm_main.button_StartServerClick(Sender: TObject); begin try IDhttpServer1.Bindings.Clear; //要绑定的端口,一定设置此项,这是真正要绑定的端口; IDhttpServer1.DefaultPort:=strtoint(trim(edit_port.Text)); IDhttpServer1.Bindings.Add.IP := trim(edit_Ip.Text); //启动服务器 IDhttpServer1.Active := True; except showmessage('启动失败!'); RootDir:=trim(edit_rootDir.Text); URL:='http://'+trim(edit_Ip.Text)+trim(edit_port.Text)+'/'; end; procedure TForm_main.button_stopClick(Sender: TObject); IDhttpServer1.Active := false; procedure TForm_main.IDhttpServer1CommandGet(AContext: TIDContext; ARequestInfo: TIDhttpRequestInfo; AResponseInfo: TIDhttpResponseInfo); Lfilename: string; LPathname: string; zhongwen:string; //浏览器请求http://127.0.0.1:8008/index.HTML?a=1&b=2 //ARequestInfo.document 返回 /index.HTML //ARequestInfo.queryParams 返回 a=1b=2 //ARequestInfo.Params.Values['name'] 接收get,post过来的数据 ////webserver发文件 {Lfilename := ARequestInfo.document; if Lfilename = '/' then begin Lfilename := '/'+trim(edit_index.Text); LPathname := RootDir + Lfilename; if fileExists(LPathname) then begin AResponseInfo.ContentStream := TfileStream.Create(LPathname,fmOpenRead + fmShareDenyWrite);//发文件 end else AResponseInfo.ResponseNo := 404; AResponseInfo.ContentText := '找不到' + ARequestInfo.document; end; } //发HTML文件 {AResponseInfo.ContentEnCoding:='utf-8'; AResponseInfo.ContentType :='text/HTML'; AResponseInfo.ContentText:='<HTML><body>好</body></HTML>'; } //发xml文件 {AResponseInfo.ContentType :='text/xml'; AResponseInfo.ContentText:='<?xml version="1.0" enCoding="utf-8"?>' +'<students>' +'<student sex = "male"><name>'+AnsiToUtf8('陈')+'</name><age>14</age></student>' +'<student sex = "female"><name>bb</name><age>16</age></student>' +'</students>';} //下载文件时,直接从网页打开而没有d出保存对话框的问题解决 //AResponseInfo.Customheaders.Values['Content-disposition'] :='attachment; filename="'+文件名+'"'; //替换 IIS {AResponseInfo.Server:='IIS/6.0'; AResponseInfo.CacheControl:='no-cache'; AResponseInfo.Pragma:='no-cache'; AResponseInfo.Date:=Now;} end. 总结
以上是内存溢出为你收集整理的delphi httpserver 使用方法全部内容,希望文章能够帮你解决delphi httpserver 使用方法所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)