Using wininet.dll

Can anyone provide any public domain examples of using the wininet.dll?

I have a copy of wininet.pas from Borland but when I tried some example code from an article I found on the web, it complains about various things including INTERNET_OPEN_TYPE_DIRECT, which seems pretty basic.

What I want to do is open a secure HTTP connection (SSL) so an example of that would get bonus points.

Ciao!
LVL 1
ergatesAsked:
Who is Participating?
 
d003303Connect With a Mentor Commented:
Yo,
here's a unit that can handle all URL requests (including https) via wininet functions. You will need the HTTPProt unit from F. Piette that Zif mentioned above for the Base64 encryption routine.

unit url_req;

interface

uses Classes, Windows, WinInet;

const TheUserAgent = 'Slash-Agent';

// initialize all values
procedure WinInetInit;

// uninitialize all values
procedure WinInetUnInit;

// get httpdata in stream
procedure WinInetGetDataFromURL(URL, User, Password : string; AStream : TMemoryStream);

implementation

uses SysUtils, HTTPProt;

var
    FInetSessionHandle,
    FConnectionHandle  : HINTERNET;

procedure WinInetInit;
begin
  // use default settings from registry
  FInetSessionHandle := InternetOpen(TheUserAgent, INTERNET_OPEN_TYPE_PRECONFIG,
                                nil, nil, 0);
end;

procedure WinInetUnInit;
begin
  InternetCloseHandle(FInetSessionHandle);
end;

procedure WinInetGetDataFromURL(URL, User, Password : string; AStream : TMemoryStream);
const BufSize          = 1024;
      Terminator: Byte = $00;
var ReadResult         : Boolean;
    CanonicalizeURLSize,
    BytesRead          : DWord;
    FAddHeader         : string;
    Buffer             : array[0..(BufSize - 1)] of Char;
    FURL               : array[0..INTERNET_MAX_URL_LENGTH] of Char;
    FSessionContext    : DWord;
begin
  FSessionContext := 0;
  if User <> ''
   then FAddHeader := 'Authorization: Basic ' + EncodeStr(encBase64, User + ':' + Password);

  if FInetSessionHandle = nil
   then raise Exception.Create('Invalid session handle');

  CanonicalizeURLSize := INTERNET_MAX_URL_LENGTH + 1;
  if InternetCanonicalizeURL(PChar(URL), FURL, CanonicalizeURLSize, 0) = false
   then raise Exception.Create('InternetCanonicalizeURL Error code ' + IntToStr(GetLastError));

  FConnectionHandle := InternetOpenURL(FInetSessionHandle, FURL, PChar(FAddHeader), Length(FAddHeader), INTERNET_FLAG_RELOAD or INTERNET_FLAG_RAW_DATA, FSessionContext);

  if FConnectionHandle = nil
   then raise Exception.Create('InternetOpenURL Error code ' + IntToStr(GetLastError));

  try
    // read data
    repeat
      ReadResult := InternetReadFile(FConnectionHandle, @Buffer[0], BufSize, BytesRead);
      if ReadResult = false
       then Break;
      AStream.SetSize(AStream.Size + BytesRead);
      AStream.Write(Buffer[0], BytesRead);
    until BytesRead = 0;
    AStream.SetSize(AStream.Size + 1);
    AStream.Write(Terminator, 1);
  finally
    InternetCloseHandle(FConnectionHandle);
  end;
end;

end.

//// end of unit ////

And here's a quick example of how it works. A *very* basic browser.

// unit code
unit https;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

type
  TForm1 = class(TForm)
    Memo1: TMemo;
    Edit1: TEdit;
    Button1: TButton;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

uses url_req;

procedure TForm1.FormCreate(Sender: TObject);
begin
  WinInetInit;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  WinInetUnInit;
end;

procedure TForm1.Button1Click(Sender: TObject);
var AStream : TMemoryStream;
    TheURL  : string;
begin
  if Edit1.Text = ''
   then Exit;
  Memo1.Clear;
  AStream := TMemoryStream.Create;
  try
    TheURL := Edit1.Text;
    WinInetGetDataFromURL(TheURL, '', '', AStream);
    Memo1.SetSelTextBuf(AStream.Memory);
  finally
    AStream.Free;
  end;
end;

end.

/// form code
object Form1: TForm1
  Left = 200
  Top = 108
  Width = 444
  Height = 479
  Caption = 'Form1'
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OnCreate = FormCreate
  OnDestroy = FormDestroy
  PixelsPerInch = 96
  TextHeight = 13
  object Memo1: TMemo
    Left = 20
    Top = 44
    Width = 397
    Height = 389
    TabOrder = 0
  end
  object Edit1: TEdit
    Left = 20
    Top = 16
    Width = 317
    Height = 21
    TabOrder = 1
    OnEnter = Button1Click
  end
  object Button1: TButton
    Left = 340
    Top = 16
    Width = 75
    Height = 25
    Caption = 'Open'
    TabOrder = 2
    OnClick = Button1Click
  end
end

Have fun,
Slash/d003303
0
 
ZifNabCommented:
Ergates,

I propose you look and use these components for http and other net related things...

And besides there cardware..., lot's of examples, great support,....

http://www.rtfm.be/fpiette/indexuk.htm

Regards, ZiF.
0
 
d003303Commented:
Yo,
if you want to use wininet services, IE 3.0 or higher has to be installed on each system your software runs on. I also recommend the components Zif mentioned, but unfortunately they do not support HTTPS (yet ?).

Slash/d003303
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
ronit051397Commented:
Download the WIN-INET.ZIP example from:
http://www.bid.w1.com/html/delphi.html
0
 
ergatesAuthor Commented:
Hi Zif,

I was going to say I know about Francois' components, they are very good but don't support SSL. I need to use SSL which winet supports. I don't want to port SSL to Delphi unless I have to!

Also Slash: there is a redistributable pack including wininet so you don't need IE installed.

Thanks,
0
 
ZifNabCommented:
d003303, the component suit has a HTTP Component? Am I missing here something, please explain..
0
 
d003303Commented:
Zif,
HTTPS is a secure HTTP protocol that encrypts the content with a SSL key. The security algo is copyrighted by the RSA Data Security Inc.

Slash/d003330
0
 
ZifNabCommented:
Thanks d003303. (and SSL = Secure Sockets Layer, I guess)

Ergates, here are some things I found on the web :

1. In the Wininet.pas ..... Is there every constant, etc.. translated?

" I ended up using the wininet API to do this, which one can access from Delphi.

 Just put wininet in your "uses" clause.

 Here's the URL of an article with examples on how to use wininet.

  http://www.microsoft.com/mind/1297/inet/inet.htm.

 It even explains how do make the https call. "

some places to look after :

 http://www.psy.uq.edu.au:8080/~ftp/Crypto
 http://www.netscape.com/newsref/std/SSL.html
0
 
ergatesAuthor Commented:
Hi,
I use D3 and wininet.pas dated 4. August 1997. I'll send you my version and the HTTPProt.pas unit.

Cheers,
Slash/d003303
0
 
d003303Commented:
Excellent!

My wininet.pas was waayy out of date. Maybe Borland should spend time keeping the files up to date rather than changing names...

I got a bit confused with Stream.setsize - on D2 this clears the stream so I was losing the returned data. I commented out the calls to SetSize and it worked fine.

Many thanks
0
 
ergatesAuthor Commented:
Yo, you're right. It is not necessaray to re-set the stream size in D3 neither in D2 if you use the Write method. I first built this part of the code with other methods that needed SetSize. Let's call this line 'legacy garbage'.

Have fun,
Slash/d003303
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.