Solved

Question about idhttp GET

Posted on 2008-10-29
14
915 Views
Last Modified: 2013-11-23
I've seen an interesting posting here: http://www.experts-exchange.com/Programming/Languages/Pascal/Delphi/Q_23286908.html

Where I could get a filename for an url which looks like this: http://www.deadlybossmods.com/download.php?id=1

the problem is, it does not result any filename if the url looks like this one: http://www.delphipages.com/edit/count.cfm?ID=3338

can someone make it work with most if not all urls?

kindest regards
s!
0
Comment
Question by:stOrM
  • 7
  • 7
14 Comments
 
LVL 28

Accepted Solution

by:
2266180 earned 250 total points
ID: 22839152
>> the problem is, it does not result any filename if the url looks like this one: http://www.delphipages.com/edit/count.cfm?ID=3338

needs a little modification
unit Unit1;
 

interface
 

uses

  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

  Dialogs, IdHeaderList;
 

type

  TForm1 = class(TForm)

    procedure FormCreate(Sender: TObject);

  private

    fn:string;

    procedure IdHTTP1HeadersAvailable(Sender: TObject;

      AHeaders: TIdHeaderList; var VContinue: Boolean);

    { Private declarations }

  public

    { Public declarations }

  end;
 

var

  Form1: TForm1;
 

implementation
 

uses idhttp;
 

{$R *.dfm}
 

procedure TForm1.IdHTTP1HeadersAvailable(Sender: TObject;

  AHeaders: TIdHeaderList; var VContinue: Boolean);

var

  ContentDisposition: string;

begin

  ContentDisposition := AHeaders.Values['Content-Disposition'];

  if ContentDisposition='' then

    exit;

  ContentDisposition := Copy(ContentDisposition, 1 + Pos(';', ContentDisposition), MaxInt);

  ContentDisposition := StringReplace(ContentDisposition, 'filename=', '', [rfIgnoreCase]);

  ContentDisposition := StringReplace(ContentDisposition, '"', '', [rfReplaceAll]);

  fn := ContentDisposition;

end;
 

procedure TForm1.FormCreate(Sender: TObject);

var h:tidhttp;

    s:TMemoryStream;

    url:string;

begin

  h:=tidhttp.create(nil);

  try

    h.HandleRedirects:=false;

    h.OnHeadersAvailable:=IdHTTP1HeadersAvailable;

    url:='http://www.delphipages.com/edit/count.cfm?ID=3338';

    s:=TMemoryStream.Create;

    try

      repeat

        try

          fn:=ExtractFileName(StringReplace(url, '/', '\', [rfReplaceAll]));

          fn:=StringReplace(fn, '?', '_', [rfReplaceAll]);

          fn:=StringReplace(fn, '&', '_', [rfReplaceAll]);

          fn:=StringReplace(fn, '=', '_', [rfReplaceAll]);

          h.Get(url, s);

          s.SaveToFile(fn);

          break;

        except

          on e:EIdHTTPProtocolException do

          begin

            if (e.ErrorCode mod 300) in [1, 2, 3, 7] then

              url:=h.response.location

            else

              raise

          end;

          on exception do

            raise;

        end;

      until false;

    finally

      freeandnil(s);

    end;

  finally

    freeandnil(h);

  end;

end;
 

end.

Open in new window

0
 
LVL 1

Author Comment

by:stOrM
ID: 22850641
Hi ciuly,
maybe I did a mistake but if I execute your code, I run into an exception telling me:

'EIdHTTPProtocolException HTTP/1.1.302 Object Moved'

kindest regards
s!
0
 
LVL 28

Expert Comment

by:2266180
ID: 22850920
that only happens when running in the IDE and not having the delphi exceptions ignored :)

so, either run it from outside of the ide, or, in ide, menu, tools - debugger options - language exceptions : uncheck stop on delphi exceptions.
0
 
LVL 1

Author Comment

by:stOrM
ID: 22851125
Ah I see, perfect job!
Thank you very much!

Maybe one last word, does this version you have done now works with all of those urls which hides filenames or is it alsmost impossible to extract every one?

kindest regards
s!
0
 
LVL 28

Expert Comment

by:2266180
ID: 22851156
can't say. if the methods used are standardized and are the 2 which I implemented, then yes :) there might be other standard ways of doing it. when you find one, just ask and we'll solve it ;)
0
 
LVL 1

Author Comment

by:stOrM
ID: 22851262
Ok I'll look if I stumble about another one, I'll let you know!
Again thank you very much!

s!
0
 
LVL 28

Expert Comment

by:2266180
ID: 22851754
you're welcome
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 1

Author Comment

by:stOrM
ID: 22852796
Hi ciuly,
I didn't thought I would come back so fast but try this url: http://go.microsoft.com/FWLink/?LinkId=84399
for the example above it will not generate the filename :-(
0
 
LVL 28

Expert Comment

by:2266180
ID: 22852908
it works for me
0
 
LVL 1

Author Comment

by:stOrM
ID: 22852977
Hmm don't know why probably my mistake I got as filename result: _LinkId_84399 instead of wuredist.cab
0
 
LVL 28

Expert Comment

by:2266180
ID: 22853010
yes, in first instance. but when doing the get, it will redirect (302) which will give the new url and thus changing the filename from _LinkId_84399 to wuredist.cab

unless you changed something in the code :P

run my code as it is (only change the url) and you will see it works.
0
 
LVL 1

Author Comment

by:stOrM
ID: 22853104
seems to be something with my other code inside the thread not sure whats wrong in the moment i need more testing
0
 
LVL 28

Expert Comment

by:2266180
ID: 22853598
tae your time. just keep in mind that according to EE rules, since you are having another problem, even though it is related to this question, you need to open a new question because it is actually another problem ;)
there is an ask a related question link just above the memo box you type in, this one: http://www.experts-exchange.com/Programming/Languages/Pascal/Delphi/newQuestionWizardRelated.jsp?qid=23860762
0
 
LVL 1

Author Comment

by:stOrM
ID: 22856260
Ok I'll see I now found the problem myself.
While running your code inside a seperate thread, I was freeing the MemoryStream and IDHHTP Object not inside the thread execution but in destroy. That was my mistake so the downloaded file couldn't be saved to hdd!
I now moved the freeing of both objects into the finally statements in thread execute method and it works like a charm!

kindest regards
s!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now