目录

1. 前言

2.功能代码

2.1. 接口单元 InterfaceDll.pas

2.2. 工具类 unt_objects.pas

2.3. 公共单元 uPub.pas

2.4. dll工程导出函数

2.5. 配置文件 set.ini

3. 结语


1. 前言

先看【Delphi Http Https 最好的解决方法(一)】再看这篇文章.

在 【Delphi Http Https 最好的解决方法(一)】,工具类定义了两个TIdHttp对象,一个用于http,另一个用于https请求。

后来我在其他项目中使用该接口的时候,遇到一个问题。

我上传1w条数据记录,我用这个接口来上传数据,碰巧这个请求耗时较长,一个请求要10秒左右,这个耗时太长了,我自己也看不下去了。所以, 【Delphi Http Https 最好的解决方法(一)】的生产使用环境有限,我就搞整了一个类似数据库连接池的工具类,在接口初始化的时候就能够创建多个TIdHttp对象,用于http和https,在上传模块,开启多个线程上传,测试时候开启了10个线程,每个线程处理1000个数据,算是达到要求了。

其实代码和【Delphi Http Https 最好的解决方法(一)】在一起的,在此我简单说明下,分享下。

2.功能代码

2.1. 接口单元 InterfaceDll.pas

unit InterfaceDll;interfaceusesunt_objects, Winapi.Windows, System.SysUtils, System.Classes, EncdDecd, Qjson;vartool: TTools;pools: THttpConnectopnPool;//----------------------------------测试部分------------------------------------
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>//测试
function dll_test: Byte; stdcall;//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<//-------------------------普通 网络请求部分------------------------------------
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>//初始化
function dll_init: Byte; stdcall;
//Post
function dll_post(sUrl, sJson, sHeader: PWideChar; var sOut: PWideChar): Byte; stdcall;
//Get
function dll_get(sUrl, sJson, sHeader: PWideChar; var sOut: PWideChar): Byte; stdcall;
//释放
function dll_uninit: Byte; stdcall;//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<//-----------------------连接池 网络请求部分------------------------------------
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>//初始化
function dll_Pool_init: Byte; stdcall;
//Post
function dll_Pool_post(sUrl, sJson, sHeader: PWideChar; var sOut: PWideChar): Byte; stdcall;
//Get
function dll_Pool_get(sUrl, sJson, sHeader: PWideChar; var sOut: PWideChar): Byte; stdcall;
//释放
function dll_Pool_uninit: Byte; stdcall;//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<implementationuses uPub, uSuperObject, qaes;//测试
function dll_test: Byte; stdcall;
beginResult:= 1;
end;//-------------------------普通 网络请求部分------------------------------------
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>//初始化
function dll_init: Byte;
beginResult:= 0;if not Assigned(tool) thentool:= TTools.Create;Result:= 1;
end;/// <summary>
///   POST请求
/// </summary>
function dll_post(sUrl, sJson, sHeader: PWideChar; var sOut: PWideChar): Byte;
varjson, jsArr: TQjson;I:integer;bHttps: Boolean;
beginResult:= 0;bHttps:= (Pos('https:', sUrl)>0);if Assigned(tool) thenbeginif tool._debug thensystemLog('[dll_post]: '+ AnsiString(sJson));json:= TQJson.Create;tryjson.Parse(sHeader);tool._Https.Request.CustomHeaders.Clear;jsArr:= json.ItemByName('params');if jsArr<> nil thenbeginfor I := 0 to jsArr.Count- 1 dotool._Https.Request.CustomHeaders.Values[jsArr.Items[I].ValueByName('key','')]:= jsArr.Items[I].ValueByName('value','')end;finallyFreeAndNil(json);end;Result:= tool.SendPost(bHttps, sUrl, sJson, sOut);endelsebeginsystemLog('[dll_post]: '+ Err_02);Exit;end;
end;//Get
function dll_get(sUrl, sJson, sHeader: PWideChar; var sOut: PWideChar): Byte;
varjson: ISuperObject;jsArr: TSuperArray;I:integer;bHttps: Boolean;
beginResult:= 0;sOut:= '';bHttps:= (Pos('https:', sUrl)>0);if Assigned(tool) thenbeginif tool._debug thensystemLog('[dll_post]: '+ AnsiString(sJson));if sHeader<>'' thenjson:= SO(sHeader);if json<>nil thenbegintool._Https.Request.CustomHeaders.Clear;jsArr:= json.O['headers'].AsArray;for I := 0 to jsArr.Length- 1 dobeginif bHttps thentool._Https.Request.CustomHeaders.Values[jsArr.O[I].S['key']]:= jsArr.O[I].S['value']elsetool._Https.Request.CustomHeaders.Values[jsArr.O[I].S['key']]:= jsArr.O[I].S['value'];end;end;Result:= tool.SendGet(bHttps, sUrl, sJson, sOut);endelsebeginsystemLog('[dll_get]: '+ Err_02);Exit;end;
end;//释放
function dll_uninit: Byte;
beginresult:= 0;if Assigned(tool) thenFreeAndNil(tool);result:= 1;
end;//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<//-----------------------连接池 网络请求部分------------------------------------
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>//初始化
function dll_Pool_init: Byte; stdcall;
beginResult:= 0;if not Assigned(pools) thenpools:= THttpConnectopnPool.Create(nil);Result:= 1;
end;//Post
function dll_Pool_post(sUrl, sJson, sHeader: PWideChar; var sOut: PWideChar): Byte; stdcall;
varjson, jsArr: TQjson;I:integer;bHttps: Boolean;conn: THttpConnection;
beginResult:= 0;bHttps:= (Pos('https:', sUrl)>0);if not Assigned(pools) thenbeginsystemLog('[dll_post]: '+ Err_02);Exit;end;conn:= pools.getHttpConnection(bHttps);tryjson:= TQJson.Create;tryjson.Parse(sHeader);conn.Request.CustomHeaders.Clear;jsArr:= json.ItemByName('params');if jsArr<> nil thenbeginfor I := 0 to jsArr.Count- 1 doconn.Request.CustomHeaders.Values[jsArr.Items[I].ValueByName('key','')]:= jsArr.Items[I].ValueByName('value','')end;finallyFreeAndNil(json);end;Result:= conn.SendPost(bHttps, sUrl, sJson, sOut);finallypools.returnHttpConnection(bHttps, conn);end;
end;//Get
function dll_Pool_get(sUrl, sJson, sHeader: PWideChar; var sOut: PWideChar): Byte; stdcall;
varjson: ISuperObject;jsArr: TSuperArray;I:integer;bHttps: Boolean;conn: THttpConnection;
beginResult:= 0;sOut:= '';bHttps:= (Pos('https:', sUrl)>0);if not Assigned(pools) thenbeginsystemLog('[dll_get]: '+ Err_02);Exit;end;conn:= pools.getHttpConnection(bHttps);tryif sHeader<>'' thenjson:= SO(sHeader);if json<>nil thenbegintool._Https.Request.CustomHeaders.Clear;jsArr:= json.O['headers'].AsArray;for I := 0 to jsArr.Length- 1 dobeginif bHttps thentool._Https.Request.CustomHeaders.Values[jsArr.O[I].S['key']]:= jsArr.O[I].S['value']elsetool._Https.Request.CustomHeaders.Values[jsArr.O[I].S['key']]:= jsArr.O[I].S['value'];end;end;Result:= conn.SendGet(bHttps, sUrl, sJson, sOut);finallypools.returnHttpConnection(bHttps, conn);end;
end;//释放
function dll_Pool_uninit: Byte; stdcall;
beginResult:= 0;if Assigned(pools) thenFreeAndNil(pools);Result:= 1;
end;//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<end.

