3
votes

I have a Delphi XE6 application which I would like to install on an Android phone. I am testing it on a Windows platform. Below is my unit source code. On my form I have a button, idHttp Indy tool and a Memo Field.

The problem is that when I click the button I get the error

HTTP/1.1 403 Forbidden.

But I can access the site through my browser, so the page isn't forbidden. Can anyone see a problem?

I have searched for how to do post requests using firemonkey and delphi and I found these links:

None of which help me.

unit Start_Interface_u;

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes,
  System.Variants,
  FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.StdCtrls,
  IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient, IdHTTP,
  FMX.Layouts, FMX.Memo;

type
  TForm1 = class(TForm)
    btnLogin: TButton;
    IdHTTP1: TIdHTTP;
    Memo1: TMemo;
    procedure btnLoginClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

  {$R *.fmx}


  procedure TForm1.btnLoginClick(Sender: TObject);
  Var
    sResult: String;
    DataToSend :TStringList;
  begin

  DataToSend := TStringList.Create;
  //DataToSend.Add('login_email=some_email');
  //DataToSend.Add('login_pass=some_password');
  IdHTTP1.Request.ContentType := 'application/x-www-form-urlencoded';

  Memo1.Lines.Add(idHTTP1.Post('http://mysite.co.za/dash.php', DataToSend));

  end;

end.
2

2 Answers

0
votes

I would try to set the user agent to simulate a common web browser, for example (taken from this answer)

Request.UserAgent := 
      'Mozilla/5.0 (Windows NT 5.1; rv:2.0b8) Gecko/20100101 Firefox/4.0b8';
0
votes

403 means that HTTP layer authentication is missing or sending incorrect credentials. TIdHTTP uses a plugin architecture for handling HTTP authentication. You need to add various IdAuthentication... units, or the IdAllAuthentications unit, to your uses clause to enable the desired HTTP authentications that Indy supports, like NTLM or Digest. There is also an TIdHTTP.OnSelectAuthorization event for finer control over which authentication type to use if the server supports multiple types, and an TIdHTTP.OnAuthorization event for prompting you for new credentials whenever the server asks for them.