Delphi 封装的TIdHTTP GET POST 请求


本文整理自网络,侵删。

 

uses idhttp;

/// <summary> 

/// 向指定URL发起Get请求 

/// </summary> 

/// <param name="http">TIdHTTP</param> 

/// <param name="URL">指定URL</param> 

/// <param name="Max">Get请求失败最大重试次数</param> 

/// <returns>返回腾讯服务器响应(string类型的json格式数据)</returns> 

function GetMethod(http: TIdHTTP; URL: String; Max: Integer): String;

 

var 

  RespData: TStringStream; 

begin 

  RespData := TStringStream.Create('', TEncoding.UTF8); 

  try 

    try 

      http.Get(URL, RespData); 

      http.Request.Referer := URL; 

      Result := RespData.DataString; 

    except 

      Dec(Max); 

      if Max = 0 then 

      begin 

        Result := ''; 

        exit; 

      end; 

      Result := GetMethod(http, URL, Max); 

    end; 

  finally 

    FreeAndNil(RespData); 

  end; 

end; 

 

/// <summary> 

/// 向指定URL提交数据(Post) 

/// </summary> 

/// <param name="http">TIdHTTP</param> 

/// <param name="URL">指定URL</param> 

/// <param name="Data">要提交的数据(UTF8String)</param> 

/// <param name="Max">Post请求失败最大重试次数</param> 

/// <returns>返回腾讯服务器响应(string类型的json格式数据)</returns> 

function PostMethod(http: TIdHTTP; URL: String; Data: UTF8String; 

    Max: Integer): String; 

var 

  PostData, RespData: TStringStream; 

begin 

  RespData := TStringStream.Create(''); 

  PostData := TStringStream.Create(Data); 

  try 

    try 

      if http = nil then 

        exit; 

      http.Post(URL, PostData, RespData); 

      Result := RespData.DataString; 

      http.Request.Referer := URL; 

    except 

      Dec(Max); 

      if Max = 0 then 

      begin 

        Result := ''; 

        exit; 

      end; 

      Result := PostMethod(http, URL, Data, Max); 

    end; 

  finally 

    http.Disconnect; 

    FreeAndNil(RespData); 

    FreeAndNil(PostData); 

  end; 

end; 

相关阅读 >>

Delphi (user agent) of a twebbrowser

Delphi unigui执行程序部署有3种形式

Delphi datetimetostr函数专用优化版

Delphi 如何取得系统中的桌面的路径

Delphi 资源管理器套件

Delphi windows 编程[8] - wm_paint 消息

Delphi 提取字符串中所有数字

Delphi fdconnection取得excel工作表名

如何判断硬盘是fat32还是ntfs

Delphi结构,字符串和指针

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



打赏

取消

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

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

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

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

评论

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