delphi Tidhttp 发送json格式报文  
官方Delphi 学习QQ群: 682628230(三千人)
频道

delphi Tidhttp 发送json格式报文


type

  TmyThreadpostJson = class(TThread)

  private

    Furl: string;

    Fpostcmd: string;

    FResult: string;

    FBOOL: Boolean;

  protected

    procedure Execute; override;

  end;


class function TmyApplicationfunction.postjson(url, postcmd: string): string;

var

  FThread: TmyThreadpostJson;

begin

try

    FThread := TmyThreadpostJson.Create;

    FThread.Furl := url;

    FThread.Fpostcmd := postcmd;

    FThread.FBOOL := False;

    FThread.Resume;


    while true do

    begin

      Application.ProcessMessages;

      if FThread.FBOOL then        //等待子线程执行完毕

      begin        Result := FThread.FResult;

        FThread.Free;

        Break;

      end;

    end;

  except


  end


procedure TmyThreadpostJson.Execute;

var

  strStream: TStringStream;

  strStream1: TStringStream;

  IdHTTP: Tidhttp;

  AJson: TQJson;

  passuess: boolean;

  strData: string;

  Strtemp: string;

  strRadom: string;

  strDes: string;

  pub, pri: TFGInt;

begin

  Fresult := '';

  IdHTTP := Tidhttp.create(nil);

  try

    IdHTTP.Request.CustomHeaders.clear;

    IdHTTP.Request.RawHeaders.values['Cookie'] := yundawms.returncookek;

    IdHTTP.HandleRedirects := true;

    IdHTTP.Request.CustomHeaders.Add('Cookie: ' + yundawms.returncookek);

    IdHTTP.Request.Connection := 'Keep-Alive';

    IdHTTP.Request.UserAgent := 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Maxthon)';

    // IdHTTP.Request.ContentType:='application/x-www-form-urlencoded;charset=UTF-8';

    IdHTTP.Request.Accept := 'image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/msword, */*';

    IdHTTP.Request.AcceptLanguage := 'zh-cn';

    IdHTTP.Request.AcceptEncoding := 'gzip, deflate';

    IdHTTP.Request.CacheControl := 'no-cache';

    IdHTTP.ReadTimeout := 1200000;

    IdHTTP.ConnectTimeout := 1200000;

    IdHTTP.Request.ContentType := 'application/json;charset=UTF-8';

    IdHTTP.Request.AcceptCharSet := 'UTF-8';

    IdHTTP.Request.AcceptEncoding := 'UTF-8';

    IdHTTP.Request.AcceptLanguage := 'UTF-8';

    IdHTTP.Request.CharSet := 'UTF-8';

    // IdHTTP1.CookieManager  := IdCookieManager1;

    IdHTTP.HTTPOptions := IdHTTP.HTTPOptions + [hoKeepOrigProtocol]; // 关键这行

    IdHTTP.ProtocolVersion := pv1_1;

    AJson := TQJson.Create;

    passuess := false;

    try

      AJson.Parse(Fpostcmd);

      if Assigned(AJson.ItemByPath('username')) then

        AJson.ItemByPath('username').value := 'jack';

      Strtemp := inttostr(ConvertDelphiDateTimeToJavaDateTime(now));

      try

        AJson.AddVariant('dateTime', Strtemp);

      except


      end;


      strData := 'jack' + '123456' + Strtemp;

      AJson.AddVariant('validation', TmyApplicationfunction.Getmd5(strData));

      passuess := true;

      Fpostcmd := AJson.AsString;

      if passuess = true then

        strStream := TStringStream.create(Fpostcmd, TEncoding.UTF8)

      else

        exit;

    finally

      AJson.Free;

    end;

    try

     //记录日志啥。except

//      raise Exception.Create('postjson:记录日志异常!'); 

    end;


    strStream1 := TStringStream.create('', TEncoding.UTF8);

    try

      IdHTTP.Post(Furl, strStream, strStream1);

      Fresult := strStream1.DataString;

      

      Fresult := StringReplace(Fresult, '\u008F', '', [rfReplaceAll]);  //含有的特殊字符 替换为空

      Fresult := StringReplace(Fresult, '\u00A0', ' ', [rfReplaceAll]); //含有全角字符替换   全角字符会乱码

    except

      raise Exception.Create('postjson:服务器链接失败,请检查【网络】是否正常!');

      Fresult := '';

    end;


    try

      //记录日志except

    end;

  finally

    IdHTTP.Disconnect;

    if Assigned(IdHTTP) then

      freeandnil(IdHTTP);

    if Assigned(strStream) then

      freeandnil(strStream);

    if Assigned(strStream1) then

      freeandnil(strStream1);

    FBOOL := True;

  end;

end;



推荐分享
图文皆来源于网络,内容仅做公益性分享,版权归原作者所有,如有侵权请告知删除!
 

Copyright © 2014 DelphiW.com 开发 源码 文档 技巧 All Rights Reserved
晋ICP备14006235号-8 晋公网安备 14108102000087号

执行时间: 0.049730062484741 seconds