Delphi HTTP POST 方法 - Cookie 问题
我正在尝试登录一个网站,但该网站总是回复
“您似乎不接受 cookie。需要 cookie 才能登录。”
为什么我的程序不允许 cookie?
function HF_Login(): boolean;
var
HTTP : TIDHTTP;
Cookie : TidCookieManager;
Data, Page : TStringList;
begin
HTTP := TIDHTTP.Create(NIL);
Cookie := TidCookieManager.Create(NIL);
HTTP.Request.UserAgent := 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; SLCC1';
HTTP.Request.Accept := 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8';
HTTP.Request.AcceptLanguage := 'en-us;q=0.7,en;q=0.3';
HTTP.Request.AcceptCharSet := 'windows-1251,utf-8;q=0.7,*;q=0.7';
HTTP.Request.Pragma := 'no-cache';
HTTP.Request.CacheControl := 'no-cache';
HTTP.Request.RawHeaders.Add('X-Requested-With: XMLHttpRequest');
HTTP.AllowCookies := True;
HTTP.HandleRedirects := True;
HTTP.ProtocolVersion := pv1_1;
HTTP.Request.Connection := 'Keep-Alive';
HTTP.CookieManager := Cookie;
Data := TStringList.Create;
Page := TStringList.Create;
// returnto=%2F&user=[user]&pass=[pass]
Data.Add('returnto=%2F&user=csz0021&pass=Yese0071');
page.Text := HTTP.Post('http://hotfile.com/login.php', Data);
//form1.RichEdit1.Text := HTTP.Post('http://hotfile.com/login.php', Data);
If Pos('Bad username/password combination.', Page.Text) <> 0 Then Result := False
else Result := True;
Page.Free;
Data.Free;
end;
这是一个用于测试网站的免费帐户。
csz0021
是的0071
谢谢
i'm trying to logging into a website but this site always response with
"You don't seem to accept cookies. Cookies are required in order to log in."
why my program doesn't allow cookies?
function HF_Login(): boolean;
var
HTTP : TIDHTTP;
Cookie : TidCookieManager;
Data, Page : TStringList;
begin
HTTP := TIDHTTP.Create(NIL);
Cookie := TidCookieManager.Create(NIL);
HTTP.Request.UserAgent := 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; SLCC1';
HTTP.Request.Accept := 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8';
HTTP.Request.AcceptLanguage := 'en-us;q=0.7,en;q=0.3';
HTTP.Request.AcceptCharSet := 'windows-1251,utf-8;q=0.7,*;q=0.7';
HTTP.Request.Pragma := 'no-cache';
HTTP.Request.CacheControl := 'no-cache';
HTTP.Request.RawHeaders.Add('X-Requested-With: XMLHttpRequest');
HTTP.AllowCookies := True;
HTTP.HandleRedirects := True;
HTTP.ProtocolVersion := pv1_1;
HTTP.Request.Connection := 'Keep-Alive';
HTTP.CookieManager := Cookie;
Data := TStringList.Create;
Page := TStringList.Create;
// returnto=%2F&user=[user]&pass=[pass]
Data.Add('returnto=%2F&user=csz0021&pass=Yese0071');
page.Text := HTTP.Post('http://hotfile.com/login.php', Data);
//form1.RichEdit1.Text := HTTP.Post('http://hotfile.com/login.php', Data);
If Pos('Bad username/password combination.', Page.Text) <> 0 Then Result := False
else Result := True;
Page.Free;
Data.Free;
end;
this is a free account to test the website.
csz0021
Yese0071
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
它不能像你这样做时一步完成,需要分两步完成。首先请求登录页面 (GET),然后发送您的登录凭据 (POST)。确保对这两个操作使用相同的 CookieManager。
编辑以帮助匿名投票者理解为什么这个答案实际上是显而易见的。 OP 收到此错误消息:
所以服务器期望看到来自客户端的一些cookie。由于 OP 仅执行一个 POST 请求,因此它的 CookieManager 不包含 cookie,因此它不会发送任何 cookie。尽管如此,服务器仍希望看到一些 cookie。在正常的基于浏览器的会话中,这些 cookie 来自哪里?嗯,如果不先实际导航到表单(GET 请求),填写登录表单是非常非常困难的。
It doesn't work in one step as you're doing it, need to do it in two steps. First request the login page (GET), then send your login credentials (POST). Make sure you use the same CookieManager for both operations.
Edit to help the anonymous downvoter understand why this answer is actually obvious. The OP gets this error message:
so the server is expecting to see some cookies from the client. Since the OP is only doing ONE POST request, it's CookieManager contains no cookies, so it's not sending any cookies. None the less the server expects to see some cookies. Where would those cookies come from in a normal browser-based session? Well, filling in the login-form is very, very hard without actually navigating to the form first (the GET request).
我建议您安装一个HTTP请求监控工具,将您的请求与标准浏览器的请求进行比较。
HTTP 分析器是一个好的开始。
I would suggest you install a HTTP request monitoring tool, compare your request with a standard browser ones.
HTTP Analyzer is a good start.
确保您使用的是 Indy 10 的最新版本。Indy 10 的 cookie 支持已经被破坏了很长一段时间,但几个月前终于得到了修复。即使您启用了 Cookie,Indy 也可能拒绝接收到的 Cookie,或者不将 Cookie 发送回服务器。
附带说明:使用
Request.CustomHeaders
属性而不是Request.RawHeaders
属性,并且不要对传递给 Post() 的 TStringList 数据进行 URL 编码。 Post 将为您在内部对数据进行编码(如果在TIdHTTP.HTTPOptions
属性中启用了hoForceEncodeParams
标志(默认情况下))。Make sure you are using an up-to-date version of Indy 10. Indy 10's cookie support was broken for a long time, but was finally fixed a few months ago. Even though you may have Cookies enabled, it is possible that Indy was either rejecting received cookies, or not sending cookies back to the server.
On a side note: use the
Request.CustomHeaders
property instead of theRequest.RawHeaders
property, and do not URL-encode the TStringList data you pass to Post(). Post will encode the data internally for you (if thehoForceEncodeParams
flag is enabled in theTIdHTTP.HTTPOptions
property, which it is by default).