zoukankan      html  css  js  c++  java
  • Delphi:HttpPost

    function doHttpPost(const aUrl, aJson: string): string;
    var
      IDHttp: TIdHTTP;
      requestBody: TStringStream;
    begin
      Result := EmptyStr;
      try
        try
          IDHttp := TIdHTTP.Create(nil);
          IDHttp.Request.ContentType := 'application/json';
          requestBody := TStringStream.Create(aJson);
          Result := AnsiLowerCase(Utf8ToAnsi(IDHttp.Post(aUrl, requestBody)));
        except
          on e: Exception do
          begin
            MyWriteLog('[POST]接口请求异常!' + e.Message);
          end;
        end;
      finally
        freeandnil(IDHttp);
        freeandnil(requestBody);
      end;
    end;
    View Code
    uses IdHashMessageDigest,IdHTTP,IdSSLOpenSSL,HTTPApp;
    
    lv_sPosCmd := TStringList.Create;
         lv_sPosCmd.Add('appCode=' + g_sAppCode);
          lv_sPosCmd.Add('service=' + C_SERVICE_QUERY_STOCK_DAY);
          lv_sPosCmd.Add('msgBody=' + lv_sMsgBody);
          lv_sPosCmd.Add('sign=' + lv_sSign);
          lv_sPosCmd.Add('requestTime=' + lv_sTime);
          lv_sPosCmd.Add('batchNo=' + lv_sBatchNo);
          MyWriteLog('提交接口数据:' + StringReplace(lv_sPosCmd.Text,#$D#$A,',',[rfReplaceAll]));
    
    function PostData(const pviUrl: string; const pviData: TStringList; var pvoData, lv_sMsg: string): Boolean;
    var
      idHttp: TIdHTTP;
    begin
      Result := False;
      try
        pvoData := '';
        lv_sMsg := '默认提示消息!';
        idHttp := TIdHTTP.Create(nil);
        try
          idHttp.Request.ContentType := 'application/x-www-form-urlencoded';      ///json
          idHttp.Request.ContentEncoding := 'utf-8';
          pvoData := Utf8ToAnsi(idHttp.Post(pviUrl, pviData));
          Result := True;
        finally
          FreeAndNil(idHttp);
        end;
      except
        on E: Exception do
        begin
          lv_sMsg := '提交URL接口异常:' + #13#10 + e.Message;
          MyWriteLog(lv_sMsg);
          Exit;
        end;
      end;
    end;
    View Code
    function doHttpPost(const aUrl, aJson: string): string;
    var
      IDHttp: TIdHTTP;
      requestBody: TStringStream;
    begin
      Result := EmptyStr;
      try
        try
          IDHttp := TIdHTTP.Create(nil);
          IDHttp.HandleRedirects := True;
          IDHttp.Request.CustomHeaders.Clear;
          IDHttp.Request.ContentType := 'application/json';
          if g_sToken <> '' then
            IDHttp.Request.CustomHeaders.Add('access-token:' + g_sToken);
          requestBody := TStringStream.Create(aJson);
          Result := Utf8ToAnsi(IDHttp.Post(aUrl, requestBody));  //AnsiLowerCase
        except
          on e: Exception do
          begin
            MyWriteLog('[POST]接口请求异常!' + e.Message);
          end;
        end;
      finally
        freeandnil(IDHttp);
        freeandnil(requestBody);
      end;
    end;
    View Code
    function doHttpPost(const pviUrl: string; const pviData: TStringList; var pvoData, lv_sMsg: string): Boolean;
    var
      idHttp: TIdHTTP;
    begin
      Result := False;
      try
        pvoData := '';
        lv_sMsg := '默认提示消息!';
        idHttp := TIdHTTP.Create(nil);
        try
          idHttp.Request.CustomHeaders.Clear;
          idHttp.Request.ContentType := 'application/x-www-form-urlencoded';      ///json
          idHttp.Request.ContentEncoding := 'utf-8';
          if g_sToken <> '' then
            idHttp.Request.CustomHeaders.Add('access-token:' + g_sToken);
          pvoData := Utf8ToAnsi(idHttp.Post(pviUrl, pviData));
          Result := True;
        finally
          FreeAndNil(idHttp);
        end;
      except
        on E: Exception do
        begin
          lv_sMsg := '提交URL接口异常:' + #13#10 + e.Message;
          MyWriteLog(lv_sMsg);
          Exit;
        end;
      end;
    end;
    View Code
  • 相关阅读:
    [备份]部分常用函数
    [考试]20150904
    [考试]20150903
    [未完成][知识点]动态规划优化初步
    [考试]20150822
    [考试]20150821
    [知识点]后缀数组
    [考试]20150816
    [考试]20150815
    BZOJ2815: [ZJOI2012]灾难
  • 原文地址:https://www.cnblogs.com/studycode/p/13161323.html
Copyright © 2011-2022 走看看