关于delphi中url文件下载编码问题(处理里面含有空格,汉字,字符)

来源:互联网 发布:国产电视剧知乎 编辑:程序博客网 时间:2024/05/21 11:19

不管是用indy控件,还是ics控件,用http下载文件的时候都会遇到,url含有汉字,空格,其他字符之类的符号,直接用utl是不行的,除非是英文状体下的字符。

需要把url编码,才能正确的下载。

个人经验与大家分享下,ulr编码

我这里 delphiXE下的url编码(unicode),delphi7可根据函数自行修改;注意string类型是widestring


引用的单元HTTPApp

function  URLEncode(const S:String;
  const InQueryString: Boolean): string;
var
  Idx: Integer;
begin
  Result := '';
  for Idx := 1 to Length(S) do
  begin
      case S[Idx] of
        'A'..'Z', 'a'..'z', '0'..'9', '-', '_', '.', '=', '&', '%':
          Result := Result + S[Idx];
        ' ':
          if InQueryString then
            Result := Result + '+'
          else
            Result := Result + '%20';
        '/':
           Result :=Result+s[Idx];
        ':':
           Result :=Result+s[Idx];
      else
          Result := Result +   HTTPEncode(UTF8Encode(s[Idx]));

      end;
  end;
end;

原创粉丝点击