Function to Return HTML Source of a URL | CodeGuru

Function to Return HTML Source of a URL

Environment: Visual C++ 6.0 Here’s a function that gives you access to the source html of a URL. As written the function stores the results to a .txt file, but you could easily modified the function to fit your needs. From there you can parse the data, create a page on the fly, and use […]

Written By
CodeGuru Staff
CodeGuru Staff
Mar 21, 2001
1 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

Environment: Visual C++ 6.0

Here’s a function that gives you access to the source html of a URL.
As written the function stores the results to a .txt file, but you could easily
modified the function to fit your needs. From there you can parse the data, create a
page on the fly, and use the Navigate2 method to display the results in a
browser. This function could be useful for develping html views that block adds
or give the user the option of text-only page views. With a little imagination you could probably
come up with many other uses for this code.

The GetSourceHtml function makes use of the CInternetSession class, so be sure to place #include “afxinet.h”
below #include “stdafx.h” in the source file that contains the GetSourceHtml function.

To use GetSourceHtml, pass it a URL as a CString in the following format: GetSourceHtml( _T(“https://www.codeguru.com”) );.
You can then use Notepad to view the results. You will find it in the C: directory as rawHtml.txt

BOOL GetSourceHtml(CString theUrl)
{
 // this first block does the actual work
 CInternetSession session;
 CInternetFile* file = NULL;
 try
 {
  // try to connect to the URL
  file = (CInternetFile*) session.OpenURL(theUrl);
 }
 catch (CInternetException* m_pException)
 {
  // set file to NULL if there’s an error
  file = NULL;
  m_pException->Delete();
 }
 // most of the following deals with storing the html to a file
 CStdioFile dataStore;
 if (file)
 {
  CString somecode;
  BOOL bIsOk = dataStore.Open(_T(“C:\rawHtml.txt”),
                              CFile::modeCreate
                              | CFile::modeWrite
                              | CFile::shareDenyWrite
                              | CFile::typeText);
  if (!bIsOk)
   return FALSE;
  // continue fetching code until there is no more
  while (file->ReadString(somecode) != NULL)
  {
   dataStore.WriteString(somecode);
  }
  file->Close();
  delete file;
 }
 else
 {
  dataStore.WriteString(_T(“Could not establish a connection with the server…”));
 }
}
CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.