• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 342
  • Last Modified:

Visual C++ To Download HTML from a web site...

Hello,
  I am currently a Visual Basic Programmer that is trying to make a long overdue switch to Visual C++.

  I need to set up a MFC app to download the HTML source from a HTTP and a HTTPS website.

  I was able to do this successfully using the MSINET.OCX (Microsoft Internet Transfer Control, v6.0) in Visual Basic.

  Is it possible to conquer this by NOT using an ActiveX control and using a built in C++ class?

  If I cannot use a built in C++ class, then how do I make that ActiveX control work?

Thanks, Christian
0
deadlyfluvirus
Asked:
deadlyfluvirus
  • 2
1 Solution
 
mnashadkaCommented:
If you're using MFC, you can use the CHttpConnection object to download a file to the local hard drive; something like:
#include <afx.h>
#include <afxinet.h>

int main()
{
  try
  {
    CInternetSession is("Downloader"); // Create an internet session
    CHttpConnection *http = is.GetHttpConnection("www.msn.com"); // Connect to the server
    CHttpFile *file = http->OpenRequest("GET", "/"); // Open a GET request for the default document
    file->SendRequest();

    DWORD status_code;
    file->QueryInfoStatusCode(status_code);

    if(status_code == 200) // Make sure that it's ok
    {
      // Create a new file and dump the results there
      CStdioFile local_file("default.htm", CFile::modeCreate | CFile::modeWrite | CFile::typeText);

      CString str;
      while(file->ReadString(str))
        local_file.WriteString(str);
      local_file.Close(); // Delete the local file
    }

    file->Close();
    delete file;

    http->Close();
    delete http;
  }
  catch(CInternetException *ie)
  {
    // Couldn't connect to the server
  }
  catch(CFileException *fe)
  {
    // Couldn't create the local file
  }

  return 0;
}
0
 
deadlyfluvirusAuthor Commented:
Thanks! That is exactly what I need!

Now...2 Things...
1.) Is there any way that it can download into a string instead of creating a file?

2.)How can I connect to an HTTPS server?

Thanks, Christian
0
 
mnashadkaCommented:
For the string part, you could just be appending each line to a string rather than doing the WriteString, changing:
// Create a new file and dump the results there
     CStdioFile local_file("default.htm", CFile::modeCreate | CFile::modeWrite | CFile::typeText);

     CString str;
     while(file->ReadString(str))
       local_file.WriteString(str);
     local_file.Close(); // Delete the local file

to
    CString total_string, str;
    while(file->ReadString(str))
      total_string += str;

As far as https is concerned, that's much more of an issue.  I can't tell you off of the top of my head how to do that.  You might want to look at the WinInet stuff.  But good luck.
0
 
robear7ntCommented:
Isn't there a CHttpsConnection?
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now