Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

reading an internet page

bropolwigbropolwig Member Posts: 3
Hi,

I'm fairly new at programming, so bear with me ;-)
I sometimes have a problem reading an internet page using the NMHTTP component. I use the following code fragment to read a page and output it to a text file:

[code]
AnsiString link;
link = "http://www.cnn.com";
NMHTTP1->Get(link);
MyList = new TStringList;
MyList->Add(NMHTTP1->Body);
MyList -> SaveToFile("test.txt");
delete MyList;
[/code]

This works most of the times. However, sometimes the program kind of "hangs". The cpu uses 100% but the page is not loaded. The page is accessible though when I use a normal web browser. So my question is if there is a way to check that the nmhttp component succesfully connects and loads the page or else have it retry the connection or...?

Thanks!

Comments

  • LobotomizerLobotomizer Member Posts: 5
    Hi! I haven't really used this component myself (until now), but perhaps you could try using the Timeout property (where you set the timeout in milliseconds), the OnSuccess and OnFailure events while also checking for exceptions:
    [code]//---------------------------------------------------------------------------
    void __fastcall TForm1::Button1Click(TObject *Sender)
    {
    AnsiString link;
    link = "http://www.cnn.com";

    NMHTTP1->TimeOut = 20000;

    try
    {
    NMHTTP1->Get(link);
    }
    catch(...)
    {
    ShowMessage("Exception in Get(link)");
    }
    }
    //---------------------------------------------------------------------------
    void __fastcall TForm1::NMHTTP1Success(CmdType Cmd)
    {
    TStringList *MyList = new TStringList;

    MyList->Add(NMHTTP1->Body);
    MyList->SaveToFile("test.txt");
    ShowMessage("Success!");

    delete MyList;
    }
    //---------------------------------------------------------------------------
    void __fastcall TForm1::NMHTTP1Failure(CmdType Cmd)
    {
    ShowMessage("Error!");
    }
    //---------------------------------------------------------------------------
    [/code]
  • bropolwigbropolwig Member Posts: 3
    Hi,

    This comment was very helpful. I tried the code with the time out statement and it works nicely. I slightly enhanced it by putting a do-while loop over the try and catch structure so it will keep on retrying to retrieve the internet page in case of an exception.

    Thanks a lot!
Sign In or Register to comment.