经过两天的努力,终于有一个成品出现了。IdHttp.Post 登陆网站。。。代码写得马虎。但是,代码都是正确的写法。。。可以参考。。
procedure TForm1.btn1Click(Sender: TObject);
var
idhttp:TIdHTTP;
datastr:TStringList;
Html:string;
ddd:TStringStream; //经历了无数次错误失败以后,急了,随手声明了个变量,尝试这样能不能成功。结果有了这篇文章。
begin
ddd := TStringStream.Create('membername=帐号&memberpass=密码&act=1'); //******1
idhttp := tidhttp.Create(nil);
IdHTTP.HTTPOptions:=[hoKeepOrigProtocol]; //如果需要http/1.1的话,这两个不能少。
idhttp.ProtocolVersion := pv1_1;// 如果需要http/1.1的话,这两个不能少。这个http/1.1应该是协议版本吧?-_-#
idhttp.HandleRedirects := True; //如果登陆后,会跳到其它页面,就要设置为True;
idhttp.Request.ContentType := 'application/x-www-form-urlencoded'; //这个好像是编码方式吧。程序中不能少。
idhttp.Request.Accept := 'application/x-shockwave-flash, image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/msword, application/vnd.ms-excel,'+
'application/vnd.ms-powerpoint, application/QVOD, application/QVOD, application/x-ms-application, application/x-ms-xbap, application/xaml+xml, */*';//不要问这些东西在哪里找的,不解释。下同。
idhttp.Request.Referer := 'http://bbs.2ccc.com/default.asp';
idhttp.Request.AcceptLanguage := 'zh-cn';
// idhttp.Request.ContentType := 'application/x-www-form-urlencoded';
idhttp.Request.AcceptEncoding := 'gzip, deflate';
idhttp.Request.UserAgent := 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 2.0.50727; .NET4.0E; .NET4.0C)';
idhttp.Request.Host := 'bbs.2ccc.com';
//idhttp.Request.ContentLength := 43;
idhttp.Request.Connection := 'Keep-Alive';
idhttp.Request.CacheControl := 'no-cache';
datastr := TStringList.Create;
datastr.Add('membername帐号'); ********2
datastr.Add('memberpass=密码');
datastr.Add('act=1');
Html := idhttp.Post('http://bbs.2ccc.com/login.asp',ddd);
mmo1.text := Html; //如果返回乱码的话,就需要这样写Utf8ToAnsi(Html)。但是,如果没有返回乱码也是这样写的话,mmo1显示空;//在这里也折腾了段时间。
// mmo1.Text := IdHttp.ResponseText;
idhttp.Free;
datastr.Free;
ddd.Free;
end;
两段标星号的需要注意。星号2处在Post的时候在最后会添加一个&符号,所以失败了,但是没有什么错误提示。最后,在网上找来各式各样的代码层层叠叠。最后选择了星号1处才成功。。。-_-# 我不知道别人是怎么学编程的。。。
'membername=帐号&memberpass=密码&act=1&'); //& 就错在这个符号
'membername=帐号&memberpass=密码&act=1'); //正确。