Solved

Using wininet.dll

Posted on 1998-05-11
11
443 Views
Last Modified: 2010-04-06
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!
0
Comment
Question by:ergates
  • 4
  • 3
  • 3
  • +1
11 Comments
 
LVL 8

Expert Comment

by:ZifNab
ID: 1344321
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
 
LVL 4

Expert Comment

by:d003303
ID: 1344322
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
 
LVL 5

Expert Comment

by:ronit051397
ID: 1344323
Download the WIN-INET.ZIP example from:
http://www.bid.w1.com/html/delphi.html
0
 
LVL 1

Author Comment

by:ergates
ID: 1344324
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
 
LVL 8

Expert Comment

by:ZifNab
ID: 1344325
d003303, the component suit has a HTTP Component? Am I missing here something, please explain..
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 4

Expert Comment

by:d003303
ID: 1344326
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
 
LVL 8

Expert Comment

by:ZifNab
ID: 1344327
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
 
LVL 4

Accepted Solution

by:
d003303 earned 400 total points
ID: 1344328
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
 
LVL 1

Author Comment

by:ergates
ID: 1344329
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
 
LVL 4

Expert Comment

by:d003303
ID: 1344330
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
 
LVL 1

Author Comment

by:ergates
ID: 1344331
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

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now