System Delphi

Title: Post form data with wininet.dll and netmanage component
Question: How do I post data to a form on the web and get the result back?
Answer:
unit posthttpdata;
interface
uses
SysUtils;
type
EPostDataError = Exception;
{** Use WINET.DLL first, then failover to NetManage component }
function PostData(Url: string; const Data : string): string;
{** Use WINET.DLL to make the connection and do everything }
function WinINetPostData(Url: string; const Data : string): string;
{** Use the netmanage HTTP component to do everything }
function NMPostData(Url : string; const Data : string): string;
implementation
uses Psock, NMHttp, WinInet;
{** Use the netmanage HTTP component to do everything }
function NMPostData(Url : string; const Data : string): string;
var
NMHTTP : TNMHTTP;
begin
NMHTTP := TNMHTTP.Create(nil);
with NMHTTP do
begin
OutputFileMode := False;
Post(Url, Data);
Result := Body;
end;
end;
{** Use WINET.DLL to make the connection and do everything }
function WinINetPostData(Url: string; const Data : string): string;
const
sHeader = 'Content-Type: application/x-www-form-urlencoded' + #13#10;
var
hInternetOpen,
hInternetConnect,
hHttpOpenRequest : HINTERNET;
Buffer: array[0..1024] of char;
BytesRead: cardinal;
sServer, sScript : string;
begin
Result := '';
// Parse out stuff
if Pos('http://', Url) 0 then
Delete(Url, 1, Length('http://'));
sServer := Copy(Url, 1, Pos('/', Url) - 1);
sScript := Copy(Url, Pos(sServer, URL) + Length(sServer), Length(Url));
// Initialize WINET
hInternetOpen := InternetOpen('http generic', INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0);
if Assigned(hInternetOpen) then
begin
try
// Connect to server
hInternetConnect := InternetConnect(hInternetOpen, PChar(sServer), 80,
nil, 'HTTP/1.0', INTERNET_SERVICE_HTTP, 0, 0);
if Assigned(hInternetConnect) then
begin
try
// Start requesting page
hHttpOpenRequest := HttpOpenRequest(hInternetConnect, 'POST', PChar(sScript), 'HTTP/1.0', '', 'text/*', INTERNET_FLAG_RELOAD, 0);
if Assigned(hHttpOpenRequest) then
begin
try
// Add headers/form info
HttpAddRequestHeaders(hHttpOpenRequest, sHeader, Length(sHeader), HTTP_ADDREQ_FLAG_REPLACE or HTTP_ADDREQ_FLAG_ADD);
HttpSendRequest(hHttpOpenRequest, nil, 0, PChar(Data), Length(Data));
// Download junk
FillChar(Buffer, SizeOf(Buffer), 0);
repeat
Result := Result + Buffer;
FillChar(Buffer, SizeOf(Buffer), 0);
InternetReadFile(hHttpOpenRequest, @Buffer, SizeOf(Buffer), BytesRead);
until BytesRead = 0;
finally
InternetCloseHandle(hHttpOpenRequest);
end;
end else
EPostDataError.Create('Unable to request page!');
finally
InternetCloseHandle(hInternetConnect);
end;
end else
EPostDataError.Create('Unable to connect to server!');
finally
InternetCloseHandle(hInternetOpen);
end;
end else
raise EPostDataError.Create('Unable to initialize Wininet');
end;
{** Use WINET.DLL first, then failover to NetManage component }
function PostData(Url: string; const Data : string): string;
begin
try
Result := WinINetPostData(Url, Data);
if Result = '' then
Result := NMPostData(Url, Data);
except
On Exception Do
Result := NMPostData(Url, Data);
end;
end;