[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 990
  • Last Modified:

I need help with Fpiettes THttpCli .....

I was using some other freeware web components... but they were buggy so I'm switching over to Fpiettes stuff... but I'm having some trouble handling the data... here is the code I'm playing with


unit HttpGet1;

interface

uses
  WinTypes, WinProcs, Messages, SysUtils, Classes, Graphics, Controls, Forms,
  Dialogs, HttpProt, StdCtrls, UrlLabel, Sock;

type
  THttpGetForm = class(TForm)
    GetButton: TButton;
    AbortButton: TButton;
    InfoLabel: TLabel;
    HttpCli1: THttpCli;
    procedure GetButtonClick(Sender: TObject);
    procedure HttpCli1DocData(Sender: TObject; Buffer: Pointer;
      Len: Integer);
    procedure HttpCli1HeaderData(Sender: TObject);
    procedure AbortButtonClick(Sender: TObject);
  private

  public

  end;

var
  HttpGetForm: THttpGetForm;
  MyArray : Array[1..1000] of byte;

implementation

{$R *.DFM}


procedure THttpGetForm.GetButtonClick(Sender: TObject);
begin
    HttpCli1.URL        := ' http://www.gurlpages.com/computer/gwena/atest.txt';
    HttpCli1.Proxy      := '';
    HttpCli1.ProxyPort  := '80';
    HttpCli1.RcvdStream := TFileStream.Create('c:\windows\temp\atest.txt', fmCreate);
    GetButton.Enabled   := FALSE;
    AbortButton.Enabled := TRUE;
    InfoLabel.Caption   := 'Loading';
    try
        try
            HttpCli1.Get;
            InfoLabel.Caption := IntToStr(HttpCli1.RcvdStream.Size) ;
        except
            on E: EHttpException do begin
                InfoLabel.Caption := 'Failed : ' +
                                     IntToStr(HttpCli1.StatusCode) + ' ' +
                                     HttpCli1.ReasonPhrase;;
            end
            else
                raise;
        end;
    finally
        GetButton.Enabled   := TRUE;
        AbortButton.Enabled := FALSE;
        HttpCli1.RcvdStream.Destroy;
        HttpCli1.RcvdStream := nil;
    end;
end;



procedure THttpGetForm.HttpCli1DocData(Sender: TObject; Buffer: Pointer;
  Len: Integer);
begin
    InfoLabel.Caption := IntToStr(HttpCli1.RcvdCount);
end;



procedure THttpGetForm.HttpCli1HeaderData(Sender: TObject);
begin
    InfoLabel.Caption := InfoLabel.Caption + '.';
end;



procedure THttpGetForm.AbortButtonClick(Sender: TObject);
begin
    HttpCli1.Abort;
end;




end.


This works VERY well... it goes to my web site and gets a tiny file 'atest.txt'  and then saves it in the windows\temp\  directory.
What I want to do is instead of getting the file and saving it on disk I just want to get it and somehow put it into the array    MyArray : Array[1..1000] of byte;  Everything I try seems to fail :(  I don't need to store the data onto the disk... the program only needs to have it in the array.... it bugs me to have to save it on disk and then read it back into myarray .... if someone can  actually change the source code above so that it places the data into the array I will add some more points :)

..Gwen..
0
Gwena
Asked:
Gwena
  • 6
  • 6
  • 4
1 Solution
 
MadshiCommented:
Try this:

procedure THttpGetForm.GetButtonClick(Sender: TObject);
var str : string;
begin
    HttpCli1.URL        := ' http://www.gurlpages.com/computer/gwena/atest.txt';
    HttpCli1.Proxy      := '';
    HttpCli1.ProxyPort  := '80';
    HttpCli1.RcvdStream := TMemoryStream.Create;
    GetButton.Enabled   := FALSE;
    AbortButton.Enabled := TRUE;
    InfoLabel.Caption   := 'Loading';
    try
        try
            HttpCli1.Get;
            InfoLabel.Caption := IntToStr(HttpCli1.RcvdStream.Size) ;
            SetLength(str, HttpCli1.RcvdStream.Size);
            Move(HttpCli1.RcvdStream.Memory^, pointer(str)^, Length(str));
            // now you have the content of the file in "str"...
            ShowMessage(str);
        except
            on E: EHttpException do begin
                InfoLabel.Caption := 'Failed : ' +
                                     IntToStr(HttpCli1.StatusCode) + ' ' +
                                     HttpCli1.ReasonPhrase;;
            end
            else
                raise;
        end;
    finally
        GetButton.Enabled   := TRUE;
        AbortButton.Enabled := FALSE;
        HttpCli1.RcvdStream.Destroy;
        HttpCli1.RcvdStream := nil;
    end;
end;

Didn't test it - should work though...

Does it help?

Regards, Madshi.

Regards, Madshi.
0
 
GwenaAuthor Commented:
Hi Madshi :)

It stops on this line

 Move(HttpCli1.RcvdStream.Memory^, pointer(str)^,


and says  'Undeclared Identifier: 'Memory'.

I sure hope this can be made to work :)


If this ends up working will I be limited to the size of
a regular string (255) for my file? Or can I easily use
an AnsiString type?  How much data could an ansi string
hold...... jeez I sound pretty clueless huh ;-)
0
 
