Page 1 of 1

Need a Delphi Webrequest Example

PostPosted: January 14th, 2010, 6:46 am
by Sezar
I need an example of webrequest in delphi that request a web url and get its context...
thanks...

Re: Need a Delphi Webrequest Example

PostPosted: January 14th, 2010, 9:55 am
by Kambiz
Here is what you need:

Code: Select all
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;

And here is an example of usage:

Code: Select all
procedure TForm1.Button1Click(Sender: TObject);
begin
  Screen.Cursor := crHourGlass;
  try
    Memo1.Lines.Text := HTTPGet('http://delphiarea.com');
  finally
    Screen.Cursor := crDefault;
  end;
end;

Re: Need a Delphi Webrequest Example

PostPosted: January 15th, 2010, 1:57 pm
by Sezar
thanks Mr.Kambiz =;

Re: Need a Delphi Webrequest Example

PostPosted: January 17th, 2010, 10:04 pm
by kokkoras
There is an Indy component doing this.

Re: Need a Delphi Webrequest Example

PostPosted: January 18th, 2010, 12:48 pm
by Kambiz
I am against Indy. :)
Its components are somehow unpredictable.