While fighting some domain expiration issues, I've written this post 3 times now haha ... Anyhow ...
First of all, I haven't used Delphi 7 in a long time, I've switched to Lazarus Pascal now that I privately would have to pay the upgrade. Lazarus Pascal is free and sort-off a Delphi 7 clone. I think the code will be identical to Delphi 7 when it comes to this example.
For your purpose, Synapse would be the easier way to go. It's free and it works for Delphi and Lazarus.
Installation is not needed, simply unzip the file (attached below, or download it from the Synapse Download page) and copy the files in "source/lib" to your project directory.
For your purpose, we would need the following function (see HTTPSend Synapse Manual):
function HttpPostURL(const URL, URLData: string; const Data: TStream): Boolean;
A very useful function, and example of use can be found in the THTTPSend object. It implements the POST method of the HTTP protocol. This function is good for POSTing form data. It sends the POST method for a URL document to an HTTP server. You must prepare the form data in the same manner as you would the URL data, and pass this prepared data to "URLdata". The following is a sample of how the data would appear: 'name=Lukas&field1=some%20data'. The information in the field must be encoded by EncodeURLElement function. The returned document is in the "Data" stream. Returns boolean TRUE if all went well.
I took the effort to write a tiny program in Lazarus and tested it with my own modem.
I started with a new program, added a TButton (Button1), two TEdit boxes (edUsername and edPassword) and a TMemo (Memo1).
After that I added httpsend and synacode to the Uses clause.
On button click:
procedure TForm1.Button1Click(Sender: TObject);
var
Response: TMemoryStream;
Variables: string;
begin
// STEP 1 - Try to login
Response := TMemoryStream.Create;
try
// Encode to make sure special chars and spaces get converter properly
Variables:=EncodeURL('Username1='+edUsername.Text+'&Password='+edPassword.Text);
if not HttpPostURL('http://192.168.2.254/login', Variables, Response) then
begin
ShowMessage('Failed to login');
exit;
end;
finally
Response.Free;
end;
// STEP 2 - Retrieve Data after successful login
// This will be different for your router of course
try
HttpGetText('http://192.168.2.254/getpage.gch?pid=1002&nextpage=status_wan_connection_t.gch', Memo1.Lines);
Except
ShowMessage('Failed to retrieve data');
end;
end;
The steps are relatively easy once you know how:
First we will try to login. For this we create a TMemoryStream to catch the Response from HttpPostURL() - in my test, this did not return anything, but the function came back with a TRUE, so the POST did work. After that we encode the variables so characters like spaces will be converted properly (to %20). Then we call HttpPostURL. On failure: exit.
Once logged in, we use HttpGetText to retrieve the URL of the page that holds the data you need and load the HTML into the TMemo.
Obviously your URL's will be slightly different, and the variables used in the FORM will be different as well of course.
I did write the code a little "simple" to keep it readable. Obviously this can be done much cleaner and efficient.
Hope this helps ...