Click here to Skip to main content
11,492,385 members (62,376 online)
Click here to Skip to main content

Discover WIN32. How to use a sync/async retrieve HTTP content in your ASP & VB projects.

, 19 May 2002 127.8K 1.7K 32
Rate this:
Please Sign up or sign in to vote.
This article shows how to create one ATL COM component using the WinInet functions, how to use it in ASP programs and how to test it from the Visual Basic client. It also shows how to use multithreading support in this component.

Sample Image - snap.gif

Introduction

This article shows how to create one ATL COM component using the WinInet functions, how to use it in ASP programs and how to test it from the Visual Basic client. It also shows how to use multithreading support in this component.

This component can be used in the same way in Visual Basic, Delphi, Access or Microsoft SQL.

Overview

This application uses ATL, MFC, ASP, Visual Basic and WinInet functions. The application have an ATL project who provide some HTTP functions. It will demonstrate how to use this component in your ASP and Visual Basic projects.

The article is based on the Uwe Keim's article “Calling scripts within scripts” – many thanks to him for his good idea. Of course you will ask what is new in my component. The reason that made me build this component was the variable time delay at a HTTP request.

My solution was an asynchronous retrieve, which give the user the possibilities to check more often whether the page is completed or not.

The article also contains a Visual Basic client who tests the functionality of the component.

I assume the reader knows or at least has a fair idea about COM programming using ATL.

Details

The component consists of two parts: 

One standard WinInet retrieve module.

  • Retrieve method.
    Retrieve(BSTR newVal, BSTR *parOut)

  • GetPage function.
    bool GetPage(CString sURL, CString& sBody)

The Retrieve method just calls the GetPage function with the sURL parameter. Inside the function, there are the calls to WinInet API:

//Open one internet connection. The “INTERNET_OPEN_TYPE_PRECONFIG” 
//parameter tell the function to read the default proxy settings from 
// the registry(the proxy settings from connections in IE):
HINTERNET internet=InternetOpen("Daemon", INTERNET_OPEN_TYPE_PRECONFIG, 
                                 NULL, NULL, NULL);

//Make one connection with the desired URL. On the third parameter we 
//use the“INTERNET_FLAG_RELOAD” 
//while constrains the function not to usea local cached copy. 

//Take a look at the documentation of this functions to see all the 
// combination of parameters:
HINTERNET file_handle=InternetOpenUrl(internet, sURL, NULL, 0, 
                                      INTERNET_FLAG_RELOAD, 0);

//Read the data from the address:
InternetReadFile(file_handle, pagina, 100000, &bytes_read);

//Close the connection
InternetCloseHandle(internet);
The asp source code is very simple (full source code are in attachment file):
url="http://www.codeproject.com"

Set myORetrieve = CreateObject("RetrievePage.RetrievePage.1")

ret = myORetrieve.Retrieve(url)        

Response.Write ret

One asynchronous retrieve module.

  • RetrieveAsync method.
    RetrieveAsync(BSTR newVal, BSTR *parOut)

  • RetrieveAsyncCompleted method.
    RetrieveAsyncCompleted(BSTR newVal, BSTR *parOut)

  • RetrieveAsyncPage method.
    RetrieveAsyncPage(BSTR newVal, BSTR *parOut)

  • MyThreadGetPage function.
    UINT MyThreadGetPage(LPVOID pParam)

The client must use first the RetrieveAsync method with the desired URL parameter. This method launch one child thread that will retrieve the page in a separate thread. In this way the program will go to the next instruction and will not wait for the method to finish the request.

