但是,我的 Delphi 应用程序在停止之前只生成了几个请求.以下是我遵循的步骤:在表单上放置一个 IdHTTP 和一个 IdSSLIOHandlerSocketOpenSSL1 组件.将 IdHTTP 组件属性 AllowCookies 和 HandleRedirects 设置为 True,将 IOHandler 属性设置为 IdSSLIOHandlerSocketOpenSSL1.设置 IdSSLIOHandlerSocketOpenSSL1 组件属性 Method := 'sslvSSLv23'我终于有了这个代码:procedure TfmMain.GetUrlToFile(AURL, AFile : String);无功输出:TMemoryStream;开始输出:= TMemoryStream.Create;尝试IdHTTP1.Get(FURL, 输出);Output.SaveToFile(AFile);最后输出.免费;结尾;结尾;但是,它没有按预期进入登录页面.我希望它的行为就像是一个网络浏览器,并继续进行重定向,直到找到最终页面.这是 Fiddler 头文件的输出:HTTP/1.1 302 发现位置:https://encrypted.google.com/缓存控制:私有内容类型:文本/html;字符集=UTF-8设置-Cookie:PREF=ID=5166063f01b64b03:FF=0:TM=1293571783:LM=1293571783:S=a5OtsOqxu_GiV3d6;到期=周四,2012 年 12 月 27 日 21:29:43 GMT;路径=/;域=.google.com设置-Cookie:NID=42=XFUwZdkyF0TJKmoJjqoGgYNtGyOz-Irvz7ivao2z0--pCBKPpAvCGUeaa5GXLneP41wlpse-yU5UuC57pBfMkv434t7XB1H68ETPNAZVnjQRnVD到期=星期三,2011 年 6 月 29 日 21:29:43 GMT;路径=/;域=.google.com;仅Http日期:2010 年 12 月 28 日,星期二 21:29:43 GMT服务器:gws内容长度:226X-XSS-保护:1;模式=块首先,这个输出有什么问题吗?我还需要做些什么来让 IdHTTP 组件继续进行重定向直到最后一页? 解决方案 调用前的IdHTTP组件属性值: 名称 := 'IdHTTP1';IOHandler := IdSSLIOHandlerSocketOpenSSL1;AllowCookies := True;HandleRedirects := True;重定向最大值:= 35;请求.用户代理:='Mozilla/5.0 (Windows NT 5.1; rv:2.0b8) Gecko/20100101 Firefox/4.'+'0b8';HTTPOptions := [hoForceEncodeParams];OnRedirect := IdHTTP1Redirect;CookieManager := IdCookieManager1;重定向事件处理程序:procedure TfmMain.IdHTTP1Redirect(Sender: TObject; var dest: string; varNumRedirect:整数;处理的变量:布尔值;var VMethod: 字符串);开始处理 := True;结尾;拨打电话: FURL := 'https://www.google.com';GetUrlToFile((FURL + '/adsense/'), 'a.html');过程 TfmMain.GetUrlToFile(AURL, AFile : String);无功输出:TMemoryStream;开始输出:= TMemoryStream.Create;尝试尝试IdHTTP1.Get(AURL, 输出);IdHTTP1.断开连接;除了结尾;Output.SaveToFile(AFile);最后输出.免费;结尾;结尾;这是 Fiddler 的(请求和响应标头)输出:Using: Delphi 2010, latest version of IndyI am trying to scrape the data off Googles Adsense web page, with an aim to get the reports. However I have been unsuccessful so far. It stops after the first request and does not proceed.Using Fiddler to debug the traffic/requests to Google Adsense website, and a web browser to load the Adsense page, I can see that the request (from the webbrowser) generates a number of redirects until the page is loaded.However, my Delphi application is only generating a couple of requests before it stops.Here are the steps I have followed:Drop a IdHTTP and a IdSSLIOHandlerSocketOpenSSL1 component on the form.Set the IdHTTP component properties AllowCookies and HandleRedirects to True, and IOHandler property to the IdSSLIOHandlerSocketOpenSSL1.Set the IdSSLIOHandlerSocketOpenSSL1 component property Method := 'sslvSSLv23'Finally I have this code:procedure TfmMain.GetUrlToFile(AURL, AFile : String);var Output : TMemoryStream;begin Output := TMemoryStream.Create; try IdHTTP1.Get(FURL, Output); Output.SaveToFile(AFile); finally Output.Free; end;end;However, it does not get to the login page as expected. I would expect it to behave as if it was a webbrowser and proceed through the redirects until it finds the final page.This is the output of the headers from Fiddler:HTTP/1.1 302 FoundLocation: https://encrypted.google.com/Cache-Control: privateContent-Type: text/html; charset=UTF-8Set-Cookie: PREF=ID=5166063f01b64b03:FF=0:TM=1293571783:LM=1293571783:S=a5OtsOqxu_GiV3d6; expires=Thu, 27-Dec-2012 21:29:43 GMT; path=/; domain=.google.comSet-Cookie: NID=42=XFUwZdkyF0TJKmoJjqoGgYNtGyOz-Irvz7ivao2z0--pCBKPpAvCGUeaa5GXLneP41wlpse-yU5UuC57pBfMkv434t7XB1H68ET0ZgVDNEPNmIVEQRVj7AA1Lnvv2Aez; expires=Wed, 29-Jun-2011 21:29:43 GMT; path=/; domain=.google.com; HttpOnlyDate: Tue, 28 Dec 2010 21:29:43 GMTServer: gwsContent-Length: 226X-XSS-Protection: 1; mode=blockFirstly, is there anything wrong with this output?Is there something more that I should do to get the IdHTTP component to keep pursuing the redirects until the final page? 解决方案 IdHTTP component property values prior to making the call: Name := 'IdHTTP1'; IOHandler := IdSSLIOHandlerSocketOpenSSL1; AllowCookies := True; HandleRedirects := True; RedirectMaximum := 35; Request.UserAgent := 'Mozilla/5.0 (Windows NT 5.1; rv:2.0b8) Gecko/20100101 Firefox/4.' + '0b8'; HTTPOptions := [hoForceEncodeParams]; OnRedirect := IdHTTP1Redirect; CookieManager := IdCookieManager1;Redirect event handler:procedure TfmMain.IdHTTP1Redirect(Sender: TObject; var dest: string; var NumRedirect: Integer; var Handled: Boolean; var VMethod: string);begin Handled := True;end;Making the call: FURL := 'https://www.google.com'; GetUrlToFile( (FURL + '/adsense/'), 'a.html'); procedure TfmMain.GetUrlToFile(AURL, AFile : String); var Output : TMemoryStream; begin Output := TMemoryStream.Create; try try IdHTTP1.Get(AURL, Output); IdHTTP1.Disconnect; except end; Output.SaveToFile(AFile); finally Output.Free; end; end;Here's the (request and response headers) output from Fiddler: 这篇关于Indy - IdHttp 如何处理页面重定向?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-24 14:40
查看更多