1,594
社区成员




uses WinINet, Windows, Sysutils, Classes, IDURI;
procedure Post(url, data:string;res:TStream);
var
hInt,hConn,hreq:HINTERNET;
buffer:PChar;
dwRead, dwFlags:cardinal;
port: Word;
uri: TIdURI;
proto, host, path: string;
begin
uri := TIdURI.Create(url);
host := uri.Host;
path := uri.Path + uri.Document;
proto := uri.Protocol;
uri.Free;
if UpperCase(proto) = 'HTTPS' then
begin
port := INTERNET_DEFAULT_HTTPS_PORT;
dwFlags := INTERNET_FLAG_SECURE;
end
else
begin
port := INTERNET_INVALID_PORT_NUMBER;
dwFlags := INTERNET_FLAG_RELOAD;
end;
hInt := InternetOpen('Delphi',INTERNET_OPEN_TYPE_PRECONFIG,nil,nil,0);
hConn := InternetConnect(hInt,PChar(host),port,nil,nil,INTERNET_SERVICE_HTTP,0,0);
hreq := HttpOpenRequest(hConn,'POST',PChar(Path),'HTTP/1.1',nil,nil,dwFlags,0);
GetMem(buffer, 65536);
if HttpSendRequest(hReq,nil,0,PChar(data),Length(data)) then
begin
dwRead:=0;
repeat
InternetReadFile(hreq,buffer,65536,dwRead);
if dwRead<>0 then
res.Write(buffer^, dwRead);
until dwRead=0;
end;
InternetCloseHandle(hreq);
InternetCloseHandle(hConn);
InternetCloseHandle(hInt);
FreeMem(buffer);
end;
var value: DWORD;
value = SECURITY_FLAG_IGNORE_CERT_CN_INVALID or
SECURITY_FLAG_IGNORE_CERT_DATE_INVALID or
SECURITY_FLAG_IGNORE_UNKNOWN_CA or
SECURITY_FLAG_IGNORE_CERT_WRONG_USAGE;
WinHttpSetOption(hreq, WINHTTP_OPTION_SECURITY_FLAGS, @value, SizeOf(value));
设置一下option,就能正常访问证书有问题的站点了