I need an example of webrequest in delphi that request a web url and get its context...
thanks...
uses
WinINet;
procedure CrackURL(const URL: String; out Scheme: Word;
out UserName, Password, Host: String; out Port: Word; out ObjName: String);
var
Parts: TURLComponents;
CanonicalURL: String;
Size: Cardinal;
begin
FillChar(Parts, SizeOf(TURLComponents), 0);
Parts.dwStructSize := SizeOf(TURLComponents);
if URL <> '' then
begin
Size := 3 * Length(URL);
SetString(CanonicalURL, nil, Size);
if not InternetCanonicalizeUrl(PChar(URL), PChar(CanonicalURL), Size, ICU_NO_META) then
Size := 0;
SetLength(CanonicalURL, Size);
Parts.dwSchemeLength := 1;
Parts.dwUserNameLength := 1;
Parts.dwPasswordLength := 1;
Parts.dwHostNameLength := 1;
Parts.dwURLPathLength := 1;
Parts.dwExtraInfoLength := 1;
InternetCrackUrl(PChar(CanonicalURL), Size, 0, Parts);
end;
Scheme := Parts.nScheme;
SetString(UserName, Parts.lpszUserName, Parts.dwUserNameLength);
SetString(Password, Parts.lpszPassword, Parts.dwPasswordLength);
SetString(Host, Parts.lpszHostName, Parts.dwHostNameLength);
Port := Parts.nPort;
SetString(ObjName, Parts.lpszUrlPath, Parts.dwUrlPathLength + Parts.dwExtraInfoLength);
end;
function HTTPGet(const URL: String): String;
const
AcceptType: array[0..1] of PChar = ('*/*', nil);
var
hINet, hConn, hReq: HINTERNET;
UserName, Password, Host, ObjName: String;
Scheme, Port: Word;
ReqFlags, Size: Cardinal;
Stream: TStringStream;
Buffer: array[0..255] of Byte;
begin
Result := '';
hINet := InternetOpen('Mozila/5.0', INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0);
if hINet <> nil then
try
CrackURL(URL, Scheme, UserName, Password, Host, Port, ObjName);
hConn := InternetConnect(hINet, PChar(Host), Port, PChar(UserName),
PChar(Password), INTERNET_SERVICE_HTTP, 0, 0);
if hConn <> nil then
try
ReqFlags := INTERNET_FLAG_RELOAD or INTERNET_FLAG_PRAGMA_NOCACHE
or INTERNET_FLAG_NO_CACHE_WRITE or INTERNET_FLAG_NO_COOKIES
or INTERNET_FLAG_NO_UI or INTERNET_FLAG_KEEP_CONNECTION;
if Scheme = INTERNET_SCHEME_HTTPS then
ReqFlags := ReqFlags or INTERNET_FLAG_SECURE;
hReq := HttpOpenRequest(hConn, 'GET', PChar(ObjName), nil, nil, @AcceptType[0], ReqFlags, 0);
if hReq <> nil then
try
if HttpSendRequest(hReq, nil, 0, nil, 0) then
begin
Stream := TStringStream.Create('');
try
while InternetReadFile(hReq, @Buffer[0], SizeOf(Buffer), Size) and (Size <> 0) do
Stream.Write(Buffer[0], Size);
Result := Stream.DataString;
finally
Stream.Free;
end;
end;
finally
InternetCloseHandle(hReq);
end;
finally
InternetCloseHandle(hConn);
end;
finally
InternetCloseHandle(hINet);
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
Screen.Cursor := crHourGlass;
try
Memo1.Lines.Text := HTTPGet('http://delphiarea.com');
finally
Screen.Cursor := crDefault;
end;
end;
Users browsing this forum: No registered users and 8 guests