scrapdogCommented:
RcvdStream is a generic TStream.  You may have to typecast it to get to the Memory property.

try:

Move((HttpCli1.RcvdStream as TMemoryStream).Memory^, pointer(str)^, Length(str));


0
2018 Annual Membership Survey

Here at Experts Exchange, we strive to give members the best experience. Help us improve the site by taking this survey today! (Bonus: Be entered to win a great tech prize for participating!)

 
GwenaAuthor Commented:
Hi there scrapdog!

That really helped!
Thank heavens you showed up... I was about to pull my hair out ;-)

I have got to find a really really GOOD book and learn more about streams and pointers..... this Delphi Developers Guide
weighs at least 10lbs... but has only a casual mention of these
2 topics :(


...Gwen..
0
 
scrapdogCommented:
If you want to learn about pointers, I suggest just getting a good PASCAL text.  Pointers are a part of the Pascal language...a Pascal textbook will cover them more in depth, and a Delphi text might assume you already know Pascal.

Streams (the encapsulated ones), however, are a part of Delphi, so you probably won't find these in a Pascal textbook.

By the way, does your program work as expected now, or do you need more help?
0
 
MadshiCommented:
Thanx, scrappy for your assistance. I was at sleep (counting sheeps :-).

Gwen, of course you should use an ansi string. The maximal size of an ansi string is about 2 GB...   :-)

Regards, Madshi.
0
 
GwenaAuthor Commented:
Hi scrapdog :)

"By the way, does your program work as expected now, or do you need more help?"

---> 'needs-help' is my middle name ;-)

The program does seem to work ok now .... I am going to
have to send you a few points for your help in fixing up
madshi's code for me .....I was wanting this code so
I would have an easy way to make any program I write
able to get a data file from my website... for update announcements, patches, ...or whatever.


I will get out the box full of books on Turbo Pascal and
see if I can get some info on streams.. It never occured to me that those old Pascal books might help... thanx
0
 
scrapdogCommented:
The sheep were running away from the wolf.

And you are just the person to kill it:

http://www.experts-exchange.com/jsp/qShow.jsp?ta=delphi&qid=10236103 

You have gotten me out of more than one graphical jam, so thanks in advance. =)

0
 
scrapdogCommented:
>I will get out the box full of books
>on Turbo Pascal and
>see if I can get some info on
>streams.. It never occured to me that
>those old Pascal books might help...
>thanx

Nope, you won't find streams in a TP book, except for 'input', 'output', and files.  There aren't any built-in encapsulated stream types.  You need a Delphi text for this stuff. =(

However, you will probably find a lot about using pointers, etc.

Hint:  you can also find get a lot of Pascal specific help at http://www.experts-exchange.com/comp/lang/pascal/   

[ "my" board...hehe =) ]
0
 
GwenaAuthor Commented:
>[ "my" board...hehe =) ]

Wow scrapdog!  You are 'Top Dog' over on that group :-)
0
 
scrapdogCommented:
hehe, makes up for my low Delphi score =)
0
 
MadshiCommented:
scrapdog, the suggestion with the low priority thread on your mouseMove questions sounds good to me. If that is still not good enough, you could forget about WM_MOUSEMOVE and poll the mouse position with GetCursor in a thread. But I don't like polling. If you get WM_MOUSEMOVE to work fast enough, that would be much better...
0
 
scrapdogCommented:
thanks for your input
0
 
GwenaAuthor Commented:
Adjusted points to 75
0
 
GwenaAuthor Commented:
Thanks Madshi!  as always you have been a great help to me :)
0
 
MadshiCommented:
You're welcome! Thanx for the point boost...   :-)
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 6
  • 6
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now