gpt4 book ai didi

delphi - 我的 GetHttp 适用于 HTTP,但 HTTPS 返回无效数据

转载 作者:行者123 更新时间:2023-12-03 15:30:02 26 4
gpt4 key购买 nike

我的 GetHttp 适用于 HTTP,但 HTTPS 返回无效数据,这是测试过程:

GetHTTP(' https://localhost/ ','',temp,true); temp.SaveToFile('c:\temp.txt');

如何解决,有什么想法吗?

function GetHTTP(AUrl, APostStr:ansistring; AStream:TStream; noncached :boolean =false): integer;
var
Retry: Boolean;
hSession, hConnect, hRequest: hInternet;
RequestMethod, Header: ansistring;
Buf:array[0..1023] of Char;
ReadCount:Cardinal;
dwError, dwErrorCode: DWORD;
ptNil: Pointer;

UrlInfo: TUrlInfo;
flags : DWORD ;

procedure SetTimeOut;
var
TimeOut: integer;
begin
TimeOut := 5 * 1000;
InternetSetOption(hSession, INTERNET_OPTION_RECEIVE_TIMEOUT, @TimeOut,
SizeOf(TimeOut));
end;

procedure GetHttpStatus; //StatusCode
var
Len, Reserved: DWORD;
begin
Reserved := 0;
Len := SizeOf(Result);
HttpQueryInfo(hRequest, HTTP_QUERY_STATUS_CODE or HTTP_QUERY_FLAG_NUMBER,
@Result, Len, Reserved) ;
end;

begin



flags := 0;
if noncached = true then flags := INTERNET_FLAG_RELOAD;

// flags := INTERNET_FLAG_SECURE; THIS DID NOT HELP ??!!! output was blank with it

Result := 0;
if not GetUrlInfo(AUrl, UrlInfo) then Exit;
hSession := InternetOpen(nil, INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0);
try
SetTimeOut;
hConnect := InternetConnect(hSession, pchar(UrlInfo.hostname),urlinfo.port,nil,nil,INTERNET_SERVICE_HTTP,INTERNET_FLAG_EXISTING_CONNECT ,0);

try
if APostStr = '' then
RequestMethod := 'GET'
else
RequestMethod := 'POST';

hRequest := HttpOpenRequest(hConnect, PChar(RequestMethod),Pchar(urlinfo.urlpath), 'HTTP/1.0', nil, nil,flags, 0);

try
if APostStr = '' then
Header := ''
else
Header := 'Content-type: application/x-www-form-urlencoded';


Retry:=True;
while Retry do
begin
if HttpSendRequest(hRequest, PChar(Header),Length(Header), PChar(APostStr), Length(APostStr)) then
dwErrorCode:=ERROR_SUCCESS
else
dwErrorCode:=GetLastError;
dwError:=InternetErrorDlg(application.Handle, hRequest, dwErrorCode,
FLAGS_ERROR_UI_FILTER_FOR_ERRORS or
FLAGS_ERROR_UI_FLAGS_CHANGE_OPTIONS or
FLAGS_ERROR_UI_FLAGS_GENERATE_DATA,
ptNil);
Retry:=(dwError=ERROR_INTERNET_FORCE_RETRY);
end;

//HttpSendRequest(hRequest, PChar(Header),Length(Header), PChar(APostStr), Length(APostStr));



GetHttpStatus;
if Result <> HTTP_STATUS_OK then Exit;
while True do
begin
if not InternetReadFile(hRequest, @Buf, SizeOf(Buf), ReadCount) then
Break;
//res := GetLastError();
if ReadCount = 0 then
Break
else
AStream.Write(Buf, ReadCount);
end;
finally
InternetCloseHandle(hRequest);
end;
finally
InternetCloseHandle(hConnect);
end;
finally
InternetCloseHandle(hSession);
end;
end;

最佳答案

我只在 Indy 中这样做过,它使用 OpenSSL并且需要一些额外的 DLL。您还需要显式地为 SSL 创建 IO 处理程序。这是一个简化的代码示例。

MyHTTP := GetIdHTTP(True);
MyHTTP.Get(SomeSecureURL, MyStream);

...

function GetIdHTTP(aSecure: Boolean = False): TIdHTTP;
var
lSSLIOHandler: TIdSSLIOHandlerSocketOpenSSL;
begin
Result := TIdHTTP.Create(nil);

if aSecure then
begin
lSSLIOHandler := TIdSSLIOHandlerSocketOpenSSL.Create;
Result.IOHandler := lSSLIOHandler;
end;
end;

关于delphi - 我的 GetHttp 适用于 HTTP,但 HTTPS 返回无效数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1789606/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com