2.2. 工具类 unt_objects.pas

unit unt_objects;interfaceusesWinapi.Windows, Winapi.Messages, IdHTTP, IdSSLOpenSSL, System.SysUtils,System.Classes, System.IniFiles, System.StrUtils, System.Variants,Winapi.Security.Cryptography, Winapi.WinRT, Winapi.CommonTypes, System.Win.WinRT,Contnrs, Vcl.ExtCtrls, System.DateUtils;constErr_02= '创建对象失败...';GFileName= 'set.ini';type//普通Http请求TTools= classprivateFDebug    : Boolean;            //调试模式FHttp     : TIdHTTP;            //HTTP专用FHttps    : TIdHTTP;            //HTTPS专用FBusy     : Boolean;            //是否忙碌FIdSSL    : TIdSSLIOHandlerSocketOpenSSL;procedure DisConnect(bHttps: Boolean);publishedproperty _debug: Boolean read FDebug write FDebug;property _Https: TIdHTTP read FHttps write FHttps;property _Http: TIdHTTP read FHttp write FHttp;property _Busy: Boolean read FBusy write FBusy;publicconstructor Create();destructor Destroy; override;//发送Post请求function SendPost(bHttps: Boolean; sUrl, sJson: PWideChar; var sOut: PWideChar): Byte;//发送Get请求function SendGet(bHttps: Boolean; sUrl, sJson: PWideChar; var sOut: PWideChar): Byte;end;//Http Https连接池THttpConnection= class(TIdHTTP)public//发送Post请求function SendPost(bHttps: Boolean; sUrl, sJson: PWideChar; var sOut: PWideChar): Byte;//发送Get请求function SendGet(bHttps: Boolean; sUrl, sJson: PWideChar; var sOut: PWideChar): Byte;end;THttpConnectopnPool= class(TComponent)privatefDefault: Integer;  //默认连接池数量fIdleSec: Integer;  //默认连接空闲时间(s)fReqSec:  Integer;  //默认请求超时时间(s)fHttpConnList: TComponentList;      //连接池对象容易fHttpsConnList: TComponentList;     //连接池对象容易fCleanTimer: TTimer;                //连接池定时器procedure fCleanTimerEvent(sender: TObject);  //定时器事件procedure fClean;             //清理空闲链接function fCreateHttpConnection(bHttps: Boolean= False): THttpConnection;  //创建新链接protectedfunction getConnectionCount: Integer;  //获取连接池数量function getUnixTimeStampSecond: Integer;publicconstructor Create(owner: TComponent);destructor Destroy; override;property ConnctionCount: Integer read getConnectionCount;function getHttpConnection(bHttps: Boolean= false): THttpConnection;  //获取连接function returnHttpConnection(bHttps: Boolean; conn: THttpConnection): Boolean;       //归还连接end;implementationuses uPub;{ TTools }constructor TTools.Create;
varsIni: TIniFile;
beginFHttp  := Tidhttp.Create(nil);FHttp.HTTPOptions := [hoKeepOrigProtocol];          //关键参数, 关系到编码自动转换FHttp.HandleRedirects:= True;FHttp.ProtocolVersion:= pv1_1;FHttp.Request.Accept:= '*/*';FHttp.Request.ContentType:= 'application/json;charset=UTF-8';FHttp.Request.Connection:= 'close';FHttp.ReadTimeout:= 30* 1000;FHttp.ConnectTimeout:= 30* 1000;FHttps  := Tidhttp.Create(nil);FHttps.HTTPOptions := [hoKeepOrigProtocol];FHttps.HandleRedirects:= True;FHttps.ProtocolVersion:= pv1_1;FHttps.Request.Accept:= '*/*';FHttps.Request.ContentType:= 'application/json;charset=UTF-8';FHttps.Request.Connection:= 'close';FHttps.ReadTimeout:= 30* 1000;FHttps.ConnectTimeout:= 30* 1000;FIdSSL  := TIdSSLIOHandlerSocketOpenSSL.Create(nil);FIdSSL.SSLOptions.Method:= sslvSSLv23;FIdSSL.SSLOptions.Mode:= sslmClient;if FileExists(ExtractFilePath(Paramstr(0))+GFileName) thenbeginsIni:= TIniFile.Create(ExtractFilePath(Paramstr(0))+GFileName);trycase sIni.ReadInteger('hq','sslver',1) of0: FIdSSL.SSLOptions.Method:= sslvSSLv2;1: FIdSSL.SSLOptions.Method:= sslvSSLv23;2: FIdSSL.SSLOptions.Method:= sslvSSLv3;3: FIdSSL.SSLOptions.Method:= sslvTLSv1;4: FIdSSL.SSLOptions.Method:= sslvTLSv1_1;5: FIdSSL.SSLOptions.Method:= sslvTLSv1_2;end;finallyFreeAndNil(sIni);end;end;FHttps.IOHandler:= FIdSSL;
end;destructor TTools.Destroy;
beginif Assigned(FHttps) thenFreeAndNil(FHttps);if Assigned(FHttp) thenFreeAndNil(FHttp);inherited;
end;procedure TTools.DisConnect(bHttps: Boolean);
beginif bHttps thenbeginif FHttps.Connected thenFHttps.Disconnect;endelsebeginif FHttp.Connected thenFHttp.Disconnect;end;
end;function TTools.SendGet(bHttps: Boolean; sUrl, sJson: PWideChar; var sOut: PWideChar): Byte;
varResponseStream: TStringStream;
beginResult:= 0;sOut:= '';DisConnect(bHttps);ResponseStream:= TStringStream.Create('', TEncoding.UTF8);trytrysystemLog('Snd: '+ sJson);FHttps.Get(sUrl, ResponseStream);sOut:= PWideChar(UTF8Decode(AnsiToUtf8(ResponseStream.DataString)));systemLog('Rcv: '+ sOut);Result:= 1;excepton e: Exception dobeginsystemLog('exp: '+ e.Message);end;end;finallyDisConnect(bHttps);end;
end;function TTools.SendPost(bHttps: Boolean; sUrl, sJson: PWideChar; var sOut: PWideChar): Byte;
varResquestStream,ResponseStream : TStringStream;
beginResult:= 0;sOut:= '';DisConnect(bHttps);trysystemLog('Snd: '+ sJson);ResquestStream := TStringStream.Create(UTF8Encode(sJson));ResponseStream := TStringStream.Create('', TEncoding.UTF8);//ResponseStream := TStringStream.Create('');tryif bHttps thenFHttps.Post(sUrl, ResquestStream, ResponseStream)elseFHttp.Post(sUrl, ResquestStream, ResponseStream);sOut := PWideChar(UTF8Decode(AnsiToUtf8(ResponseStream.DataString)));//sOut := PWideChar(UTF8Decode(WideString(ResponseStream.DataString)));systemLog('Rcv: '+ sOut);Result:= 1;excepton e: Exception dosystemLog('Exp: '+ e.Message);end;finallyDisConnect(bHttps);end;
end;{ THttpConnection }function THttpConnection.SendGet(bHttps: Boolean; sUrl, sJson: PWideChar;var sOut: PWideChar): Byte;
varResponseStream: TStringStream;
beginResult:= 0;sOut:= '';DisConnect(bHttps);ResponseStream:= TStringStream.Create('', TEncoding.UTF8);trytrysystemLog('Snd: '+ sJson);Get(sUrl, ResponseStream);sOut:= PWideChar(UTF8Decode(AnsiToUtf8(ResponseStream.DataString)));systemLog('Rcv: '+ sOut);Result:= 1;excepton e: Exception dobeginsystemLog('exp: '+ e.Message);end;end;finallyDisConnect(bHttps);end;
end;function THttpConnection.SendPost(bHttps: Boolean; sUrl, sJson: PWideChar;var sOut: PWideChar): Byte;
varResquestStream,ResponseStream : TStringStream;
beginResult:= 0;sOut:= '';DisConnect(bHttps);trysystemLog('Snd: '+ sJson);ResquestStream := TStringStream.Create(UTF8Encode(sJson));ResponseStream := TStringStream.Create('', TEncoding.UTF8);//ResponseStream := TStringStream.Create('');tryif bHttps thenPost(sUrl, ResquestStream, ResponseStream)elsePost(sUrl, ResquestStream, ResponseStream);sOut := PWideChar(UTF8Decode(AnsiToUtf8(ResponseStream.DataString)));//sOut := PWideChar(UTF8Decode(WideString(ResponseStream.DataString)));systemLog('Rcv: '+ sOut);Result:= 1;excepton e: Exception dosystemLog('Exp: '+ e.Message);end;finallyDisConnect(bHttps);end;
end;{ THttpConnectopnPool }constructor THttpConnectopnPool.Create(owner: TComponent);
begininherited Create(owner);fDefault:= 20;fIdleSec:= 600;if not Assigned(fHttpConnList) thenfHttpConnList:= TComponentList.Create;if not Assigned(fHttpsConnList) thenfHttpsConnList:= TComponentList.Create;if (fCleanTimer= nil) thenbeginfCleanTimer:= TTimer.Create(Self);fCleanTimer.Name:= 'CleanTimer';fCleanTimer.Interval:= 10* 60* 1000;fCleanTimer.OnTimer:= fCleanTimerEvent;fCleanTimer.Enabled:= True;end;
end;destructor THttpConnectopnPool.Destroy;
varindex: Integer;http: TIdHttp;
beginfor index := fHttpConnList.Count-1 downto 0 dobeginhttp:= TIdHttp(fHttpConnList[index]);http.Disconnect;FreeAndNil(http);end;for index := fHttpsConnList.Count-1 downto 0 dobeginhttp:= TIdHttp(fHttpsConnList[index]);http.Disconnect;FreeAndNil(http);end;inherited;
end;procedure THttpConnectopnPool.fClean;
varindex, iNow: Integer;
beginiNow:= getUnixTimeStampSecond;for index:= fHttpConnList.Count-1 downto 0 dobeginif THttpConnection(fHttpConnList[index]).Tag>0 then //未使用得连接beginif fHttpConnList.Count> fDefault thenbeginif iNow-THttpConnection(fHttpConnList[index]).Tag> fIdleSec thenfHttpConnList.Delete(index);end;endelsebeginif iNow+ THttpConnection(fHttpConnList[index]).Tag> fReqSec thenbeginfHttpConnList.Delete(index);if fHttpConnList.Count< fDefault thenfHttpConnList.Add(fCreateHttpConnection);end;end;end;for index:= fHttpsConnList.Count-1 downto 0 dobeginif THttpConnection(fHttpsConnList[index]).Tag>0 then //未使用得连接beginif fHttpsConnList.Count> fDefault thenbeginif iNow-THttpConnection(fHttpsConnList[index]).Tag> fIdleSec thenfHttpsConnList.Delete(index);end;endelsebeginif iNow+ THttpConnection(fHttpsConnList[index]).Tag> fReqSec thenbeginfHttpsConnList.Delete(index);if fHttpsConnList.Count< fDefault thenfHttpsConnList.Add(fCreateHttpConnection(True));end;end;end;
end;procedure THttpConnectopnPool.fCleanTimerEvent(sender: TObject);
beginTTimer(sender).Enabled:= False;trytryfClean;excepton e: Exception dosystemLog('[fCleanTimerEvent]: '+ e.Message);end;finallyTTimer(sender).Enabled:= True;end;
end;function THttpConnectopnPool.fCreateHttpConnection(bHttps: Boolean): THttpConnection;
varFHttp: THttpConnection;FIdSSL    : TIdSSLIOHandlerSocketOpenSSL;sIni: TIniFile;
beginResult:= nil;FHttp:= THttpConnection.Create(nil);FHttp.HTTPOptions := [hoKeepOrigProtocol];          //关键参数, 关系到编码自动转换FHttp.HandleRedirects:= True;FHttp.ProtocolVersion:= pv1_1;FHttp.Request.Accept:= '*/*';FHttp.Request.ContentType:= 'application/json;charset=UTF-8';FHttp.Request.Connection:= 'close';FHttp.ReadTimeout:= 60* 1000;FHttp.ConnectTimeout:= 60* 1000;if bHttps thenbeginFIdSSL  := TIdSSLIOHandlerSocketOpenSSL.Create(nil);FIdSSL.SSLOptions.Method:= sslvSSLv23;FIdSSL.SSLOptions.Mode:= sslmClient;if FileExists(ExtractFilePath(Paramstr(0))+GFileName) thenbeginsIni:= TIniFile.Create(ExtractFilePath(Paramstr(0))+GFileName);trycase sIni.ReadInteger('hq','sslver',1) of0: FIdSSL.SSLOptions.Method:= sslvSSLv2;1: FIdSSL.SSLOptions.Method:= sslvSSLv23;2: FIdSSL.SSLOptions.Method:= sslvSSLv3;3: FIdSSL.SSLOptions.Method:= sslvTLSv1;4: FIdSSL.SSLOptions.Method:= sslvTLSv1_1;5: FIdSSL.SSLOptions.Method:= sslvTLSv1_2;end;finallyFreeAndNil(sIni);end;end;FHttp.IOHandler:= FIdSSL;end;Result:= FHttp;
end;function THttpConnectopnPool.getConnectionCount: Integer;
begin//
end;function THttpConnectopnPool.getHttpConnection(bHttps: Boolean): THttpConnection;
varindex: Integer;
beginResult:= nil;if bHttps thenbeginfor index := 0 to fHttpsConnList.Count- 1 dobeginif THttpConnection(fHttpsConnList[index]).Tag>0 thenbeginResult:= THttpConnection(fHttpsConnList[index]);Result.Tag:= - getUnixTimeStampSecond;end;end;if Result= nil thenbeginResult:= fCreateHttpConnection(bHttps);Result.Tag:= - getUnixTimeStampSecond;fHttpsConnList.Add(Result);end;endelsebeginfor index := 0 to fHttpConnList.Count- 1 dobeginif THttpConnection(fHttpConnList[index]).Tag>0 thenbeginResult:= THttpConnection(fHttpConnList[index]);Result.Tag:= - getUnixTimeStampSecond;end;end;if Result= nil thenbeginResult:= fCreateHttpConnection(bHttps);Result.Tag:= - getUnixTimeStampSecond;fHttpConnList.Add(Result);end;end;if (Result<> nil) and (Result.Connected) thenResult.Disconnect;
end;function THttpConnectopnPool.getUnixTimeStampSecond: Integer;
beginResult:= DateTimeToUnix(Now)-8*60*60;
end;function THttpConnectopnPool.returnHttpConnection(bHttps: Boolean; conn: THttpConnection): Boolean;
beginif bHttps thenResult:= fHttpsConnList.IndexOf(conn)>-1elseResult:= fHttpConnList.IndexOf(conn)>-1;if Result thenbeginif conn.Connected thenconn.Disconnect;conn.Tag:= getUnixTimeStampSecond;end;
end;end.

