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

猜你喜欢

转载自www.cnblogs.com/studycode/p/13161323.html