首页 > 其他分享 >delphi httpserver 使用方法

delphi httpserver 使用方法

时间:2023-03-14 20:55:40浏览次数:38  
标签:httpserver end edit delphi ARequestInfo TForm AResponseInfo main 方法

unit main;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, IdBaseComponent,IdContext ,IdComponent, IdCustomTCPServer, IdCustomHTTPServer,
  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
var
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('启动失败!');
  end;
  RootDir:=trim(edit_rootDir.Text);
  URL:='http://'+trim(edit_Ip.Text)+trim(edit_port.Text)+'/';
end;

procedure TForm_main.Button_stopClick(Sender: TObject);
begin
  IdHTTPServer1.Active := false;
end;

procedure TForm_main.IdHTTPServer1CommandGet(AContext: TIdContext;
  ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
var
  LFilename: string;
  LPathname: string;
  zhongwen:string;
begin
//浏览器请求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);
  end;
  LPathname := RootDir + LFilename;
  if FileExists(LPathname) then begin
      AResponseInfo.ContentStream := TFileStream.Create(LPathname, fmOpenRead + fmShareDenyWrite);//发文件

  end
  else
  begin
      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>';}

   //下载文件时,直接从网页打开而没有弹出保存对话框的问题解决
//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;
end.
————————————————
版权声明:本文为CSDN博主「云中雁012345」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/peixiaobin_blog/article/details/50894089

标签:httpserver,end,edit,delphi,ARequestInfo,TForm,AResponseInfo,main,方法
From: https://www.cnblogs.com/xionda/p/17216343.html

相关文章

  • Markdown使用方法
    Markdown学习二级标题三级标题标题:#+空格+标题名字,然后回车二级标题:两个##字体Hello,World!Hello,World!Hello,World!Hello,World!引用这是一个应用(>+空格)......
  • 数据分析学习-常用分析方法-总结二
    数据分析—常用分析方法一、5W2H分析方法5w:what(是什么)、when(何时)、where(何地)、why(为什么)、who(是谁)2h:how(怎么做)、howmuch(多少钱)案例一:如何是一款产品what:这是什么产......
  • could not find version 9.6 of matlab runtime解决方法
    背景描述:用Qt执行matlab做的.exe程序文件,报如下图错误,couldnotfindversion9.6ofthematlabRuntime.电脑上原装的是matlab2017a对应的版本是9.2解决方案:去该链接下......
  • 如何修改Tomcat默认端口号8080的方法
     版权如何修改Tomcat默认端口号8080的方法Tomcat默认端口是8080,如果你电脑无法访问localhost:8080,那么很可能就是端口占用,我们就需要修改端口号。可以在tomcat配置文件中......
  • Calibre无法输入字母和字符解决方法
     在使用Calibre时,发现键盘输入不好使。原因:低版本Calibre和ibus输入引擎不兼容。解决方法:1、在进入virtouoso之前要sourcebashrc这个文件,在bashrc文件中加入......
  • QT路径获取方法
    路径获取方法: //获取应用程序当前工作目录的绝对路径   QStringcurrentPath=QDir::currentPath()+"/1.txt";   qDebug()<<currentPath;   qDebug()<<Q......
  • 关于antd Upload上传预览模糊解决方法
    1.先看看转换的代码1.base64转换为文件dataURLtoFile(dataurl,filename){constarr=dataurl.split(',');constmime=arr[0].match(/:(.*?);/)[1];......
  • Steam流中的sorted方法排序
    使用方法packagecom.sports.basketball.controller.dto;importjava.util.ArrayList;importjava.util.Comparator;importjava.util.List;importjava.util.stream.Col......
  • DELPHI + uniGUI 开发CentOS环境下的Apache模块遇到的问题
    一直以来,用uniGUI做开发都比较顺手,但是缺点是这样做的只能在Windows环境下使用。而我们现在租用的虚拟服务器,往往都是CentOS的环境。而DELPHI+uniGUI能不能做LINUX的应......
  • CAD文字标注怎么调字体大小?CAD标注字体调整方法
    当CAD图纸中的文字标注太大或太小的时候,便需要设计师对图纸中的CAD文字标注字体大小进行调整。那么,你知道CAD文字标注怎么调字体大小吗?本节教程小编就以浩辰CAD软件为例来......