procedure TForm1.Button1Click(Sender: TObject);
var
params:tstrings;
begin
idHTTP1.Request.UserAgent:='Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)';
idHTTP1.Request.ContentType:='application/x-www-form-urlencoded; Charset=UTF-8';
idHTTP1.Request.Host:='my.qq.com';
idHTTP1.Request.Connection:='Keep-Alive';
idHTTP1.Request.Accept:='*/*';
idHTTP1.Request.SetHeaders;
idHTTP1.Request.CustomHeaders.Values['Cookie']:= edit4.text;
Params:=TStringList.Create;
params.Add('fp='+'loginerroralert');
params.Add('u='+edit1.Text);
params.Add('p='+edit2.Text);
params.Add('verifycode='+edit3.Text);
params.Add('u1='+'http://my.qq.com');
params.Add('aid='+'8000203');
params.Add('dummy='+'1');
params.Add('h='+'1');
memo1.Text:=idhttp1.Post('http://ptlogin2.qq.com/login',params);
memo1.Text:=utf8toansi(memo1.Text);
end;
idHTTP1实现自动登录
中文乱码问题
Utf8ToAnsi(idhttp.get(url))
:测试过,正确
如果直接用 sText:=MyIdHTTP.Get(url);经常会出现乱码;
如果借用TStringStream;就可以解决这个问题;
比如:
var
url:string;
MyIdHTTP: TIdHTTP;
ss : TStringStream;
begin
ss := TStringStream.Create('', TEncoding.GetEncoding(936));
MyIdHTTP := TIdHTTP.Create(self);
MyIdHTTP.HandleRedirects := True;
MyIdHTTP.Request.ContentType := 'application/x-www-form-urlencoded';
url:='http://www.daizhicun.com';
MyIdHTTP.Get(url,ss);
stext:=ss.DataString;
FreeAndNil(ss);
end;
idhttp get 中文出现乱码的问题
默认是按系统指定locale(CP_ACP)输出的,简体中文系统默认的是gb18030(虽然名义上是gb2312的代码页)。但是indy只对utf-8的编码进行了处理,所以其他编码只能自己想办法处理了,比如这样:
-
Delphi(Pascal) code
-
var ss : TStringStream; begin ss := TStringStream.Create('', TEncoding.GetEncoding(936)); { 指定gb2312的中文代码页,或者54936(gb18030)更好些 utf8 对应 65001} try IdHTTP1.Get(CSURL, ss); IdHTTP1.Get(CSURL); Memo1.Text := ss.DataString; finally ss.Free; end; end;