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
  • 相关阅读:
    Oracle SQL语句收集
    SqlParameter In 查询
    SQL 性能优化
    Entity Framework
    【XLL API 函数】 xlfSetName
    【XLL API 函数】xlfUnregister (Form 2)
    【XLL API 函数】xlfUnregister (Form 1)
    【Excel 4.0 函数】REGISTER 的两种形式以及VBA等效语句
    【Excel 4.0 函数】REGISTER
    【Bochs 官方手册翻译】 第一章 Bochs介绍
  • 原文地址:https://www.cnblogs.com/studycode/p/13161323.html
Copyright © 2011-2022 走看看