The program must implement one timer (or at the user's actions) who verify at some intervals if the RetrieveAsyncCompleted method return true (the requested page was retrieved). Only then the RetrieveAsyncPage method will provide the requested page.

While the RetrieveAsyncCompleted method return false you could send to the console a message such as “Please wait. Data is loading…”

//Make a new thread. The “MyThreadGetPage” is the name of the 
//function who will resolve the HTTP request. Inside the 
// MyThreadGetPage function, there is one call to the previous 
//discussed GetPage function.:
AfxBeginThread( MyThreadGetPage , this, THREAD_PRIORITY_NORMAL )


// The program use a coleection to keep all request to the component.
// Add a new URL request:
pObject->m_MapStringToHTTPpage[sKey]        = oHTTPpage;


// Look at collection to see if it is a new URL or not:
m_MapStringToHTTPpage.Lookup(sURL, oHTTPpage)
The Visual Basic source code is very simple (full source code are in attachment file, in RetrieveVBClient director):
'Launch the thread with the specified address
str = myORetrieve.RetrieveAsync(URL.Text)

'Look if the retrieved page is completed:
lRetrieveAsyncCompleted = myORetrieve.RetrieveAsyncCompleted(URL.Text) 

'If it is true, read the data from the specified URL address
str = myORetrieve.RetrieveAsyncPage(URL.Text)

If you want to see how to build an ATL component, take a look at the Build an ATL Crypt component article.

Installation

  • Copy the DLL into a directory with system execute privilege and register it with regsvr32 command or put it on the MTS.  To put it on the MTS is better because if we want to modify the component and register it again, this is possible without restarting the computer – in case when using the regsvr32 command who “blocks” your dll file.
  • Use directly the visual basic dialog console. Input some string on the URL edit box and click on the button!
  • Copy the director with asp pages on the Web server – and just try it !

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Adrian Bacaianu
Web Developer
Romania Romania
I make programming for over 4 years and extensive experience in C++, ASP, Pascal, MFC, COM+, ATL, TCP/IP, HTTP protocols, XML, XSL, SOAP and SQL.
For the last 2 years i working extensively at the background of financial sites (databases, n tier architecture).

I’m available for contracts and/or outsourcing (adrian_bacaianu@yahoo.com).

Comments and Discussions

 
GeneralMy vote of 5 Pin
manoj kumar choubey28-Feb-12 19:31
membermanoj kumar choubey28-Feb-12 19:31 
Questionretrieve HTTP content in VB.Net Wind Applica Pin
darkstock3-Feb-06 12:59
memberdarkstock3-Feb-06 12:59 
hi peeers,

i am currently developing multi-form Windows application in VB.Net using Visulal Studio 2005 Pro, but at the moment i am encountering the problem how to create the code that will:
1. retrieve HTTP specific content (stock quote from yahoo.finance) into the VB.Net UI
2. store that the content into virtual dataset that later on can be used for analysis (eg moving average)

I have looked at Adrian Bacaianu code snipets and articles which give some leads though it was written for excel, whilst in this project data should show in the Windows Application.

i know it is a big call, that is why it would be highly appreciate if someone can share his experience in finding code solution for the above issues., cheers

hood
GeneralPlease! Help me Pin
14118314-Apr-05 0:40
member14118314-Apr-05 0:40 
GeneralDoesn't give any result Pin
Sanjay Saini27-Aug-04 20:56
memberSanjay Saini27-Aug-04 20:56 
GeneralVB project crashes on XP Pin
Joel Katona9-Apr-02 12:47
memberJoel Katona9-Apr-02 12:47 
QuestionRe: VB project crashes on XP Pin
Alexander Kosenkov29-Nov-06 5:29
memberAlexander Kosenkov29-Nov-06 5:29 
Generall,Grab the URL from the Browser Pin
Anonymous18-Feb-02 8:33
memberAnonymous18-Feb-02 8:33 
GeneralQuestion Pin
Alex B27-Jan-02 10:22
memberAlex B27-Jan-02 10:22 
GeneralRe: Question Pin
Anonymous13-Feb-02 22:01
memberAnonymous13-Feb-02 22:01 
GeneralCOM Server DLL Never Unloads Pin
anonymous31-Jul-01 6:59
memberanonymous31-Jul-01 6:59 
GeneralRe: COM Server DLL Never Unloads Pin
Adrian Bacaianu31-Jul-01 23:13
memberAdrian Bacaianu31-Jul-01 23:13 
GeneralRe: COM Server DLL Never Unloads Pin
Wictor Wilén1-Aug-01 7:00
memberWictor Wilén1-Aug-01 7:00 
GeneralserverXMLHTTP Pin
Gil Messerman30-Jul-01 22:28
memberGil Messerman30-Jul-01 22:28 
GeneralRe: serverXMLHTTP Pin
Adrian Bacaianu30-Jul-01 23:40
memberAdrian Bacaianu30-Jul-01 23:40 
GeneralRe: serverXMLHTTP Pin
Christian Tratz1-Aug-01 6:30
memberChristian Tratz1-Aug-01 6:30 
GeneralRe: serverXMLHTTP Pin
Jeff Day2-Aug-01 8:20
memberJeff Day2-Aug-01 8:20 
GeneralNice Pin
Chris Maunder30-Jul-01 14:43
memberChris Maunder30-Jul-01 14:43 
GeneralRe: Nice Pin
Anonymous31-Jul-01 7:53
memberAnonymous31-Jul-01 7:53 
GeneralRe: Nice Pin
Cameron Grant1-Aug-01 23:00
memberCameron Grant1-Aug-01 23:00 
GeneralRe: Nice Pin
Gonzalo Ruiz de Villa7-Sep-01 3:55
memberGonzalo Ruiz de Villa7-Sep-01 3:55 
GeneralRe: Nice Pin
Adrian Bacaianu18-May-02 12:23
memberAdrian Bacaianu18-May-02 12:23 
GeneralRe: Nice Pin
bryce23-Jul-02 20:17
memberbryce23-Jul-02 20:17 
QuestionRe: Nice Pin
Alexander Kosenkov29-Nov-06 5:33
memberAlexander Kosenkov29-Nov-06 5:33 
AnswerRe: Nice Pin
bryce29-Nov-06 12:00
memberbryce29-Nov-06 12:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150520.1 | Last Updated 20 May 2002
Article Copyright 2001 by Adrian Bacaianu
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid