• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

delphiTidhttp发送json格式报文

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
type
  TwmsThreadpostJson = class(TThread)
  private
    Furl: string;
    Fpostcmd: string;
    FResult: string;
    FBOOL: Boolean;
  protected
    procedure Execute; override;
  end;

class function TWmsApplicationfunction.postjson(url, postcmd: string): string;
var
  FThread: TwmsThreadpostJson;
begin
  if booleanshowflash then
    cxSetSplashVisibility(Application.MainForm <> nil, '');
  try
    FThread := TwmsThreadpostJson.Create;
    FThread.Furl := url;
    FThread.Fpostcmd := postcmd;
    FThread.FBOOL := False;
    FThread.Resume;

    while true do
    begin
      Application.ProcessMessages;
      if FThread.FBOOL then        //等待子线程执行完毕
      begin
        cxSetSplashVisibility(False, '');
        Result := FThread.FResult;
        FThread.Free;
        Break;
      end;
    end;
  except

  end

procedure TwmsThreadpostJson.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 := Tyundawms.getwmsuser;
      Strtemp := inttostr(ConvertDelphiDateTimeToJavaDateTime(now));
      try
        AJson.AddVariant('dateTime', Strtemp);
      except

      end;

      strData := Tyundawms.getwmsuser + Tyundawms.getwmspassword + Strtemp;
      AJson.AddVariant('validation', TWmsApplicationfunction.Getmd5(strData));
      passuess := true;
      Fpostcmd := AJson.AsString;
      if passuess = true then
        strStream := TStringStream.create(Fpostcmd, TEncoding.UTF8)
      else
        exit;
    finally
      AJson.Free;
    end;
    try
      yundawms.AppService.GeneralUtility.retmain('log:post' + ' url:' + Furl + ' date:' + Fpostcmd);
    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
      yundawms.AppService.GeneralUtility.retmain('log:get' + ' url:' + Furl + ' resultdate:' + Fresult);
    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;

转:https://www.cnblogs.com/yangxuming/p/8086779.html


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Matlab统计工具箱应用(III)发布时间:2022-07-18
下一篇:
C#与matlab混合编程(1)多元线性回归发布时间:2022-07-18
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap