Reputation:
I'm am writing a software which will decrypt JDownloader DLC files. This program will send the dlc file to "http://dcrypt.it/decrypt/upload" with POST request and will get decoded links as response.
This is my code which throws that error:
var
Form1: TForm1;
Stream: TStringStream;
Params: TIdMultiPartFormDataStream;
HTTP: TIdHTTP;
implementation
{$R *.dfm}
procedure TForm1.Button2Click(Sender: TObject);
begin
Stream := TStringStream.Create('');
Params := TIdMultiPartFormDataStream.Create;
Params.AddFile('File1', 'D:\3fcaa401f8f3ebe32fb93cb607ecadd01ee954ed.dlc', 'application/octet-stream');
HTTP.Post('http://dcrypt.it/decrypt/upload', Params, Stream);
ShowMessage(Stream.DataString);
end;
When I debug my program, Delphi IDE shows these in IdHTTP.pas:
function TIdCustomHTTP.GetRequest: TIdHTTPRequest;
begin
Result := FHTTPProto.Request; //This line is highlighted.
end;
So, what should I do in order to fix this issue?
Upvotes: 1
Views: 1844
Reputation: 28517
You are not initializing the HTTP component. Since it is a global variable it will be automatically initialized to nil
.
When you call HTTP.Post('http://dcrypt.it/decrypt/upload', Params, Stream);
it will not break immediately at the place of call. You are calling it on a nil
reference, so it causes the AV as soon as it accesses some inner field or attempts to call a virtual method.
You have to construct the HTTP component before you use it:
HTTP := TIdHTTP.Create(nil);
try
...
finally
HTTP.Free;
end;
Also, good practice is that you should avoid using global variables. You can and should declare all the necessary variables as local. And make sure that you release everything after you are done.
Upvotes: 6