Download and save related pages

zhaozj2021-02-08  275

Download and save related pages

Author: Xu Jing Zhou

Download related sample source code (http://www.9cbs.net/cnshare/soft/11/11834.shtm)

Basic composition: Use the Wininet class, directly open the session, read and save the web to related files.

The achievement is as follows:

Bool getSourceHtml (CString theurl, cstring filename)

{

CinternetSession session;

CinternetFile * file = NULL;

Try

{

// Try to connect to the specified URL

File = (CinternetFile *) session.openurl (theurl);

}

Catch (CinternetException * m_pexception)

{

// If there is a mistake, set the file empty

File = NULL;

M_PEXCEPTION-> delete ();

Return False;

}

// Use Datastore to save the read web file

Cstdiofile DataStore;

IF (file)

{

CString Somecode; // can also use the lPTSTR type, will not delete / N Enter in the text

Bool Bisok = DataStore.Open (StrPath "//" FileName,

Cfile :: MODECREATE

| Cfile :: ModeWrite

| CFILE :: Sharedenywrite

| Cfile :: typetext);

IF (! bisok)

Return False;

// Read and write the web file until it is empty

While (file-> readstring (somecode)! = null) // If the LPTSTR type is used, read the maximum number nmax set 0, end the empty character

{

Datastore.writestring (someCode);

DataStore.WritString ("/ n"); // If somecode uses the lPTSTR type, you don't need this sentence.

}

File-> close ();

DELETE FILE;

}

Else

{

DataStore.WritString (_t ("to the Connection to the Specified Server"))));

Return False;

}

Return True;

}

Let's take a look at how to use it:

1. Join the Winint class, as follows:

#include "afxinet.h" // Add to download the header to use

2, add the following download to your project, you can use the following code (where the first parameter is the URL, the second parameter is the file name saved after the download):

/ / Get the path where the main program is located, there is a global variable StrPath

GetModuleFileName (null, strpath.getbuffersetlength (max_path 1), max_path);

StrPath.releaseBuffer ();

Int npos;

Npos = strpath.reversefind ('//');

StrPath = strpath.Left (npos);

Bool m_bdownloadfailed; m_bdownloadfailed = getSourceHtml ("http://www.vckbase.com", "news.txt"); // Download the default URL for the prompts in which the method is implemented, see the sample source code after downloading.

Contact information:

Email: jingzhou_xu@163.com

Future Studio (Future Studio)

转载请注明原文地址:https://www.9cbs.com/read-2106.html

New Post(0)