Exsample of “Windows HTTP Services” in Delphi
2 min readJan 2, 2021
Hi, I am mohri.
Delphi comes standard with many useful components for HTTP connections.However, this time I will introduce the method using “Windows HTTP Services”. In Visual C++ etc., it is a library called winhttp.h. I think Delphi users are a library that they don’t really touch.
The actual code example is below.
procedure TForm1.FormCreate(Sender: TObject);
var
hand1, hConn, hReq: HINTERNET;
begin
hand1 := WinHttpOpen('Mohri/1.0', WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, 0);
if Assigned(hand1) then
begin
hConn := WinHttpConnect(hand1,'mjeld.com', INTERNET_DEFAULT_PORT, 0);
if Assigned(hConn) then
begin
hReq := WinHttpOpenRequest(hConn, 'GET', 'index.php', nil, WINHTTP_NO_REFERER, WINHTTP_NO_REFERER, 0);
if Assigned(hReq) then
begin
var bRes:LongBool := WinHttpSendRequest(hReq,WINHTTP_NO_ADDITIONAL_HEADERS,0,WINHTTP_NO_REQUEST_DATA,0,0,0);
if bRes then
begin
var ss := TStringStream.Create('', TEncoding.UTF8);
OutputDebugString('OK');
if WinHttpReceiveResponse(hReq, nil) then
begin
var s1,d1: DWORD;
var buf: TBytes;
WinHttpQueryDataAvailable(hReq, @s1);
SetLength(buf, s1 + 1);
WinHttpReadData(hReq,buf[0],s1,@d1);
ss.WriteBuffer(buf, d1);
ss.Position := 0;
OutputDebugString(PChar(ss.DataString));
end;
end;
end;
WinHttpCloseHandle(hReq);
end;WinHttpCloseHandle(hConn);
end;
WinHttpCloseHandle(hand1);end;
In the previous article, I used WinRT’s HttpClient. You can see the difference by comparing.
I write the same article in Japanese. If you are interested, please translate it.