1.
[Image]Capture.jpg
? 2. Code Getpagebyurl
Static member variable define
String getpagebyurl::m_curpage = "";//Current saved Web page source code
curl* Getpagebyurl::m_curl = NULL;
/************************************************************************/
/* Function Name: Initialize
/* Function: Initialize the Libcurl library
/* Return Value: Success, return true; failed, return false
/************************************************************************/
BOOL Getpagebyurl::initialize ()
{
Curl_global_init (Curl_global_all);
M_curl = Curl_easy_init ();
if (M_curl)
{
Curl_easy_setopt (M_curl, curlopt_followlocation, 1L);
Curl_easy_setopt (M_curl, curlopt_writefunction, Writefunc);
Curl_easy_setopt (M_curl, Curlopt_writedata, &m_curpage);
}
Else
{
MessageBoxA (NULL, "Getpagebyurl::initialize failed!", "Getpagebyurl::initialize", mb_iconerror);
return false;
}
return true;
}
/************************************************************************/
/* Function Name: Writefunc
/* Function: Libcurl will call this standard function,
/* size_t function (void *ptr, size_t size, size_t nmemb, void *userdata);
/* To provide the opportunity to format Web page data
/* CURL_EASY_SETOPT (Curl, curlopt_writefunction, writefunc);
/* Return value: Returns the size of buffer
/************************************************************************/
size_t Getpagebyurl::writefunc (char *data, size_t size, size_t nmemb, String *writerdata)
{
if (Writerdata = = NULL)
return 0;
size_t len = size*nmemb;
Writerdata->append (data, Len);
return Len;
}
/************************************************************************/
/* Function Name: getpage
/* Function: According to the URL, fetch the corresponding web page source; using the Libcurl library
/* Return value: Successful, returns a string containing the Web page source code; failed, return empty string
/************************************************************************/
BOOL Getpagebyurl::getpage (
Const string& URLSTR,//url string
string& page//output parameter, return the corresponding pages source code
)
{
_assert (""! = Urlstr);
if (!m_curl)
{
MessageBoxA (NULL, "You must initialize Curl first!", "Getpagebyurl", mb_iconerror);
return false;
}
M_curpage.clear ();
Curl_easy_setopt (M_curl, Curlopt_url, Urlstr.c_str ());
Curlcode res = curl_easy_perform (m_curl);
if (res! = CURLE_OK)
{
Utility::messagebox (NULL, "Getpagebyurl::getpage", Mb_iconerror, "Get the page of%s failed!", Urlstr.c_str ());
return false;
}
Else
{
page = M_curpage;
return true;
}
}
/************************************************************************/
/* Function Name: Cleanup
/* Function: Clean up memory
/* return value: None
/************************************************************************/
void Getpagebyurl::cleanup ()
{
if (M_curl)
{
/* Always cleanup */
Curl_easy_cleanup (M_curl);
M_curl = NULL;
}
}
3. [Code]use it
Use Getpagebyurl
Getpagebyurl::initialize ();
String page = "";
Getpagebyurl::getpage ("Http://www.oschina.net/p/curl", page);
Getpagebyurl::getpage ("http://www.oschina.net/", page);
Getpagebyurl::cleanup ();
4. [File] GetPageByURL.h
#pragma once
#include <curl/curl.h>
Class Getpagebyurl
{
Public
Getpagebyurl (void);
Public
~getpagebyurl (void);
Private
static string M_curpage;
Static CURL *m_curl;
Private
Static size_t Writefunc (char *data, size_t size, size_t nmemb, string *writerdata);
Public
static bool Initialize ();
static bool GetPage (const string& URLSTR, string& page);
static void Cleanup ();
};
5. [File] GetPageByURL.cpp
#include "StdAfx.h"
#include "GetPageByURL.h"
Static member variable define
String getpagebyurl::m_curpage = "";//Current saved Web page source code
curl* Getpagebyurl::m_curl = NULL;
Getpagebyurl::getpagebyurl (void)
{http://www.enterdesk.com/special/shouhui/?
} Hand-painted pictures
Getpagebyurl::~getpagebyurl (void)
{
}
/************************************************************************/
/* Function Name: Initialize
/* Function: Initialize the Libcurl library
/* Return Value: Success, return true; failed, return false
/************************************************************************/
BOOL Getpagebyurl::initialize ()
{
Curl_global_init (Curl_global_all);
M_curl = Curl_easy_init ();
if (M_curl)
{
Curl_easy_setopt (M_curl, curlopt_followlocation, 1L);
Curl_easy_setopt (M_curl, curlopt_writefunction, Writefunc);
Curl_easy_setopt (M_curl, Curlopt_writedata, &m_curpage);
}
Else
{
MessageBoxA (NULL, "Getpagebyurl::initialize failed!", "Getpagebyurl::initialize", mb_iconerror);
return false;
}
return true;
}
/************************************************************************/
/* Function Name: Writefunc
/* Function: Libcurl will call this standard function,
/* size_t function (void *ptr, size_t size, size_t nmemb, void *userdata);
/* To provide the opportunity to format Web page data
/* CURL_EASY_SETOPT (Curl, curlopt_writefunction, writefunc);
/* Return value: Returns the size of buffer
/************************************************************************/
size_t Getpagebyurl::writefunc (char *data, size_t size, size_t nmemb, String *writerdata)
{
if (Writerdata = = NULL)
return 0;
size_t len = size*nmemb;
Writerdata->append (data, Len);
return Len;
}
/************************************************************************/
/* Function Name: getpage
/* Function: According to the URL, fetch the corresponding web page source; using the Libcurl library
/* Return value: Successful, returns a string containing the Web page source code; failed, return empty string
/************************************************************************/
BOOL Getpagebyurl::getpage (
Const string& URLSTR,//url string
string& page//output parameter, return the corresponding pages source code
)
{
_assert (""! = Urlstr);
if (!m_curl)
{
MessageBoxA (NULL, "You must initialize Curl first!", "Getpagebyurl", mb_iconerror);
return false;
}
M_curpage.clear ();
Curl_easy_setopt (M_curl, Curlopt_url, Urlstr.c_str ());
Curlcode res = curl_easy_perform (m_curl);
if (res! = CURLE_OK)
{
Utility::messagebox (NULL, "Getpagebyurl::getpage", Mb_iconerror, "Get the page of%s failed!", Urlstr.c_str ());
return false;
}
Else
{
page = M_curpage;
return true;
}
}
/************************************************************************/
/* Function Name: Cleanup
/* Function: Clean up memory
/* return value: None
/************************************************************************/
void Getpagebyurl::cleanup ()
{
if (M_curl)
{
/* Always cleanup */
Curl_easy_cleanup (M_curl);
M_curl = NULL;
}
}
Download the corresponding HTML page by URL using Libcurl