3
votes

I am trying to access a URL in Delphi using a TIdHTTP Indy Tool. I have done the following:

  • Set Accept Cookies = True
  • Set Handle Redirect = True
  • Added a TIdCookieManager

http://sms.saicomvoice.co.za:8900/saicom/index.php?action=login&username=SOME_USERNAME&password=SOME_PASSWORD&login=login

The Post request works and it returns the HTML. The problem is it doesn't return the correct HTML (See Image Below).

If I take that URL ( Filling in the username and password ) and paste it into my browser exactly The Same as my Delphi Application would then logs into the correct website. But as soon as I do it with my Delphi App it returns the HTML for the login page.

The request is supposed to be executed timeously in a TTimer in Delphi.

Can anyone lead me unto the right path or point me in a direction as to how I can solve this problem ?

Some Additional Information

  • WriteStatus is a Procedure That writes output to a TListBox
  • BtnEndPoll Stops the timer

    Procedure TfrmMain.TmrPollTimer(Sender: TObject);
    Var
      ResultHTML: String;
      DataToSend: TStringList;
    Begin
      Inc(Cycle, 1);
    
      LstStatus.Items.Add('');
      LstStatus.Items.Add('==================');
      WriteStatus('Cycle : ' + IntToStr(Cycle));
      LstStatus.Items.Add('==================');
      LstStatus.Items.Add('');
    
      DataToSend := TStringList.Create;
    
      Try
        WriteStatus('Setting Request Content Type');
        HttpRequest.Request.ContentType := 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8';
        WriteStatus('Setting Request User Agent');
        HttpRequest.Request.UserAgent := 'Mozilla/5.0 (Windows NT 5.1; rv:2.0b8) Gecko/20100101 Firefox/4.0b8';
    
        WriteStatus('Posting Request');
        ResultHTML := HttpRequest.Post(FPostToURL, DataToSend);
        WriteStatus('Writing Result');
        FLastResponse := ResultHTML;
    
        WriteStatus('Cycle : ' + IntToStr(Cycle) + ' -- FINISHED');
        LstStatus.Items.Add('');
      Except
        On E: Exception Do
          Begin
            MakeNextEntryError := True;
            WriteStatus('An Error Occured: ' + E.Message);
    
            If ChkExceptionStop.Checked Then
              Begin
                BtnEndPoll.Click;
                WriteStatus('Stopping Poll Un Expectedly!');
              End;
          End;
      End;
    End;
    

* Image Example *

HTML Output

1
could you include the relevant part of the HTML as text instead?mjn

1 Answers

4
votes

HttpRequest.Request.ContentType := 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,/;q=0.8';

That is not a valid ContentType value. That kind of value belongs in the Request.Accept property instead. It tells the server which ContentTypes the client will accept in the response.

ResultHTML := HttpRequest.Post(FPostToURL, DataToSend);

You are posting a blank TStringList. Putting a URL into a browser's address bar sends a GET request, not a POST request, so you should be using TIdHTTP.Get() instead:

ResultHTML := HttpRequest.Get('http://sms.saicomvoice.co.za:8900/saicom/index.php?action=login&username=SOME_USERNAME&password=SOME_PASSWORD&login=login');

You would use TIdHTTP.Post() if you wanted to simulate the HTML webform being submitted to the server (since it specifies method=post), eg:

DataToSend.Add('username=SOME_USERNAME');
DataToSend.Add('password=SOME_PASSWORD');
DataToSend.Add('login=Login');
ResultHTML := HttpRequest.Post('http://sms.saicomvoice.co.za:8900/saicom/index.php?action=login', DataToSend);