idhttp Post信息乱码的问题(100)

  • 主题发起人 主题发起人 himoo
  • 开始时间 开始时间
H

himoo

Unregistered / Unconfirmed
GUEST, unregistred user!
想post 一个这样的内容ajax=1&offset=10&p=[{"t":"base","gend":"男生","prov":"北京","city":"西城区"},{"t":"birt","year":"1975","month":"1","day":"1"}]&s=0不过如果把idhttp的hoForceEncodeParams 设置成False的话,里面的中文全变成???了如果设成Ture,则里面的 [ 和 ] 变成了 %5B 和 %5D ,如何同时达到呢?
 
uses HTTPAppHTTPEncode看下这个
 
试试这个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; 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;
 
接受答案了.
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
D
回复
0
查看
2K
DelphiTeacher的专栏
D
D
回复
0
查看
2K
DelphiTeacher的专栏
D
D
回复
0
查看
1K
DelphiTeacher的专栏
D
后退
顶部