delphi7 idhttp post中文的问题
procedure TForm1.fasong();
var
  Params1:TStrings;
  url:string;
  tmp:string;  begin     IdHTTP1.AllowCookies:=false;
  Params1 :=TStringList.Create;
  try     IdHTTP1.Request.Accept:= '*/*';
     IdHTTP1.Request.ContentType:= 'application/x-www-form-urlencoded';
     IdHTTP1.Request.Host:= 'www.xxx.com';
     IdHTTP1.Request.AcceptEncoding:= 'gzip, deflate';
     IdHTTP1.Request.Referer:= 'http://www.xxx.com/qwe/index.php';
     IdHTTP1.HTTPOptions:=IdHTTP1.HTTPOptions+[hoKeepOrigProtocol];
     IdHTTP1.ProtocolVersion:=pv1_1;
    IdHTTP1.Request.CustomHeaders.Clear;
     IdHTTP1.Request.CustomHeaders.add(t);
  try
   Params1.Append('id=111');
    Params1.Append('name=登录');
    try
      url := 'http://www.xxx.com/qwe/iii.php';
      IdHTTP1.HandleRedirects:=true;
      tmp:=idhttp1.Post(url,Params1);
    except
    end;
    except
    end;
  finally    Params1.Free;
  end;
end;
抓包显示‘登录'是  %B5%C7%C2%BC  而不是UTF8,怎样才能POST出去的数据是UTF8编码的?
谢谢!

解决方案 »

  1.   

    please try this :( 這個只針對GB2312編碼,請自行確認自己的編碼是不是GB2312碼)type
      ISO8859String  = type AnsiString(1252);
      GB2312String   = type AnsiString(936);
      function URIParamsEncode(const ASrc: RawByteString): RawByteString;
      const
        UnsafeChars = ['*', '#', '%', '<', '>', '[', ']'];
        ASCIIChars  = [#$21..#$7f];
        AnsiHex : array[0..15]of AnsiChar = '0123456789ABCDEF';
      var
        i, len  : Integer;
        b : Byte;
        c : AnsiChar;
        sBuff : RawByteString;
        pSrc, pDst  : PAnsiChar;
      begin
        len := Length(ASrc);
        if(ASrc[len]='&')then Dec(len);
        SetLength(sBuff, len*3);
        pSrc  := Pointer(ASrc);
        pDst  := Pointer(sBuff);
        for i := 0 to Len - 1 do
        begin
          c := pSrc[i];
          if((c in UnsafeChars)or(not(c in ASCIIChars)))then
          begin
            b := Byte(c);
            pDst[0] := '%';
            pDst[1] := AnsiHex[b shr 4];
            pDst[2] := AnsiHex[b and $f];
            Inc(pDst, 3);
          end else
          begin
            pDst^ := c;
            Inc(pDst);
          end;
        end;
        pSrc  := Pointer(sBuff);
        SetString(Result, pSrc, pDst-pSrc);
      end;
    var
      lstPost : TStringList;
      stmTmp  : TMemoryStream;
      sI8859  : ISO8859String;
      sGb2312 : GB2312String;
    begin
      lstPost := TStringList.Create;
      stmTmp  := TMemoryStream.Create;
      try
        lstPost.Add('mobile='+Edit1.Text);
        lstPost.Add('action=mobile');
        lstPost.Add('test=测 试');
        IdHTTP1.Request.ContentType := 'application/x-www-form-urlencoded';    sGb2312 := URIParamsEncode( GB2312String(StringReplace(
            lstPost.Text, #13#10, '&', [rfReplaceAll] )) );
        stmTmp.Write(sGb2312[1], Length(sGb2312));
        stmTmp.Position := 0;    sI8859  := ISO8859String(IdHTTP1.Post(URLPost, stmTmp));
        SetString(sGb2312, PAnsiChar(sI8859), Length(sI8859));
        Memo1.Text  := string(sGB2312);
      finally
        stmTmp.Free;
        lstPost.Free;
      end;
      

  2.   

    s:=ansitoutf8('登陆');
    ChangeStr:=HTTPEncode(s);
    showmessage(ChangeStr);
      

  3.   

    谢谢kye_jufei
    只可惜那是针对delphi2009
    我是新手不知在delphi7中如何用
      

  4.   


    unit Unit1;
    interface
    uses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, ExtCtrls, IdBaseComponent, IdComponent, IdTCPConnection,
      IdTCPClient, IdHTTP, IdCookieManager,types, StdCtrls,httpapp;
    type
      TForm1 = class(TForm)
      IdHTTP1: TIdHTTP;
        ……
        ……  private
        { Private declarations }
      public
        { Public declarations }
       end;
    var
      Form1: TForm1;
      t:string;
    implementation
    {$R *.dfm}procedure TForm1.Toupiao();
        var
      Params1:TStrings;
     ChangeStr,yyy, url:string;
      tmp:string;
      begin  yyy:='测试';
      ChangeStr:=UTF8Encode(yyy);
         IdHTTP1.AllowCookies:=false;
      Params1 :=TStringList.Create;
      try
         IdHTTP1.Request.Accept:= '*/*';
         //IdHTTP1.Request.X-flash-version := 10,0,32,18
         IdHTTP1.Request.ContentType:= 'application/x-www-form-urlencoded';
         IdHTTP1.Request.Host:= www.xxx.com';
         IdHTTP1.Request.AcceptEncoding:= 'gzip, deflate';
         IdHTTP1.Request.Referer:= 'http://www.xxx.com/index.php';      
        IdHTTP1.Request.CustomHeaders.Clear;
         IdHTTP1.Request.CustomHeaders.add(t);
        
      try
       Params1.Append('id=602');
        Params1.Append('name='+ChangeStr);
        try
          url := 'http://www.xxx.com/index.php';
          IdHTTP1.HandleRedirects:=true;
          tmp:=idhttp1.Post(url,Params1);
          except
        end;
        except
        end;
      finally
        Params1.Free;
      end;
    end;
    总算找到了delphi7中解决方法
    留给后来者
      

  5.   

    关键句ChangeStr:=UTF8Encode(yyy);