?
Solved

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

Posted on 2003-02-24
4
Medium Priority
?
339 Views
Last Modified: 2013-11-20
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
Comment
Question by:deadlyfluvirus
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 8

Accepted Solution

by:
mnashadka earned 400 total points
ID: 8010376
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
 

Author Comment

by:deadlyfluvirus
ID: 8011122
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
 
LVL 8

Expert Comment

by:mnashadka
ID: 8011663
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
 
LVL 1

Expert Comment

by:robear7nt
ID: 8075851
Isn't there a CHttpsConnection?
0

Featured Post

Get MySQL database support online, now!

At Percona’s web store you can order your MySQL database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question