Delphi idhttpserver的使用方法


本文整理自网络,侵删。

 
idhttpserver的使用方法

1)CommandGet(AContext: TIdContext; ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);事件

该事件和IDTCPSERVER的EXECUTE()事件一样,都是“线程方法”,即事件是在子线程里面执行的,在其内书写代码要注意“线程保护”。

该事件可以接受客户端HTTP GET、POST或其他HTTP方法请求。

if SameText(ARequestInfo.Command, 'post') then    // http post方法

else if SameText(ARequestInfo.Command, 'get') then    // http get方法

2)获取URL参数

ARequestInfo.Params.Values['sql']

3)回复客户端

回复客户端流

AResponseInfo.ContentStream := LFiredac.QuerySQL(ARequestInfo.Params.Values['sql'], ARequestInfo.Params.Values['storageformat']);  // 流
AResponseInfo.WriteContent;

回复客户端字符串

AResponseInfo.ContentText := LFiredac.SaveData(ARequestInfo.Params.Values['tablename'], ARequestInfo.PostStream, ARequestInfo.Params.Values['storageformat']);
AResponseInfo.WriteContent;

4)回复客户端中文字符串不乱码

AResponseInfo.ContentText := LFiredac.RestQuery(ARequestInfo.Params.Values['sql']);
AResponseInfo.ContentType := 'text/html; charset=GB2312';
AResponseInfo.WriteContent;

5)PostStream(AContext: TIdContext; AHeaders: TIdHeaderList; var VPostStream: TStream);事件

如果要接受客户端发过来的流,则在此事件中要写如下代码:

begin
VPostStream := TMemoryStream.Create; 
end;
来源:https://www.cnblogs.com/hnxxcxg/p/9929330.html



参考二:
前几天研究了下post的方式和服务器的接收处理,环境delphi10.1,首先说客户端post数据,采用idhttp,有两种方式,一种是字符串,一种是流如下:

1.psot字符串的方式

var
  s: string;
  stm: TStringStream; //接收返回内容
  send:TStringList;   //post的内容
begin
  send:=TStringList.Create;

  send.Text:='要post的内容';
  //send.add('name=张三'); //也可以按照不同的参数赋值
  //send.add('sex=男');
  stm := TStringStream.Create(s, TEncoding.UTF8);

  stm.Position := 0;
  try
    IdHTTP1.Post(url, send , stm);
    Memo2.Lines.Text:=stm.DataString; //服务端返回的内容
  except
    result := false;
  end;
  stm.Free;

  send.free;

2.通过流的方式post

var
  s,res:string;
  stm:TStringStream;
begin
  s:='要post的内容,比如说xml格式的文本';
  stm := TStringStream.Create(s, TEncoding.UTF8);
  stm.Position := 0;
  try
    res:=IdHTTP1.Post('url地址', stm);
    Memo2.Lines.Text:=res; //服务端返回的内容
  finally
    stm.Free;

  end;

下面是服务端接收处理,服务端采用IdHTTPServer,在OnCommandGet事件中处理数据

 ARequestInfo.ContentType := 'text/html;Charset=UTF-8';
  path := ARequestInfo.Document; //访问的路径
  if UpperCase(ARequestInfo.command) = 'POST' then

  begin
 //接收post字符串的处理
 // memo1.Lines.Add(arequestinfo.Params.Values['name']) ;//按参数名称接收
// memo1.Lines.Add(arequestinfo.Params.Values['sex']); 

//    Memo1.Lines.Add('FormParams:'+ARequestInfo.FormParams); //所有数据
//    Memo1.Lines.Add('Params:'+ARequestInfo.Params.Text);
//    aRequestContent:= ARequestInfo.UnparsedParams;
    aRequestContent := ARequestInfo.Params.GetText;
   //下面是接收数据流的处理过程

   // if (ARequestInfo.PostStream <> nil) and (ARequestInfo.PostStream.Size > 0) then
   // bergin
   //      ARequestInfo.PostStream.Position := 0;

   //      aRequestContent := StreamToString(ARequestInfo.PostStream);
   // end;

   aRequestContent := tiduri.URLDecode(aRequestContent);  //解决汉字乱码问题
// 数据处理过程

......

AResponseInfo.ContentType := 'text/html';
AResponseInfo.CharSet := 'utf-8';
AResponseInfo.ContentText := '根据处理过程返回客户端信息';
AResponseInfo.WriteContent;
――――――――――――――――
原文链接:https://blog.csdn.net/zflybird/article/details/80202485

相关阅读 >>

Delphi 修改ie首页代码

Delphi 利用sendinput模拟鼠标键盘

Delphi 三种方式读取txt文本文件

Delphi 显示程序占用内存多少

Delphi 最全_日期格式_dateutils时间单元说明

Delphi sqlite 简明上手指南

Delphi getwebbrowserhtml 获取网页源代码

Delphi 用image 画图

Delphi richedit控件的用法

Delphi 泛型容器单元(generics.collections) tdictionary<t>

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...