我已经查看了SO建议的链接,但似乎没有任何相关链接,因此如下。。
(在下文中,实际url和标头数据已被混淆)
我真的很感激使用Delphi和(最好)Twebbrowser来自动从https网页下载数据,因为我在Indy方面没有太多成功。
在Firefox中,我导航到url:https://www.thedomain.com/folder1/Showdata.aspx?code=111显示了一些数据,在那里我可以点击一个图像,将数据下载为csv。
我正试图在Delphi中使用Twebbrowser,通过导航到url,然后单击该图像,或者通过程序调用与该图像相同的函数,来实现这一过程的自动化。。
网页显示带有锚文本的图像。。。
<a class="button dlCSV" href="javascript:void(0);" onclick="MyLib.DownloadCsv();return false;">Download</a>
看起来点击该按钮会导致POST到
https://www.thedomain.com/folder1/AnotherFolder/Sendcsv.ashx
(带有一堆标题),它实际上完成了下载。
我第一次尝试使用TWebrowser导航到
https://www.thedomain.com/folder1/Showdata.aspx?code=111
然后在Twebbrowser窗口中手动单击该按钮。它下载了一个名为Sendcsv.ashx.的空文件
然后,我尝试使用此处的代码以程序方式单击图像:http://www.experts-exchange.com/Programming/Languages/Pascal/Delphi/Q_27493399.html.这似乎没有任何作用。它找到了图像并调用了它的点击方法,但没有下载任何内容,也没有显示错误消息。
有人能帮助我使用Delphi下载这些数据所需的代码吗
可能有用的其他信息
使用Firefox中调试器中的"网络"选项卡,我发现单击图像会导致POSThttps://www.thedomain.com/folder1/AnotherFolder/Sendcsv.ashx
并且存在如下所示的头部负载。然而,我不知道如何编写将这些标头发送到Sendcsv.ashx的代码(如果这是我需要做的!)。
响应标头
- 缓存控制:"最大年龄=0"
- 内容处置:"附件;filename=thedata.csv"
- 内容编码:"gzip"
- 内容长度:"6050"
- 内容类型:"text.html;charset=utf-8"
- 日期:"2014年11月3日星期二10:05:43 GMT"
- Pragma:"公共"
- 服务器:"Microsoft IIS/8.0"
- X-AspNet-版本:"4.0.30319"
- X-Powered-Bor:"ASP.NET"
请求标头
- 主机:"www.thedomain.com"用户代理:"Mozilla/5.0(Windows NT 6.1
- WOW64;rv:33.0)壁虎/2010101 Firefox/33.0"
- 接受:"text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8"
- 接受语言:"en-GB,en;q=0.5"接受编码:"gzip,deflate"
- 参考人:"https://www.thedomain.com/folder1/Showdata.aspx?code=111"
- Cookie:"rememberme=True"连接:"保持活力"
- 内容类型:"application/x-www-form-urlencoded"
- 内容长度:"15972"
从上传流请求标头
- 内容类型:"application/x-www-form-urlencoded"
- 内容长度:"15972"
最近,对于这种情况,我一直在使用通过导入Microsoft XML
类型库获得的MSXML2_TLB.pas
单元。
它有一个类XMLHTTP
,它的作用与XmlHttpRequest
完全相同,就像您在web开发中可能认识到的那样。
uses SysUtils, ActiveX, AxCtrls, MSXML2_TLB;
//be sure to call CoInitialize(nil); on application or thread startup
//procedure ...
var
reqData:string;
req:XMLHTTP;
str:TOleStream;
f:TFileStream;
begin
//Did you catch the request data? load/set/build it here:
//reqData:=
req:=CoXMLHTTP.Create;
req.open('POST','https://www.thedomain.com/folder1/AnotherFolder/Sendcsv.ashx',false,'','');
req.setRequestHeader('Host','www.thedomain.com');
// try first without this one: it may not be required:
//req.setRequestHeader('Referer','https://www.thedomain.com/folder1/Showdata.aspx?code=111');
// same here, may not affect response
//req.setRequestHeader('Cookie','rememberme=True');
req.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
req.setRequestHeader('Content-Length',IntToStr(Length(reqData)));
req.send(reqData);
if req.status=200 then
begin
str:=TOleStream.Create(IUnknown(req.responseStream) as IStream);
try
f:=TFileStream.Create('thedata.csv',fmCreate);//or TMemoryStream?
try
f.CopyFrom(str,str.Size);
finally
f.Free;
end;
finally
str.Free;
end;
end
else
raise Exception.Create('CSV request failed: '+req.statusText);//parse req.responseText?
end;