zoukankan      html  css  js  c++  java
  • Delphi IDHTTP控件:GET/POST 请求

    一、GET 请求

     procedure GetDemo;
     2 var
     3   IdHttp : TIdHTTP;
     4   Url : string;//请求地址
     5   ResponseStream : TStringStream; //返回信息
     6   ResponseStr : string;
     7 begin
     8   //创建IDHTTP控件
     9   IdHttp := TIdHTTP.Create(nil);
    10   //TStringStream对象用于保存响应信息
    11   ResponseStream := TStringStream.Create('');
    12   try
    13     //请求地址
    14     Url := 'http://dict.youdao.com/';
    15     try
    16       IdHttp.Get(Url,ResponseStream);
    17     except
    18       on e : Exception do
    19       begin
    20         ShowMessage(e.Message);
    21       end;
    22     end;
    23     //获取网页返回的信息
    24     ResponseStr := ResponseStream.DataString;
    25     //网页中的存在中文时,需要进行UTF8解码
    26     ResponseStr := UTF8Decode(ResponseStr);
    27   finally
    28     IdHttp.Free;
    29     ResponseStream.Free;
    30   end;   
    31 end;

    如果Get需要添加请求参数,则直接在地址后添加,各参数间用&连接

    如:http://dict.youdao.com?param1=1&param2=2

    二、Post 请求

    1 procedure PostDemo;
     2 var
     3   IdHttp : TIdHTTP;
     4   Url : string;//请求地址
     5   ResponseStream : TStringStream; //返回信息
     6   ResponseStr : string;
     7 
     8   RequestList : TStringList;     //请求信息
     9   RequestStream : TStringStream;
    10 begin
    11   //创建IDHTTP控件
    12   IdHttp := TIdHTTP.Create(nil);
    13   //TStringStream对象用于保存响应信息
    14   ResponseStream := TStringStream.Create('');
    15 
    16   RequestStream := TStringStream.Create('');
    17   RequestList := TStringList.Create;
    18   try
    19     Url := 'http://f.youdao.com/?path=fanyi&vendor=fanyiinput';
    20     try
    21       //以列表的方式提交参数
    22       RequestList.Add('text=love');
    23       IdHttp.Post(Url,RequestList,ResponseStream);
    24 
    25       //以流的方式提交参数
    26       RequestStream.WriteString('text=love');
    27       IdHttp.Post(Url,RequestStream,ResponseStream);
    28     except
    29       on e : Exception do
    30       begin
    31         ShowMessage(e.Message);
    32       end;
    33     end;
    34     //获取网页返回的信息
    35     ResponseStr := ResponseStream.DataString;
    36     //网页中的存在中文时,需要进行UTF8解码
    37     ResponseStr := UTF8Decode(ResponseStr);
    38   finally
    39     IdHttp.Free;
    40     RequestList.Free;
    41     RequestStream.Free;
    42     ResponseStream.Free;
    43   end;
    44 end;

    Post请求在网页中多使用List形式提交参数。

    不过在一些API中规定了POST的请求格式为 JSON 格式或 XML,这是需要注意发起请求前需要先设置 ContentType 属性,使用Stream方式提交

    已上面代码为例:

    提交 JSON 格式:IdHttp.Request.ContentType :='application/json';

    提交 XML 格式: IdHttp.Request.ContentType :='text/xml';

    如未按要求格式提交,一般会返回 HTTP 1.1 / 415

  • 相关阅读:
    Enterprise Solution 界面设计规范
    大型.NET商业软件代码保护技术 技术与实践相结合保护辛苦创造的劳动成果
    开源WinForms界面开发框架Management Studio 选项卡文档 插件 Office 2007蓝色风格 后台线程
    解析Visual Studio 2015促进生产力的10个新功能
    LLBL Gen Pro 4.2 Lite 免费的对象关系映射开发框架与工具
    Windows Server 2012部署Enterprise Solution 5.4
    ASP.NET MVC:some benefits of asp.net mvc
    Python:Opening Python Classes
    ASP.NET MVC:4 Ways To Prevent Duplicate Form Submission(转载)
    架构:The Onion Architecture : part 3(洋葱架构:第三篇)(转载)
  • 原文地址:https://www.cnblogs.com/jiangyuhu/p/12176643.html
Copyright © 2011-2022 走看看