环境是D7,用idhttp登陆19楼,报http/1.1 405错误,发的数据包貌似没有错误,想不出原因,请教大家,谢谢
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ComCtrls, ToolWin, IdCookieManager, IdBaseComponent,
IdComponent, IdTCPConnection, IdTCPClient, IdHTTP, ExtCtrls;type
TForm1 = class(TForm)
IdHTTP1: TIdHTTP;
IdCookieManager1: TIdCookieManager;
ToolBar1: TToolBar;
LoginBtn: TButton;
ToolButton1: TToolButton;
Memo1: TMemo;
Button1: TButton;
ToolButton2: TToolButton;
Timer1: TTimer;
procedure LoginBtnClick(Sender: TObject);
procedure IdHTTP1WorkEnd(Sender: TObject; AWorkMode: TWorkMode);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.LoginBtnClick(Sender: TObject);
var
Params:TStrings;
res:TStringStream;
Url,Cook_Str:String;
begin
IdHTTP1.ReadTimeout:= 25000;
IdHTTP1.Request.Accept:= 'image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*';
IdHTTP1.Request.AcceptEncoding:= 'gzip, deflate';
IdHTTP1.Request.AcceptLanguage:= 'zh-cn';
IdHTTP1.Request.CacheControl:= 'no-cache';
IdHTTP1.Request.Connection:= 'Keep-Alive';
IdHTTP1.Request.ContentLength:=144;
IdHTTP1.Request.ContentType:= 'application/x-www-form-urlencoded'; Cook_Str:='__utma=92551362.1470865355.1315979220.1315979220.1315979220.1; __utmz=92551362.1315979220.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none);';
Cook_Str:=Cook_Str+' Hm_lvt_5185a335802fb72073721d2bb161cd94=1315979210890; __utmb=92551362.1.10.1315979220; __utmc=92551362; Hm_lpvt_5185a335802fb72073721d2bb161cd94=1315979220531; cdb_sid=7280dda0956157fb93a80c5b0f690556; BIGipServerforum_web_pool=100729098.20480.0000';
IdHTTP1.Request.CustomHeaders.Add(Cook_Str);
IdHTTP1.Request.Host:= 'passport.19lou.com';
IdHTTP1.Request.Referer:= 'http://hangzhou.19lou.com/';
IdHTTP1.Request.UserAgent:= 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0)'; Params :=TStringList.Create;
try
Params.Append('autoLogin=1');
Params.Append('eb0478242e3f3dbdbbf7df7566f404c1=d5520ab6f8095f0f72d4847d4b95c633');
Params.Append('login=?');
Params.Append('userName=********');
Params.Append('userPass=********'); try
Url := 'http://passport.19lou.com/login/index/t/normal';
IdHTTP1.HandleRedirects:=True; res := TStringStream.Create(''); IdHTTP1.Post(Url,Params,res);
UTF8Decode(TStringStream(IdHTTP1.Response.ContentStream).dataString); Memo1.Lines.Add(res.DataString);
except end; finally
Params.Free;
end;
end;procedure TForm1.IdHTTP1WorkEnd(Sender: TObject; AWorkMode: TWorkMode);
begin
Memo1.Lines.Add('****************************(DEBUG开始)***************************');
Memo1.Lines.Add('=======(访问页面)=========');
Memo1.Lines.Add(IdHTTP1.URL.URI);
Memo1.Lines.Add('=======(页面信息)=========');
Memo1.Lines.Add(IdHttp1.ResponseText);
Memo1.Lines.Add('=======(页面反馈)=========');
Memo1.Lines.Add('-------[发送过去的信息]---------------');
Memo1.Lines.AddStrings(IdHTTP1.Request.RawHeaders);
Memo1.Lines.Add('-------[发送过去的自定义信息]---------------');
Memo1.Lines.AddStrings(IdHTTP1.Request.CustomHeaders);
Memo1.Lines.Add('-------[返回回来的信息]---------------');
Memo1.Lines.AddStrings(IdHTTP1.Response.RawHeaders);
Memo1.Lines.Add('-------[返回回来的自定义信息]---------------');
Memo1.Lines.AddStrings(IdHTTP1.Response.CustomHeaders);
Memo1.Lines.Add('=======(页面内容)=========');
Memo1.Lines.Add(UTF8Decode(TStringStream(IdHTTP1.Response.ContentStream).dataString));
Memo1.Lines.Add('***************************(DEBUG结束)***************************');
end;end.
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ComCtrls, ToolWin, IdCookieManager, IdBaseComponent,
IdComponent, IdTCPConnection, IdTCPClient, IdHTTP, ExtCtrls;type
TForm1 = class(TForm)
IdHTTP1: TIdHTTP;
IdCookieManager1: TIdCookieManager;
ToolBar1: TToolBar;
LoginBtn: TButton;
ToolButton1: TToolButton;
Memo1: TMemo;
Button1: TButton;
ToolButton2: TToolButton;
Timer1: TTimer;
procedure LoginBtnClick(Sender: TObject);
procedure IdHTTP1WorkEnd(Sender: TObject; AWorkMode: TWorkMode);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.LoginBtnClick(Sender: TObject);
var
Params:TStrings;
res:TStringStream;
Url,Cook_Str:String;
begin
IdHTTP1.ReadTimeout:= 25000;
IdHTTP1.Request.Accept:= 'image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*';
IdHTTP1.Request.AcceptEncoding:= 'gzip, deflate';
IdHTTP1.Request.AcceptLanguage:= 'zh-cn';
IdHTTP1.Request.CacheControl:= 'no-cache';
IdHTTP1.Request.Connection:= 'Keep-Alive';
IdHTTP1.Request.ContentLength:=144;
IdHTTP1.Request.ContentType:= 'application/x-www-form-urlencoded'; Cook_Str:='__utma=92551362.1470865355.1315979220.1315979220.1315979220.1; __utmz=92551362.1315979220.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none);';
Cook_Str:=Cook_Str+' Hm_lvt_5185a335802fb72073721d2bb161cd94=1315979210890; __utmb=92551362.1.10.1315979220; __utmc=92551362; Hm_lpvt_5185a335802fb72073721d2bb161cd94=1315979220531; cdb_sid=7280dda0956157fb93a80c5b0f690556; BIGipServerforum_web_pool=100729098.20480.0000';
IdHTTP1.Request.CustomHeaders.Add(Cook_Str);
IdHTTP1.Request.Host:= 'passport.19lou.com';
IdHTTP1.Request.Referer:= 'http://hangzhou.19lou.com/';
IdHTTP1.Request.UserAgent:= 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0)'; Params :=TStringList.Create;
try
Params.Append('autoLogin=1');
Params.Append('eb0478242e3f3dbdbbf7df7566f404c1=d5520ab6f8095f0f72d4847d4b95c633');
Params.Append('login=?');
Params.Append('userName=********');
Params.Append('userPass=********'); try
Url := 'http://passport.19lou.com/login/index/t/normal';
IdHTTP1.HandleRedirects:=True; res := TStringStream.Create(''); IdHTTP1.Post(Url,Params,res);
UTF8Decode(TStringStream(IdHTTP1.Response.ContentStream).dataString); Memo1.Lines.Add(res.DataString);
except end; finally
Params.Free;
end;
end;procedure TForm1.IdHTTP1WorkEnd(Sender: TObject; AWorkMode: TWorkMode);
begin
Memo1.Lines.Add('****************************(DEBUG开始)***************************');
Memo1.Lines.Add('=======(访问页面)=========');
Memo1.Lines.Add(IdHTTP1.URL.URI);
Memo1.Lines.Add('=======(页面信息)=========');
Memo1.Lines.Add(IdHttp1.ResponseText);
Memo1.Lines.Add('=======(页面反馈)=========');
Memo1.Lines.Add('-------[发送过去的信息]---------------');
Memo1.Lines.AddStrings(IdHTTP1.Request.RawHeaders);
Memo1.Lines.Add('-------[发送过去的自定义信息]---------------');
Memo1.Lines.AddStrings(IdHTTP1.Request.CustomHeaders);
Memo1.Lines.Add('-------[返回回来的信息]---------------');
Memo1.Lines.AddStrings(IdHTTP1.Response.RawHeaders);
Memo1.Lines.Add('-------[返回回来的自定义信息]---------------');
Memo1.Lines.AddStrings(IdHTTP1.Response.CustomHeaders);
Memo1.Lines.Add('=======(页面内容)=========');
Memo1.Lines.Add(UTF8Decode(TStringStream(IdHTTP1.Response.ContentStream).dataString));
Memo1.Lines.Add('***************************(DEBUG结束)***************************');
end;end.
我使用XE测试了一下,在POST异常处又POST了一次,
显示的是<dd class="iblock"><strong>你已经处于登录状态 系统自动给予跳转</strong><br><br>
可想而知,刚才POST登陆已经成功了!但是怎么去掉405这个错误我就不知道了.待高人解决吧!
以下是我的代码:procedure TForm1.Button1Click(Sender: TObject);
const
LoginUrl='http://passport.19lou.com/login/index/t/normal'; //这个是输入账号密码的页面
var
ParStr,ResStream:TStringStream;
begin
ParStr :=TStringStream.Create;
ResStream :=TStringStream.Create('',TEncoding.Default);//网页内容编码 IdHTTP1.HandleRedirects :=True;
with IdHTTP1.Request do
begin
Referer :='http://passport.19lou.com/login/index/t/normal';
ContentType :='application/x-www-form-urlencoded';
UserAgent :='Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727)';
end;
ParStr.WriteString('userName=********&autoLogin=1'); //账号隐藏了
ParStr.WriteString('&userPass=********'); //密码也隐藏
ParStr.WriteString('&22906fbbd9903dab9e77f627c808b724=aa5334c99185df63172bd0f7489c1757'); //这两串字符在输入账号密码的页面源码中可看到!
ParStr.WriteString('&login=%C1%A2%BC%B4%B5%C7%C2%BC');
try
IdHTTP1.ProtocolVersion :=pv1_1;
IdHTTP1.HTTPOptions:=[hoKeepOrigProtocol];
IdHTTP1.Post(LoginUrl,ParStr);
except
IdHTTP1.Post(LoginUrl,ParStr,ResStream);
end;
ShowMessage(ResStream.DataString);
FreeAndNil(ParStr);
FreeAndNil(ResStream);
end;
你在IDHTTP控件的OnRedirect事件中写代码就可以了
将重定向的地址改为19楼的主页,这样就显示成功登陆主页的源码了.
IdHTTP1.HTTPOptions:=[hoKeepOrigProtocol];
加了你这两句,就可以去掉405的错误了,但是现在收到的没显示啊,用了Utf8ToAnsi();也不行,OnRedirect事件没有触发。
还有你的OnRedirect这个事件要加上登陆时的COOKIE的,不加的话,还是未登陆状态,请问你加了吗?
遇到问题自己思考一下噢
Response:string;
begin
Response :=idhttp.get('http://passport.19lou.com/login/index/t/normal');
ShowMessage(Response);
end;
看看Response是否有你所要找的字符串????????????????????????????????????????
document.write('<script type="text/javascript" src="http://js.19louimg.cn/passport/login.js"></script>');
document.write('<div class="nav_uinfo" id="navuinfo"><form id="loginForm" name="loginForm" method="post" action="http://passport.19lou.com/login/index/t/normal" target="_self" onsubmit="return checkdata();"><ul class="loginform"><li><label class="iblock inp_text"><span class="iblock">用户名:</span><input type="text" id="userName" name="userName" /></label><label class="iblock inp_checkbox"><input type="checkbox" name="autoLogin" value="1" checked /></label><a href="http://passport.19lou.com/register/index" target="_blank">申请入住</a></li><li><label class="iblock inp_text"><span class="iblock">密 码:</span><input type="password" name="userPass" id="userPass"/></label><label class="iblock inp_sm"><input type="hidden" id="ca681972f12ff91b56e8e8b5f96345a7" name="ca681972f12ff91b56e8e8b5f96345a7" value="6e74d7c0ff1150bf0c3fc7d8693367bd"/><input type="submit" value="立即登录" name="login" id=login"/></label><a href="http://passport.19lou.com/lostpass/index" target="_blank">找回密码</a></li></ul></form></div>');
function alertErrorLocalTime_getCookie(name) {
var cookie_start = document.cookie.indexOf(name);
var cookie_end = document.cookie.indexOf(";", cookie_start);
return cookie_start == -1 ? '' : unescape(document.cookie.substring(cookie_start + name.length + 1, (cookie_end > cookie_start ? cookie_end : document.cookie.length)));
}
function alertErrorLocalTime_setCookie(cookieName, cookieValue, seconds, path, domain, secure) {
var expires = new Date();
expires.setTime(expires.getTime() + seconds);
document.cookie = escape(cookieName) + '=' + escape(cookieValue)
+ (expires ? '; expires=' + expires.toGMTString() : '')
+ (path ? '; path=' + path : '/')
+ (domain ? '; domain=' + domain : '')
+ (secure ? '; secure' : '');
}var mydate = new Date();
var mytimestamp = parseInt(mydate.valueOf() / 1000);
if(Math.abs(mytimestamp - 1317128019) > 86400 && alertErrorLocalTime_getCookie('alertErrorLocalTime')=="") {
alertErrorLocalTime_setCookie("alertErrorLocalTime", mytimestamp, 86400, "/", ".19lou.com", false);
window.alert('注意:\n\n你本地计算机的时间设定与论坛时间相差超过 24 个小时,\n这可能会影响你的正常登录,请调整本地计算机设置。\n\n当前论坛时间是: (GMT +8) 2011-09-27 20:53:39\n如果你认为论坛时间不准确,请与论坛管理员联系。');
}红色部分不就是你要取的内容嘛?
39c6bca9fd3c5ee504a2fa0d8c7114bc 4139aec5c2500e28cb74f187a2b29615
用GET获取的网页里包含的值是
id="39c6bca9fd3c5ee504a2fa0d8c7114bc" name="39c6bca9fd3c5ee504a2fa0d8c7114bc" value="b009f1f75d51ded6ca143d472222664d"ID这一部分是正确的,但是后半部分是不对的。
这是GET中的代码
id="27bbbf00ef356f9e2351357dd3ef4f14" name="27bbbf00ef356f9e2351357dd3ef4f14" value="78493eb41f0cb5ca35e1bd086c0a0d34
这是用Httpanalyzerstd截获的包POST /login/index/t/normal HTTP/1.1
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*
Referer: http://passport.19lou.com/login/index/t/normal
Accept-Language: zh-cn
Content-Type: application/x-www-form-urlencoded
UA-CPU: x86
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727)
Host: passport.19lou.com
Content-Length: 141
Connection: Keep-Alive
Cache-Control: no-cache
Cookie: cdb_sid=d715056b6adc992137ce7b29a0e39fd6; BIGipServerforum_web_pool=83951882.20480.0000userName=love&autoLogin=1&userPass=123456&27bbbf00ef356f9e2351357dd3ef4f14=78493eb41f0cb5ca35e1bd086c0a0d34&login=%C1%A2%BC%B4%B5%C7%C2%BC