Need a Delphi Webrequest Example

Please discuss general Delphi programming topics here.

Need a Delphi Webrequest Example

Postby Sezar » January 14th, 2010, 6:46 am

I need an example of webrequest in delphi that request a web url and get its context...
thanks...
Sezar
Sezar
Junior Member
Junior Member
 
Posts: 42
Joined: April 18th, 2007, 4:40 pm
Location: Mashhad,Iran

Re: Need a Delphi Webrequest Example

Postby Kambiz » January 14th, 2010, 9:55 am

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;
Kambiz
User avatar
Kambiz
Administrator
Administrator
 
Posts: 2429
Joined: March 7th, 2003, 7:10 pm

Re: Need a Delphi Webrequest Example

Postby Sezar » January 15th, 2010, 1:57 pm

thanks Mr.Kambiz =;
Sezar
Sezar
Junior Member
Junior Member
 
Posts: 42
Joined: April 18th, 2007, 4:40 pm
Location: Mashhad,Iran

Re: Need a Delphi Webrequest Example

Postby kokkoras » January 17th, 2010, 10:04 pm

There is an Indy component doing this.
Fotis
User avatar
kokkoras
Moderator
Moderator
 
Posts: 317
Joined: March 12th, 2005, 11:19 pm
Location: Thessaloniki, Greece

Re: Need a Delphi Webrequest Example

Postby Kambiz » January 18th, 2010, 12:48 pm

I am against Indy. :)
Its components are somehow unpredictable.
Kambiz
User avatar
Kambiz
Administrator
Administrator
 
Posts: 2429
Joined: March 7th, 2003, 7:10 pm


Return to Delphi Programming

Who is online

Users browsing this forum: No registered users and 8 guests

cron