2.3. 公共单元 uPub.pas

unit uPub;interfaceusesSystem.SysUtils, System.Classes, qaes, qstring, IdHashMessageDigest, IdHash;typeTMD5= class(TIdHashMessageDigest5);TAppPara = classpublicclass function AppPath: string;class function AppName: string;end;TFilePath = class(TAppPara)publicclass function IniFile: string;end;//写日志
procedure systemLog(Msg: AnsiString);
//AES对象初始化
procedure InitEncrypt(sKey, sIv: PWideChar; aesModel, keyType, paddingmodel: integer; var AES: TQAES);
//字符串转MD5
function StrToMD5(sIn: WideString): WideString;implementationprocedure systemLog(Msg: AnsiString);
varF: TextFile;FileName: string;ExeRoad: string;
begintryExeRoad := ExtractFilePath(ParamStr(0));if ExeRoad[Length(ExeRoad)] = '\' thenSetLength(ExeRoad, Length(ExeRoad) - 1);if not DirectoryExists(ExeRoad + 'log') thenbeginCreateDir(ExeRoad + '\log');end;FileName := ExeRoad + '\log\DLL_Log' + FormatDateTime('YYMMDD', NOW) + '.txt';if not FileExists(FileName) thenbeginAssignFile(F, FileName);ReWrite(F);endelseAssignFile(F, FileName);Append(F);Writeln(F, FormatDateTime('HH:NN:SS.zzz ', Now) + Msg);CloseFile(F);except//可能在事务中调用,避免意外Exit;end;
end;procedure InitEncrypt(sKey, sIv: PWideChar; aesModel, keyType, paddingmodel: integer; var AES: TQAES);
varAInitVector: TQAESBuffer;AKeyType: TQAESKeyType;I: Integer;
begincase keyType of0:AKeyType := kt128;1:AKeyType := kt192;2:AKeyType := kt256;end;if aesModel= 0 thenAES.AsECB(sKey, AKeyType)elsebeginfor I := 1 to Length(sIv) doAInitVector[I-1]:= byte(sIv[I-1]);AES.AsCBC(AInitVector, sKey, AKeyType);end;//AES.PaddingMode在AES.AsECB  AES.AsCBC中是默认值的 所以在以下进行单独设置case paddingmodel of0:AES.PaddingMode:= pmZero;1:AES.PaddingMode:= pmPKCS5;2:AES.PaddingMode:= pmPKCS7;end;
end;//字符串转MD5
function StrToMD5(sIn: WideString): WideString;
varMd5Encode: TMD5;
beginMd5Encode:= TMD5.Create;result:= Md5Encode.HashToHex(Md5Encode.HashString(UTF8Encode(sIn)));Md5Encode.Free;
end;{ TAppPara }class function TAppPara.AppName: string;
beginResult := ExtractFileName(ParamStr(0));
end;class function TAppPara.AppPath: string;
beginResult := ExtractFilePath(ParamStr(0));
end;{ TFilePath }class function TFilePath.IniFile: string;
beginResult := AppPath + 'set.ini';
end;end.

