我正在尝试使用 Delphi XE5 通过 Indy 组件登录 Gmail(不是电子邮件), 使用此功能:
procedure TForm1.Button1Click(Sender: TObject);
var
http : TIdHTTP;
S, GALX, Email, Pass : String;
lParam : TStringList;
begin
try
lParam := TStringList.Create;
try
http := TIdHTTP.Create(nil);
http.IOHandler := IOHandler;
http.CookieManager := Cookie;
http.AllowCookies := true;
http.HandleRedirects := true;
http.Request.UserAgent := 'Mozilla/5.0 (Windows NT 6.2; WOW64; rv:27.0) Gecko/20100101 Firefox/27.0';
http.Request.Host := 'accounts.google.com';
http.Request.Accept := 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8';
http.Request.ContentType := 'application/x-www-form-urlencoded';
S := http.Get('https://accounts.google.com/ServiceLogin');
Delete(S, 1, Pos('GALX', S));
S := Copy(S, 1, Pos('">', S) - 1);
Delete(S, 1, Pos('value=', S) + length('value='));
GALX := S;
lParam.Add('GALX='+GALX);
lParam.Add('Email='+Email);
lParam.Add('Passwd='+Pass);
Memo1.Lines.Add(http.Post('http://accounts.google.com/ServiceLoginAuth', lParam));
finally
http.Free;
end;
finally
lParam.Free;
end;
end;
现在,每当我尝试执行时,我都会得到:HTTP/1.0.405 方法不允许。
只有当电子邮件/通行证正确时我才会收到此错误,当电子邮件/通行证错误时我会收到通常的错误页面,所以我猜测这不是不允许的 POST
方法。
我在这里做错了什么?
最佳答案
您没有提交 /ServiceLoginAuth
查找的所有输入字段。如果您查看 /ServiceLogin
的 HTML,除了您已经发送的 3 个字段之外,还有 8 个其他字段发布到 /ServiceLoginAuth
。当从 HTML 表单提交数据时,您必须提交 HTML 表单想要提交的所有内容,而不能只是挑选您想要的内容。尝试添加其他字段,看看会发生什么。
在发布到 /ServiceLoginAuth
时,您需要在 TIdHTTP.Request.Referer
属性中提供 /ServiceLogin
URL,因此它认为请求来自 /ServiceLogin
。
您正在使用 HTTPS 检索 /ServiceLogin
,但您正在使用 HTTP 发布到 /ServiceLoginAuth
。您需要使用 HTTPS。
当用户拥有多个 Google 帐户时,/ServiceLogin
会发布到 /AccountChooser
,然后使用其他输入参数重定向回 /ServiceLogin
,所以您可能也需要考虑到这一点。
发布到 /ServiceLoginAuth
会重定向到 /CheckCookie
,然后重定向到 /ManageAccount
,因此请确保这些请求完整且准确每一步。
关于delphi - 使用 Indy 连接到 Gmail,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22393550/