I'm trying to download a file from "https://www.cbr.ru/s/newbik" resource using TScHttpWebRequest component but it raises an exception "scNotFound".
Here's my code:
Code: Select all
procedure TForm1.Button3Click(Sender: TObject);
var
vRequest: TScHttpWebRequest;
vResponse: TScHttpWebResponse;
vFile: TFileStream;
begin
vRequest := TScHttpWebRequest.Create('https://www.cbr.ru/s/newbik');
try
vResponse := vRequest.GetResponse; // <-- exception raises here
try
vFile := TFileStream.Create('tmp.zip', fmCreate);
vResponse.ReadToStream(vFile);
finally
vResponse.Free;
vFile.Free;
end;
finally
vRequest.Free;
end;
end;
And if you change URI in my samle code to this new location manually - all works well:HTTP/1.1 301 Moved Permanently
...
Location: /Queries/XsltBlock/File/101478?fileId=0
...
Code: Select all
procedure TForm1.Button3Click(Sender: TObject);
var
vRequest: TScHttpWebRequest;
vResponse: TScHttpWebResponse;
vFile: TFileStream;
begin
//vRequest := TScHttpWebRequest.Create('https://www.cbr.ru/s/newbik');
vRequest := TScHttpWebRequest.Create('https://www.cbr.ru/Queries/XsltBlock/File/101478?fileId=0');
try
vResponse := vRequest.GetResponse;
try
vFile := TFileStream.Create('tmp.zip', fmCreate);
vResponse.ReadToStream(vFile);
finally
vResponse.Free;
vFile.Free;
end;
finally
vRequest.Free;
end;
end;
Is it possible to make this component do redirects automatically?