Register for EarthWeb's Million Dollar Sweepstakes!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Fast Track Visual C++ 6.0 Programming
(Publisher: John Wiley & Sons, Inc.)
Author(s): Steve Holzner
ISBN: 0471312908
Publication Date: 09/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


Opening the URL

Once we have an Internet session object, we can use that object’s OpenURL() function to read the file we want from the Internet. That function uses four different protocols, depending on what protocol you specify for the URL. For example, if the URL is http://www.microsoft.com/visualc, OpenURL() uses the HTTP protocol and pointer to an object of type CHttpFile. Here are the protocols and return types from OpenURL():

file://xxxx returns CStdioFile*
http://xxxx returns CHttpFile*
gopher://xxxx returns CGopherFile*
ftp://xxxx returns CInternetFile*

The following code shows how to use OpenURL():

CStdioFile* OpenURL(LPCTSTR pstrURL, DWORD dwContext = 1, DWORD dwFlags =
INTERNET_FLAG_TRANSFER_ASCII, LPCTSTR pstrHeaders = NULL, DWORD
dwHeadersLength = 0);

The first parameter is the URL to open, the next is application-specific data that we won’t use, and the third parameter sets the type of transfer you want. The third parameter can be one of the following:

INTERNET_FLAG_TRANSFER_ASCII Transfer the file as ASCII text.
INTERNET_FLAG_TRANSFER_BINARY Transfer the file as a binary file.
INTERNET_FLAG_RELOAD Get the data from the wire even if it is locally cached.
INTERNET_FLAG_DONT_CACHE Do not cache the data, either locally or in any gateways.
INTERNET_FLAG_SECURE Request secure transactions on the wire with Secure Sockets Layer or PCT.
INTERNET_OPEN_FLAG_USE_EXISTING_CONNECT If possible, reuse the existing connections to the server for new requests generated by OpenUrl instead of creating a new session for each connection request.
INTERNET_FLAG_PASSIVE Used for an FTP site.

The last two parameters in OpenURL() allow you to send additional header information to the server, but we won’t do that here. The following example shows how we use OpenURL() to open the Microsoft Visual C++ home page, getting a pointer to an object of class CHttpFile:

void CHTTPView::OnFileDownloadtext()
 {
    CInternetSession* Session = new CInternetSession();

    CHttpFile* HTTPFile = (CHttpFile*)
Session->OpenURL(“<http://www.microsoft.com/visualc>”);                        ⇐
    .
    .
    .

We also check the CHttpFile pointer to make sure it’s non-NULL before using it.

void CHTTPView::OnFileDownloadtext()
 {
    CInternetSession* Session = new CInternetSession();

    CHttpFile* HTTPFile = (CHttpFile*)
Session->OpenURL(“<http://www.microsoft.com/visualc>”);

    if(HTTPFile == NULL)                                               ⇐
        return;                                                        ⇐
            .
            .
            .

To store the Web page, we use a data buffer. We set that up, making it 2K in length, and read in the first 2K of the Web page using the CHttpFile Read() function.

void CHTTPView::OnFileDownloadtext()
 {
    #define BufferLength 2048                                          ⇐

    CInternetSession* Session = new CInternetSession();

    CHttpFile* HTTPFile = (CHttpFile*)
Session->OpenURL(“<http://www.microsoft.com/visualc>”);

    if(HTTPFile == NULL)
        return;

    char HTTPData[BufferLength];                                       ⇐

    HTTPFile->Read(HTTPData, BufferLength);                            ⇐
        .
        .
        .

We’ve read in the Web page’s HTML at this point, and we can display it in our view. Because we’ve based that view on the CEditView class, we can simply place the text in the text box that is the basis for the view. We reach that text box with GetEditCtrl().

void CHTTPView::OnFileDownloadtext()
 {
    #define BufferLength 2048

    CInternetSession* Session = new CInternetSession();

    CHttpFile* HTTPFile = (CHttpFile*)
Session->OpenURL(“<http://www.microsoft.com/visualc>”);

    if(HTTPFile == NULL)
        return;

    char HTTPData[BufferLength];

    HTTPFile->Read(HTTPData, BufferLength);

    GetEditCtrl().SetWindowText(HTTPData);                             ⇐
        .
        .
        .

We’ve displayed the Web page’s HTML at this point; finally, we close the CHttpFile object and close the Internet session.

void CHTTPView::OnFileDownloadtext()
 {
    #define BufferLength 2048

    CInternetSession* Session = new CInternetSession();

    CHttpFile* HTTPFile = (CHttpFile*)
Session->OpenURL(“<http://www.microsoft.com/visualc>”);

    if(HTTPFile == NULL)
        return;

    char HTTPData[BufferLength];

    HTTPFile->Read(HTTPData, BufferLength);

    GetEditCtrl().SetWindowText(HTTPData);
     HTTPFile->Close();                                              ⇐

    Session->Close();                                                ⇐

}


Figure 8.1  Downloading the HTML of the Visual C++ home page.


Connecting to the Internet

If you create a new Internet session and the computer isn’t connected to the Internet, the program brings up the standard Connect dialog box and asks the user to connect.


Now run the HTTP program, as shown in Figure 8.1, and select the Download text item in the File menu to download the HTML of the Visual C++ home page at Microsoft. Now we’re working with the HTTP protocol. Note that OpenURL() can use FTP, Gopher, and file protocols as well.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.