使用Indy在Delphi 5中下载CSV [英] Download CSV in Delphi 5 with Indy
问题描述
我知道有很多Indy线程,但是我找不到适合我的情况的线程.
I know there's alot of Indy threads but I can't get one to match my case.
我已获得一个包含用户名和密码形式的URL.然后,该操作将作用于URL/reports.php上,该URL/reports.php上有多个超链接.
I have been given a URL with a username and password form. this then actions to a URL/reports.php on which there are multiple hyperlinks.
这些链接中的每个链接都将定向到带有URL变量的页面,例如Reports.php?report = variablename,可立即开始下载.
Each of these links will direct to a page with URL variables e.g. reports.php?report=variablename where a download will immediately start.
到目前为止我的想法:
procedure TForm1.PostData(Sender: TObject);
var
paramList:TStringList;
url,text:string;
// IdHTTP1: TIdHTTP;
IdSSLIOHandlerSocket1: TIdSSLIOHandlerSocket;
idLogFile1 : TidLogFile;
begin
idLogFile1 := TidLogFile.Create(nil);
with idLogFile1 do
begin
idLogFile1.Filename := 'C:\HTTPSlogfile.txt';
idLogFile1.active := True;
end;
IdHTTP1 := TIdHTTP.Create(nil);
IdSSLIOHandlerSocket1 := TIdSSLIOHandlerSocket.Create(nil);
IdSSLIOHandlerSocket1.SSLOptions.Method := sslvSSLv23;
IdHTTP1.IOHandler := IdSSLIOHandlerSocket1;
IdHTTP1.HandleRedirects := true;
IdHTTP1.ReadTimeout := 5000;
IdHTTP1.Intercept := idLogFile1;
paramList:=TStringList.create;
paramList.Clear;
paramList.Add('loguser=testuser');
paramList.Add('logpass=duke7aunt');
paramList.Add('logclub=8005');
url := 'https://www.dfcdata.co.uk/integration/reports.php?report=live';
try
IdHTTP1.Post(url,paramList);
except
on E:Exception do
begin
showMessage('failed to post to: '+url);
ShowMessage('Exception message = '+E.Message);
end;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
reportType : String;
begin
PostData(Self);
reportType := 'live';
GetUrlToFile('',reportType+'.csv');
end;
procedure TForm1.GetUrlToFile(AURL, AFile : String);
var
Output : TMemoryStream;
success : Boolean;
begin
success := True;
Output := TMemoryStream.Create;
try
try
IdHTTP1.Get(AURL, Output);
IdHTTP1.Disconnect;
except
on E : Exception do
begin
ShowMessage('Get failed to GET from '+IdHTTP1.GetNamePath +'. Exception message = '+E.Message);
success := False;
end;
end;
if success = True then
begin
showMessage('Filed saved');
Output.SaveToFile(AFile);
end;
finally
Output.Free;
end;
end;
每次尝试都会出现"IOHandler无效"错误.显然,我没有正确地发布到初始页面,但是有人可以就我所缺少的内容向我提出建议吗?还可以在登录后直接点击下载URL,还是必须使用Cookie?
On each try I get "IOHandler is not valid" error. Obviously I'm not posting correctly to the initial page but can anyone advise me on what I'm missing? Also can I simply then hit the download URL after login or will I have to use cookies?
谢谢
推荐答案
您的代码中有几个错误:
There are several bugs in your code:
1)PostData()
正在请求HTTPS URL,但未将启用SSL的IOHandler分配给TIdHTTP.IOHandler
属性.您需要这样做.
1) PostData()
is requesting an HTTPS URL, but it is not assigning an SSL-enabled IOHandler to the TIdHTTP.IOHandler
property. You need to do so.
2)Button1Click()
正在将URL传递给未指定任何协议的GetUrlToFile()
,因此TIdHTTP
将最终将该URL相对于其现有URL进行处理,因此尝试从https://www.testurl.com/test/testurl.com/test/reports.php
下载https://testurl.com/test/reports.php
.如果要请求相对URL,请不要包含主机名(在这种情况下甚至不要包含路径,因为您是将多个请求发送到同一路径,只是不同的文档).
2) Button1Click()
is passing a URL to GetUrlToFile()
that does not specify any protocol, so TIdHTTP
will end up treating that URL as relative to its existing URL, and thus try to download from https://www.testurl.com/test/testurl.com/test/reports.php
instead of https://testurl.com/test/reports.php
. If you want to request a relative URL, don't include the hostname (or even the path in this case, since you are sending multiple requests to the same path, just different documents).
3)您正在泄漏TIdHTTP对象.
3) you are leaking the TIdHTTP object.
这篇关于使用Indy在Delphi 5中下载CSV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!