Their instructions were simple enough: Get the raw news data from a given ASP page (no more than 4 times a day), create an ASP file with the formatted news in it, and #include it on your page. Here's how I did just that.
First, there were a few restrictions. My site is hosted by a local ISP, and the initial idea of writing a COM component to do the work had a few problems. Remember that the component could only get the content no more than 4 times a day. So the component needed to be called by some scheduling service - something my ISP may not want to do.
The component would also need write permissions to a folder on my site so I could write the ASP file, again maybe not a good idea.
The answer was to write an MFC application (an .exe) that runs on my computer. It gets the data, formats it, then FTP's the file to my ISP. I therefore had full control over when the application ran, and could easily debug and modify it as necessary.
Creating the App using the MFC AppWizard.
Creating the application is trivial using Visual C++ v6.0.• Use File/New, and select MFC AppWizard (exe) from the Projects tab.
• Enter the project name and choose Ok.
• Choose Single document. Hit Next.
• Use the defaults for Steps 2-5. Next, Next, Next, Next!
• On Step 6, choose CHtmlView for the Base class.
• Hit Finish, then Ok. You're done!
Let's write a browser!
I chose CHtmlView as our base class because it made our app a browser. That wasn't difficult, was it?Build and run the application (hit Ctrl+F5), and you'll see Microsoft's C++ home page. Let's quickly change it to point to the page on your site where the news will appear - that way when we're finished I can instantly see the results.
• In the ClassView double-click on the OnInitialUpdate method in your View class.
• Change "http://www.microsoft.com/visualc/" to "http://CoverYourASP.com/"
• Check it out with Ctrl+F5.
So now I have a working application to view the results on our web site - but I haven't done any real work. Using MFC's classes even the next part is easy. I'll briefly describe the code as I go but - (here's the copout) - use MSDN to get help on parts you don't understand!
Start by opening an HTTP connection to the news source. CInternetSession is the key to this process - just create one, and call the GetHttpConnection method:
| // create an internet session CInternetSession csiSession; // parse URL to get server/object/port CString sGetFromURL ( "http://aspwire.com/getnews.asp?site=secret&lang=1&cnt=10" ); if ( !AfxParseURL ( sGetFromURL, dwServiceType, sServerName, sObject, nPort ) ) throw; // open HTTP connection CHttpConnection* pHTTPServer = NULL; pHTTPServer = csiSession.GetHttpConnection ( sServerName, nPort ); // get HTTP object CHttpFile* pFile = NULL; pFile = pHTTPServer->OpenRequest ( CHttpConnection::HTTP_VERB_GET, sObject, NULL, 1, NULL, NULL, INTERNET_FLAG_RELOAD ); pFile->SendRequest(); // open file to store raw data into CStdioFile cfRaw ( "News.txt", CFile::modeCreate | CFile::modeReadWrite ); TCHAR sRaw [1024]; // transfer the data into our local file while ( pFile->ReadString ( sRaw, 1023 ) ) cfRaw.WriteString ( sRaw ); // I've finished with the HTTP objects // so close them pFile->Close(); pHTTPServer->Close(); |
That's the first part done! I now have a file on our local disc that contains the news feed's raw data. It's in the format URL, Date, Type, Title - here's a section of the file I just created:
http://aspwire.com/redir?2888
7/28
article
Hosting multiple domains on one IP Address
http://aspwire.com/redir?2865
7/28
article
404 Error Page
Now I have to get the data I need from this file, and format it the way I need.
The ASP file we're going to generate is shown below - I will #include it just like any other file, then call the function ShowNews().
utils/News.asp<%function ShowNews ( ) { Out('<table width="100%" border="0" align="center">' ); Out('<tr><td width="90%" nowrap valign="top">' ); Out('<table width="100%">'); Out('<tr><td>• <a href="http://aspwire.com/brief.asp?6275" target="CYAExternal">Amazing New ASP Template Pack</a></td></tr>'); Out('<tr><td>• <a href="http://aspwire.com/brief.asp?6274" target="CYAExternal">A/V Publisher</a></td></tr>'); Out('<tr><td>• <a href="http://aspwire.com/brief.asp?6273" target="CYAExternal">fXgraph Ver 4 Beta Animated Graphing Comp Released</a></td></tr>'); Out('<tr><td>• <a href="http://aspwire.com/brief.asp?6271" target="CYAExternal">CompareFilesX now has reporting built in</a></td></tr>'); Out('<tr><td>• <a href="http://aspwire.com/brief.asp?6272" target="CYAExternal">CodeCharge Studio ASP code generator Released</a></td></tr>'); Out('<tr><td>• <a href="http://aspwire.com/brief.asp?6298" target="CYAExternal">URLDecode Function</a></td></tr>'); Out('<tr><td>• <a href="http://aspwire.com/brief.asp?6297" target="CYAExternal">An Extensive Examination of the DataGrid Web Control: Part 5</a></td></tr>'); Out('<tr><td>• <a href="http://aspwire.com/brief.asp?6296" target="CYAExternal">Protecting Portions of your Web Site</a></td></tr>'); Out('<tr><td>• <a href="http://aspwire.com/brief.asp?6295" target="CYAExternal">Specifying Configuration Settings in Web.config</a></td></tr>'); Out('</table>'); Out('</td><td width="10%" nowrap valign="bottom">' ); Out('<a href="http://aspwire.com/" target="CYAExternal"><img src="images/Affiliates/ASPWire.gif" width="100" height="30" alt="News from ASPWire" border="0"></a><br><b>Powered by ASPWire</b><br><a href="GetNews.asp">Read how this is done</a>'); Out ( '</td></tr></table>' ); } %> |
| // open file to store formatted data into TCHAR* sNewsFile = _T( "News.asp" ); CStdioFile cfASP ( sNewsFile, CFile::modeCreate | CFile::modeWrite ); // output the static part of the asp file - the function declaration, // the ASPWire image and the heading cfASP.WriteString ( _T("<%\nfunction ShowNews ( )\n{\n") ); cfASP.WriteString ( _T("\tOut('<a href=\"http://aspwire.com/\"><img src=\"images/ASPWireNews.gif\" alt=\"News from ASPWire\" border=0></a>');\n") ); cfASP.WriteString ( _T("\tOut(' (<a href=\"GetNews.asp\">Read how this is done</a>)<p>');\n") ); cfASP.WriteString ( _T("\tOut('<table width=\"100%\">');\n") ); cfRaw.SeekToBegin (); CString sLine; bool bNewRow = true; for (int i=0; ; i++) { if ( FALSE == cfRaw.ReadString ( sLine ) ) break; switch ( i % 4 ) { case 0: // URL cfASP.WriteString ( _T("\tOut('") ); if ( bNewRow ) cfASP.WriteString ( _T ( "<tr>" ) ); bNewRow = !bNewRow; cfASP.WriteString ( _T("<td>• <a href=\"") ); cfASP.WriteString ( sLine ); cfASP.WriteString ( _T("\" target=\"CYAExternal\">") ); break; case 3: // title sLine.Replace ( "'", "\\'" ); cfASP.WriteString ( sLine ); cfASP.WriteString ( _T("</a></td>") ); if ( bNewRow ) cfASP.WriteString ( _T ( "</tr>" ) ); cfASP.WriteString ( _T("');\n") ); break; } } cfASP.WriteString ( _T("\tOut('</table>');\n") ); cfASP.WriteString ( _T("}\n%>") ); // close files cfRaw.Close (); cfASP.Close (); |
Uploading a file via FTP is almost exactly the same as the way I used HTTP objects earlier - I call CInternetSessions' GetFtpConnection method:
| // now upload file to my website if ( !AfxParseURL ( sSendToURL, dwServiceType, sServerName, sObject, nPort ) ) throw; // open FTP connection pFTPServer = csiSession.GetFtpConnection ( sServerName, _T( "username" ), _T( "password" ), nPort ); // change to utils folder pFTPServer->SetCurrentDirectory ( _T( "utils" ) ); // put the new file in the folder pFTPServer->PutFile ( sNewsFile, sNewsFile ); |
Writing this code is an excellent way to introduce yourself to MFC's internet objects. Remember that these are all available to use just as easily from a COM component too. Have fun!
discuss this topic to forum
