Delphi XE7开发的获取网页中字符串的编码是否是UTF8


本文整理自网络,侵删。

 如果没有文件的头信息,那么需要直接通过字符串进行判断,代码通过IsUTF8String这个函数进行判断,最终得出结果。

所有的代码


procedure TForm1.Button1Click(Sender: TObject);

var

  mstream: TMemoryStream;

  ustream: TStringStream;

  astream: TStringStream;

  stream: TStringStream;

  idHttpObj: TIdHTTP;

  ssss: TStringList;

  url: sTring;

  ss: string;

  rs: RawByteString;

  aByte: Byte;

  isFoundCode: Boolean;

begin

  url := 'http://www.dfwlt.com'; // UTF8

  mstream := TMemoryStream.Create;

  try

     idHttpObj := TIdHTTP.Create(nil);

      try

        Memo1.Lines.Clear;

        Memo1.Lines.add(url);

        Memo1.Lines.add('正在读取请稍后...');

        Memo1.Lines.add('');

        idHttpObj.Get(url, mstream);

        mstream.Position := 0;

        Memo1.Lines.Clear;

        mstream.Read(aByte, 1);


        isFoundCode := False;

        if aByte = $FF then

        begin

          mstream.Read(aByte, 1);

          if aByte = $EF then

          begin

            isFoundCode := True;

          end;

        end

        else if aByte = $EF then

        begin

          mstream.Read(aByte, 1);

          if aByte = $BB then

          begin

            mstream.Read(aByte, 1);

            if aByte = $BF then

            begin

              isFoundCode := True;

            end;

          end

          else if aByte = $FF then

          begin

            isFoundCode := True;

          end;

        end;

        mstream.Position := 0;


        if isFoundCode then

        begin

          //自动识别 BOM

          Memo1.Lines.LoadFromStream(mstream);

        end

        else

        begin

          //没有 BOM 猜吧。

          ustream := TStringStream.Create('', TEncoding.Unicode);

          try

            ustream.CopyFrom(mstream, 0);

            astream := TStringStream.Create('', TEncoding.ANSI);

            try

              mstream.Position := 0;

              astream.CopyFrom(mstream, 0);

              if ustream.DataString <> astream.DataString then

              begin

                SetLength(rs, Length(astream.Bytes) + 1);

                FillChar(rs[Low(string)], Length(rs), 0);

                Move((@astream.Bytes[0])^, rs[Low(string)], Length(astream.Bytes));

                if IsUTF8String(rs) then

                begin

                  stream := TStringStream.Create('', TEncoding.UTF8);

                  try

                    mstream.Position := 0;

                    stream.CopyFrom(mstream, 0);

                    ss := stream.DataString;

                  finally

                    FreeAndNil(stream);

                  end;

                end

                else

                begin

                  ss := astream.DataString;

                end;

              end

              else

              begin

                ss := ustream.DataString;

              end;

              Memo1.Lines.Text := ss;

            finally

              FreeAndNil(astream);

            end;

          finally

            FreeAndNil(ustream);

          end;

        end;

      finally

        FreeAndNil(idHttpObj);

      end;

  finally

    FreeAndNil(mstream);

  end;

  

end;

相关阅读 >>

Delphi 如何在toolbar中的toolbutton上显示文字

Delphi access技巧集

Delphi tgpimage 把图像转存为其他格式

Delphi rest客户端程序

Delphi string与tstringlist

Delphi 检测文件是否被占用

Delphi 鼠标拖动控件自由移动位置

Delphi 禁止用键盘左右箭头,去切换pagecontrol页签

Delphi 判断文件是否是图像文件

Delphi 批量生成 a到z 字母

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...