我正在使用https://indy.fulgan.com/SSL/的zip文件openssl-1.0.2r-i386-win32中的Delphi 2009,Indy ver 10.5498以及libeay32.dll和ssleay32.dll。 范围检查在项目选项中处于打开状态,而在任何地方均未处于关闭状态。
使用下面的代码,该代码是我在this帖子的帮助下通过Remy生成的,可以在打开调试功能的IDE中运行时通过https将数据通过https上传到服务器上的API,也可以使用调试开始。
但是,如果我构建发行版,那么无论是通过IDE运行还是作为exe运行,在result := HttpClient.Post(THE_URL, FormData);
行上都会出现范围检查错误
params列表仅包含to,from,subject,body等,并且在filenames列表中没有附件,即filenames.Count =0。U_GeneralRoutines.TheFileStoreFolder
只是ProgramData中存储SSL DLL的文件夹
由于调试器没有捕获到该错误,因此在调用之前和之后都在两行showmessage中添加了行。当构建为调试程序时,将显示两条消息,并且发布成功。当构建为发行版时,第一个显示,然后我得到范围检查错误。
我不认为POST代码中有错误,那么可能出什么问题了?
function UploadToAPI(params, filenames: TStrings): string;
var
HttpClient: TIdHttp;
IdSSLIOHandler: TIdSSLIOHandlerSocketOpenSSL;
FormData : TIdMultiPartFormDataStream;
i : integer;
PathToSSLlibraries : string;
begin
FormData := TIdMultiPartFormDataStream.Create;
HttpClient:= TIdHttp.Create;
IdSSLIOHandler:= TIdSSLIOHandlerSocketOpenSSL.Create;
PathToSSLlibraries := IncludeTrailingPathDelimiter(U_GeneralRoutines.TheFileStoreFolder);
IdOpenSSLSetLibPath(PathToSSLlibraries); //set path to libeay32.dll and ssleay32.dll in the common ProgramData folder
HttpClient.IOHandler := IdSSLIOHandler;
HttpClient.Request.CustomHeaders.FoldLines := true ;
try
for i := 0 to params.Count - 1 do
FormData.AddFormField(params.Names[i], params.ValueFromIndex[i]);
for i := 0 to filenames.Count - 1 do
FormData.AddFile('attachment', filenames[i]); //works with ver 10.5498 but not with 10.2.5
//add authorisation header
HttpClient.Request.CustomHeaders.Add('Authorization:Basic ' + ATHORISATION_STR); //byte64 encoding of the api key
HttpClient.ProtocolVersion := pv1_1; //get the full server response which allows for just one try-except
HttpClient.HTTPOptions := HttpClient.HTTPOptions + [hoKeepOrigProtocol, hoNoProtocolErrorException, hoWantProtocolErrorContent];
try
showmessage('about to post');
result := HttpClient.Post(THE_URL, FormData); //post to the api
showmessage('posted');
except
on E: Exception do
begin
result := E.ClassName + ': ' + E.message;
raise;
end;
end; //try
finally
FormData.Free;
IdSSLIOHandler.free;
HttpClient.free;
end;
我知道这种情况通常是由发行版中未初始化的变量引起的,这些变量会在IDE /调试中自动初始化。但是,在调用POST之前,我过程中的所有变量似乎都已被初始化。