2.4. dll工程导出函数

exportsdll_init,dll_post,dll_get,dll_uninit,dll_pool_init,dll_pool_post,dll_pool_get,dll_pool_uninit;

2.5. 配置文件 set.ini

[hq]
sslver=1

---------------------------------------------------------------------------------------------------------------------------------

3. 结语

欢迎大家指教,该dll我用于不同项目中,请求正常。

如果生产环境上传单一,两种请求方式都可以,如果用到请求频繁,直接使用连接池方式。

Delphi Http Https 最好的解决方法(二)相关推荐

  1. 记Outlook插件与Web页面交互的各种坑 (含c# HttpWebRequest 连接https 的完美解决方法)

    记Outlook插件与Web页面交互的各种坑 (含c# HttpWebRequest 连接https 的完美解决方法) 参考文章: (1)记Outlook插件与Web页面交互的各种坑 (含c# Htt ...

  2. 360极速浏览器显示https证书无效解决方法

    关于360极速浏览器显示https证书无效解决方法,笔者在网上找了很多,网上的说法概括如下 1.日期不对,右下角调下系统日期 2.证书没导入,ie浏览器导入证书,具体的用搜索工具搜索下 我现在要讲的可 ...

  3. Unity导出APk出错解决方法二

    错误提示(需得打开编辑器log文件才能看到全部log,Unity3d只显示一部分): Error building Player: CommandInvokationFailure: Unable t ...

  4. 思科交换机创建Vlan时出错的解决方法(二)

    目录 问题 解决思路 总结 问题 思科3560机器,当配置多个vlan时候,在鍵入exit或apply时候经常会出现下面的出错信息,使得无法创建新的vlan.问题复现如下: 根因:存储空间不足 SW1 ...

  5. Git报错fatal:Authentication failed for‘https://git……解决方法

    报错就是因为你输入的账号或者密码错误,导致验证失败. 方法一配置全局参数: git config --global user.name "xxx" git config --glo ...

  6. 需要使用新应用以打开此https链接的解决方法

      每次电脑开了机,不一会就会弹出      想关也关不掉,确定也点不了.虽然重启能恢复正常,但每次开机再重启未免有些太麻烦了吧?      没关系,我有办法!       1. 先打开任务管理器 2 ...

  7. 关于 “Makefile:3:***遗漏分隔符。停止 。”解决方法二

    下面时装自他人的: ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 我编写makefile ...

  8. unable to access https://github.com/....解决方法

    git config --global url."https://".insteadOf git:// 可以把 git:// 替换成 https:// 方便使用 https 协议 ...

  9. win10 1803版本Chrome(谷歌浏览器),360浏览器极速内核打不开https网站的解决方法

    win10 1803版本Chrome(谷歌浏览器),360浏览器极速内核打不开https网站的,总是超时或者卡顿.1803的345版本是可以打开的,但是376版本就不行了.经过研究发现用以下方法可以解 ...

最新文章

  1. ORACLE11g中创建裸设备
  2. 在布局空间标注的尺寸量不对_你最关心的4大空间家居尺寸布局,设计师之间的秘密...
  3. reactjs中的事件处理
  4. [reprint]如何编写引导程序 Hello World
  5. 判别分析分为r型和q型吗_电流互感器天天见,但是你真正了解和知道它的一些注意事项吗?...
  6. centos6配置mysql远程访问_Linux服务器配置-VSFTP服务配置(六)
  7. Linux系列在线培训五月盛情开幕!!(5月9日,10日,16日,17日,23日,24日)18:30 - 21:30,
  8. 如何从一个php文件向另一个地址post数据,不用表单和隐藏的变量
  9. php ldap ad 登录验证,PHP中的LDAP身份验证 – 无需密码即可进行身份验证
  10. 爬虫学习笔记(一)初识爬虫
  11. Recoverit for Mac(数据恢复软件)
  12. Java-web下使用RSA进行加密解密操作
  13. MATLAB自带的遗传算法工具箱(GADS)
  14. ext2文件系统源代码之ext2.h
  15. The Productive Programmer 读书笔记
  16. python编程知识大全_python编程入门之二:必备基础知识
  17. 如何将图片文字转换成word
  18. 台式计算机屏幕扩展,台式机屏幕如何扩展
  19. Hash函数与算法、哈希查找、哈希冲突解决方法总结
  20. Realme GT Neo5 SE ROOT 解锁BL教程

热门文章

  1. 智慧城市小程序如何将商家入驻入口一键关闭
  2. Linux网络编程(传输层协议 )—tcp三次握手/四次挥手
  3. 移动WEB开发之流式布局-京东案例
  4. java大顶堆类,构建大顶堆、堆排序实现(java)
  5. cocos2dx3.3显示中文
  6. Appium+python自动化(一)- 环境搭建—上(超详解)
  7. RMD kint 不输出 include=TRUE,echo=TRUE
  8. Openssl 安装及其编译项目时库丢失问题解决方法
  9. android10调用tuya接口完成二维码登录
  10. docx文档怎么排列图片_PDF文档中的图片怎么提取出来?不得不说这两个方法太好用了...