[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Using wininet.dll

Posted on 1998-05-11
11
Medium Priority
?
488 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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 1600 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses
Course of the Month17 days, 20 hours left to enroll

831 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