![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
To access the contents, click the chapter and section titles.
Fast Track Visual C++ 6.0 Programming
Opening the URLOnce we have an Internet session object, we can use that objects 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():
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 wont use, and the third parameter sets the type of transfer you want. The third parameter can be one of the following:
The last two parameters in OpenURL() allow you to send additional header information to the server, but we wont 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 its 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); ⇐ . . . Weve read in the Web pages HTML at this point, and we can display it in our view. Because weve 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); ⇐ . . . Weve displayed the Web pages 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(); ⇐ }
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 were working with the HTTP protocol. Note that OpenURL() can use FTP, Gopher, and file protocols as well.
|
![]() |
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. |